IMDTouchKnowledge BaseShop main store

How-to

How to Rotate a 3D Object on a Touch Panel With Drag Input

HMI and touchscreens Published 7 min read

Short answer

Leave the camera fixed and rotate the object: map each drag to a rotation delta at 0.3 degrees per pixel and compose it onto the object's current orientation as a quaternion, around a pivot at its center. Only the object transform changes per drag, so no camera up-vector or translation is ever recalculated.

On this page
  1. Should the drag rotate the camera or the object?
  2. Where does the rotation pivot go?
  3. How much rotation should one pixel of drag produce?
  4. How do I apply each drag to the object's current orientation?
  5. How do I wire the drag handler step by step?
  6. Why does the object roll or jump instead of turning?
  7. Does the panel send touch as mouse events or touch events?
  8. Questions asked

Rotate the object, not the camera, and start at a sensitivity of 0.3 degrees per pixel of drag. Fix the camera once, positioned away from the object and aimed at it, then treat every drag as a small rotation delta that composes onto the object's current orientation around a pivot at its center. That is the arrangement that is easy to maintain: no camera up-vector recalculation, no translation away from the origin, and the working mouse version converts to touch with one handler decision. This walkthrough gives the sensitivity to start with, the composition math with real numbers, the handler steps, and the touch-event check for the panel.

Should the drag rotate the camera or the object? #

Leave the camera fixed in position and aim, and apply every drag to the object's orientation — only the object transform changes, so no camera up-vector or translation is recalculated during a drag.

A camera carries three things you would have to keep coherent if you moved it: position, viewing direction, and the up reference that stops the view rolling. Orbiting the camera to turn the object recomputes all three on every drag. Rotating the object recomputes none of them. Translating the object is no substitute either: translation changes position, not orientation, and it walks the object away from the rotation center you picked. None of these approaches is wrong — an orbiting camera is a legitimate inspection viewer — but each adds work this design does not need.

Where does the rotation pivot go? #

At the model's own origin, provided that origin sits at the object's geometric center — then the object turns in place and every other point travels around that stationary center.

Check the model's local origin before wiring any input. If the origin is offset from the geometry, the object's center sweeps a circle around the pivot on every drag, which reads as an orbit or a sideways lurch rather than a turn. Fix it by wrapping the rotation in a translate-rotate-translate: move the intended pivot to the coordinate origin, apply the composed rotation, move it back. Keep the object's placement in the scene separate from the orientation you update during dragging. The same pivot discipline governs 2D HMI graphics, as in rotating WinCC graphics around one shared pivot.

How much rotation should one pixel of drag produce? #

Start at 0.3 degrees per pixel: on a panel 1280 pixels wide, one full-width swipe then turns the object 1280 × 0.3 = 384 degrees, a little over one full revolution, so an operator reaches any face in a single gesture.

The basis is that swipe reach, not pointer resolution. What moves the number is panel width in pixels: a 1920-pixel-wide panel at the same 0.3 degrees per pixel would give 576 degrees per swipe, which is too fast, so hold the per-swipe total near 384 degrees and rescale to 384 ÷ 1920 = 0.2 degrees per pixel. Each 0.1 degrees per pixel of adjustment moves a full-width swipe by 128 degrees on a 1280-pixel panel. Sensitivity is the last dial to touch, not the first: it never corrects rotation about the wrong axis or the wrong pivot, so settle the geometry before tuning the feel.

How do I apply each drag to the object's current orientation? #

Compose quaternions: a rotation q1 followed by a rotation q2 is the product q2·q1, and a rotation of θ about a unit axis n is q = [cos(θ/2), n·sin(θ/2)] — multiply each drag delta onto the stored orientation, and never re-derive orientation from the pointer's screen position.

Storing orientation as a quaternion is what makes incremental dragging stable: each drag continues from the object's present pose instead of snapping to a pose computed from wherever the contact happens to sit. The compose order also encodes your axis choice. Multiplying on the left, q_new = q_delta · q_current, applies the delta about screen-fixed axes: a horizontal drag always spins the object about the screen's vertical, whatever its current pose. Multiplying on the right, q_current · q_delta, applies it about the object's own axes, which follow the object and read as rolling after a few drags. For an inspection viewer, commit to screen-fixed axes and left-multiply.

Worked example. A drag of 150 px right and 80 px down at 0.3 degrees per pixel gives 150 × 0.3 = 45 degrees of yaw about the screen vertical (Y) and 80 × 0.3 = 24 degrees of pitch about the screen horizontal (X). In half-angle form:

  • q_yaw = [cos(45°/2), (0, sin(45°/2), 0)] = [0.9239, (0, 0.3827, 0)]
  • q_pitch = [cos(24°/2), (sin(24°/2), 0, 0)] = [0.9781, (0.2079, 0, 0)]

With the product rule (w1, v1)·(w2, v2) = (w1w2 − v1·v2, w1v2 + w2v1 + v1×v2), the delta for yaw then pitch, q_delta = q_pitch · q_yaw, is:

  • scalar part: 0.9781 × 0.9239 = 0.9037
  • vector part: (0.1921, 0.3743, 0.0796)

Check: 0.9037² + 0.1921² + 0.3743² + 0.0796² = 1.0000 after rounding, so the product is a unit quaternion and a valid rotation. Its equivalent single rotation is 2 × arccos(0.9037) ≈ 50.7 degrees about a diagonal axis — not 45 + 24 = 69 degrees, because rotations about different axes do not add; only rotations about the same axis do. The final update is q_new = q_delta · q_current.

How do I wire the drag handler step by step? #

Treat the drag as contact-driven state, not as a formula over absolute pointer position: begin on contact inside the control, accumulate movement between events, compose each delta onto the stored orientation, and clear the state when contact ends.

  1. Set the view once: camera away from the object and aimed at the pivot, with the whole object visible.
  2. Confirm the pivot is the point that must stay stationary; wrap an offset model origin in the translate-rotate-translate from the previous section.
  3. Enter the dragging state only after the contact moves 10 px from its start point, and only inside the control — enough slop to absorb landing jitter while a tap on an overlapping control still behaves as a tap.
  4. On each movement event, subtract the previous contact position from the current one and convert each component to degrees with the sensitivity you set.
  5. Build the delta quaternion in half-angle form and left-multiply it onto the stored orientation.
  6. Store the latest contact position as the reference for the next event.
  7. Clear the dragging state on release and on cancellation — lost contact, focus leaving the control — so rotation cannot run on after the finger lifts.

A handler that misses the release event keeps stale dragging state, the same class of fault that leaves a WebIQ button highlighted after touch release. And keep this interaction on the visualization: a rotating 3D view is a viewing aid, not position feedback from a machine axis, and any link to machine motion is a separate, explicitly engineered function.

Why does the object roll or jump instead of turning? #

Because the handler changed more than the orientation: after one correct drag exactly one thing differs — the object's orientation — and each visible symptom below traces to a specific wrong transform.

What you see What to check
The view rolls or the vertical swings Camera orientation changing, or right-axis composition adding object-local roll
The object sweeps across the display Pivot or model origin mismatch, or transform order in the wrapper
The object snaps to a new pose at first contact Handler using absolute pointer position instead of the delta from the initial contact
Mouse drag works, touch does nothing Panel delivering touch on an event path the handler does not listen to
Rotation continues after the finger lifts Missing release or cancellation handling, stale dragging state
The object shrinks or vanishes mid-drag Unintended translation in the transform, or clipping in the display control

Test with a model that has an obvious front, top, and side: a symmetric part hides reversed axes and rotation about the wrong point.

Does the panel send touch as mouse events or touch events? #

That depends on the operating system and UI framework, and it decides only which handler you write — the rotation math is identical either way, so settle the question before adapting the working mouse version.

Run the working mouse drag on the target panel and observe which events arrive; the OS input configuration and the framework's event documentation name the paths. Then verify: horizontal and vertical drags separately, to confirm each axis and direction; contact at several locations in the control, since first contact must not change orientation; release inside and outside the control where the platform allows it, confirming rotation stops; several consecutive rotations, confirming the pivot holds and the camera never changes. If a press-and-hold before movement opens a context menu instead of starting the drag, that is gesture emulation, handled separately in fixing press-and-hold right-click in HMI apps on a touch panel. If the contact point lands offset from where the finger actually is, sort out why touch calibration fails or drifts before touching the handler.

Questions asked

How do I rescale the drag sensitivity for a wider panel?

Keep the rotation per full-width swipe constant instead of the per-pixel value. One full swipe should turn the object a little over one revolution, about 384 degrees. On a 1920-pixel-wide panel that is 384 divided by 1920, or 0.2 degrees per pixel; on a 1280-pixel panel it is 0.3 degrees per pixel. Any value in that range keeps every face reachable with one sweep of a finger.

Can I store the orientation as three angles instead of a quaternion?

For a viewer restricted to yaw and pitch about fixed screen axes, a two-angle pair is enough and never locks, because those two axes stay perpendicular. The moment you add a third rotation axis, angle triples reach a pose where one axis loses effect and the display flips. Quaternions compose through those poses without a singularity, which is why the composition step uses them.

Should the object keep spinning after I lift my finger?

Default to no on an operator screen: the object should stop with the hand, so what the operator sees is what they set. Inertia is a deliberate visual effect, implemented as a per-frame decay of the measured angular velocity that stops below a small threshold, and it belongs on informational kiosks rather than stations next to running machinery.

What should happen when a second finger lands during a drag?

On a single-contact interface, ignore the extra contact or end the drag cleanly, and never let a second contact overwrite the active one mid-gesture. On a panel that reports multi-touch, reserve one contact for rotation and give a deliberate two-finger pinch its own zoom handler, so a zoom never re-enters the rotation path.

Keep reading

  • How-to

    How to Style a Pressed Button State on a Touch HMI

    The pressed look on a browser-based HMI is the CSS :active pseudo-class, styled like any other state rule. One test decides everything else: how long the highlight lasts. Feedback only during contact is :active or the browser tap highlight; feedback stuck after release is a :hover rule matched by touch, which belongs inside @media (hover: hover).

    HMI and touchscreens

  • Selection guide

    Do You Need SCADA Software for a Modbus HMI Project

    No. Modbus only proves a possible data link, so pick the runtime target first: a touch panel programmed by its own editor for machine-side parameter control, or a licensed PC SCADA runtime when the screens must run on a computer. The documented demonstration mode of a Vijeo Citect-class runtime stops after about 15 minutes with live devices, which settles a bench test but not continuous operation.

    HMI and touchscreens

  • Troubleshooting

    WinCC flexible IO field not editable on the panel

    An IO field that displays its tag but ignores touches is disabled, not over-protected: the enabled checkbox for operator interaction is cleared even though the secured box is unchecked. Tick enabled on the object first, then clear or set the secured box to match your access policy, compile and download. Clearing security alone never makes the field interactive.

    HMI and touchscreens