Advertisement Jump to content
Sign in to follow this  

Rotating a model using gestures

This topic is 2492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I have a 3d model rendered on the screen. I zoom in/out it using the pinch gesture (that gives me the vectors position1, position2, and the values delta1 and delta2).

Next step would be to calculate the rotation for the model. How could I get so considering the input information I can deal with?


Share this post

Link to post
Share on other sites

I think there are multiple ways. Which one to choose depends on how you would like the rotation to behave.
I could imagine that the axis to rotate around is perpendicular to the view direction and the swipe direction.
The following code does exactly this:

// Compute the swipe direction (The forward, up and right vectors are given in world space. They are the first three columns of the view matrix.)
// mouseDeltaX and ...Y are the change of the finger (or mouse) position (normalized through division by the viewport resolution.)
Vector3 swipeDir = viewMatrix.Right * mouseDeltaX + viewMatrix.Up * mouseDeltaY;

// The rotation axis is perpendicular to the swipe direction and the forward axis of the camera.
Vector3 rotationAxis = Vector3.Cross(swipeDir, viewMatrix.Forward);

// Transform the rotation axis from world space to object space.
rotationAxis = Vector3.Transform(rotationAxis, Matrix.Invert(worldMatrix));

// Compute rotation quaternion (the angle is some constant) that spins the object a little bit further.
Quaterion rotation = Quaternion.CreateFromAxisAngle(rotationAxis, angle);

The actual rotation of the model is then updated by:
model.Rotation *= Quaternion.Normalize( rotation );

I hope this helps you a little. smile.png

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!