Advertisement

Getting direction vector from quaternion

Started by March 25, 2002 10:57 AM
2 comments, last by Portable591 22 years, 10 months ago
I''m still trying to get a better grip on quaternions, so I''ve got a question. I have a ship in space or something. The ship''s orienatation is kept in a quaternion used to point it in the correct direction. To rotate the ship, I convert some temporary euler angles to a quaternion and multiply it. To rotate the ship before drawing it, I get the angle-axis form of the rotation from the quaterion and use OpenGL''s glRotatef function. All of this works great, but now I need some way to move the ship in the direction that it appears to be facing. I made a function to get the rotation matrix from the quaternion, and I multiplied the matrix by a vector (in this case it was <0, 0, 5>) to get a displacement for the position of the ship. This doesn''t seem to work for me. Once the ship starts rotating a bit, the movement of the ship doesn''t match the visible heading of said ship. I do not know if my logic is flawed here, or if I just need to add something small to my code to get it to work. I''m willing to provide any information that is needed to help someone resolve this problem I am having. Thanks to anyone that can help me out.
> I made a function to get the rotation matrix from the
> quaternion, and I multiplied the matrix by a vector (in this
> case it was <0, 0, 5>) to get a displacement for the position
> of the ship. This doesn''t seem to work for me.

This should work. You can also apply the quaternion to the vector directly: most quaternion APIs supply such a function.

One probme might be there are two conventions for applying quaternions to a vector

v'' = qvq^-1

and

v'' = q^-1vq

Which one you use determines how any function that relates quaternions to rotations work. If any function is the wrong way round (compared ot other functions) you need to invert/conjugate the quaternions before applying them to the function, so you might try this for your function. Apart from this it''s difficult to say without more info.
John BlackburneProgrammer, The Pitbull Syndicate
Advertisement
quote:
Original post by Portable591
I multiplied the matrix by a vector (in this case it was <0, 0, 5>) to get a displacement for the position of the ship.



I may be misinterpreting you, but it looks like you''re saying that you''re doing this to effect a translation. If so it''s wrong. You add a vector to perform a translation or, if you''re using 4x4 matrices, you can multiply by a translation matrix. Or you can just use glTranslatef (or whatever it is) to translate in the ship''s forward direction.

Yes, the goal of this is to move the ship forward in the direction that it is pointing. I had thought about using glTranslatef to move the ship to it''s correct position on the screen, but I quickly found that I need to know it''s world position for collision detection.

This topic is closed to new replies.

Advertisement