Mike
Mike

Reputation: 963

How to calculate rotation axis and angle?

I am trying to rotate a model in 3D so that it faces the right direction. The rotation I want is fairly trivial and can be broken down into two steps:

  1. Rotate the model 90 degrees on its x-axis.
  2. Rotate the model 180 degrees on its z-axis (relative to the first rotation).

The way to set a model's rotation in the framework I'm using (openFrameworks) is by calling its setRotation method. This method takes an angle, as well as floats x, y and z that specify the axis of rotation. How do I calculate the axis of rotation and angle for this particular rotation? I can't rotate the model two times sequentially because any call to setRotation overwrites previous rotations.

Please let me know if I can provide more information or clarity.

EDIT: In case anyone has the same question, this post helped me a lot.

Upvotes: 0

Views: 3128

Answers (1)

Spektre
Spektre

Reputation: 51845

weird that you can not apply more then one transform ... maybe you just use wrong function but anyway:

If you have direct access to transform matrix (or by get,set)

  1. google for transform matrices if you do not have the knowledge
  2. generate first rotation matrix and store it to M1
    • can use the setRotation for that
  3. generate second rotation matrix and store it to M2
  4. multiply them M=M1*M2
  5. use this M instead of setRotation

If yo do not have the direct access to transform matrix and have to use just the setRotation

  • in that case you have to use quaternion which is the 4D vector you call the setRotation with
  • google for quaternion math and find the application of 2 rotations
  • I do not use them so I can not help with that but there are also equations out there
  • which converts 3x3 rotation matrix into quaternion and back
  • so you can still use the algorithm above
  • obtain M
  • extract the rotation matrix from it (it is just sub matrix you omit last row and column)
  • compute quaternion from it
  • and call setRotation with the result

Upvotes: 1

Related Questions