Burf2000
Burf2000

Reputation: 5193

iPhone OpenGL: Finding out the rotation of an object

I need to find the rotation of an object. So I am rotating a cube on 2 axis and when a user clicks, I need to rotate the cube to the face they clicked on. I looked at the ModelView matrix and converted the points from radian to degrees but the range goes from 0 to 180 (90 happens twice)

Upvotes: 0

Views: 204

Answers (1)

Andrew
Andrew

Reputation: 24846

If you know a modelview matrix then you can find the quaternion representing your rotation unambiguously.

Here is a link with a formula to compute the quaternion from a rotation matrix http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm

EDIT:

If you have 2 modelview matrices for your object and you want a smooth movement from one position to another you can still use quaternions!

Let you have 2 matrices w1 w2. For each build a quaternion q1 and q2 using the existing formulas - it's simple. Make the animation like this:

on each animation step construct the current quaternion for object rotation:

allTransform = q2 - q1
currentTransform = q1 + t*allTransform

where t is from [0, 1], allTransform is s quaternion When you know the currentTransform quaternion build a model view matrix from it - just using a ready formula.

That will produce nice and smooth animation.

Upvotes: 2

Related Questions