Sign in to follow this  
Ankles

[Resolved] Getting forward/up out of a quaternion.

Recommended Posts

Ankles    122
This is confusing even me - sorry in advance. A quaternion represents a vector and a rotation around that right? If you have a quaternion to represent an entity do you only need 1 or 3? With my current straight vector setup I have up, forwards and right vectors. If I was to change this over to quaternions for easier rotation does that mean I only need 1? If the vector component points "forwards", how then do I get an up and a sideways vector for jumping, strafing and turning? Or do you still have all 3 quaternions, but its now just easier to rotate them around each other? I think you should be able todo it all with just 1 quaterion but i can't see how. [Edited by - Ankles on May 21, 2006 6:50:08 AM]

Share this post


Link to post
Share on other sites
jyk    2094
- You only need one quaternion to represent your object's orientation.

- Although it's a common intuitive mistake, the 'axis' part of a rotation is not, in general, the same as the forward direction vector corresponding to that rotation. Usually none of the direction vectors is parallel to the axis.

- To get the direction vectors, just look up quaternion to matrix conversion; the direction vectors are the rows or columns of the matrix that is derived from the quaternion. The only gotcha is that if you use the wrong convention (row vectors instead of column vectors or vice versa), your rotations will be in the 'wrong' direction. Often this can simply go unnoticed without any ill effects, but it's still an important thing to get right.

- A quaternion is not always the best representation for an object's orientation. For example, if the object is a humanoid character, a player in an FPS game, a vehicle, or something similar (i.e. not 6DOF), then using a quat is largely a waste of time and just adds an unnecessary and potentially problematic intermediate step.

Share this post


Link to post
Share on other sites
Ankles    122
Quote:
Original post by jyk
- To get the direction vectors, just look up quaternion to matrix conversion; the direction vectors are the rows or columns of the matrix that is derived from the quaternion.

- A quaternion is not always the best representation for an object's orientation.


Currently I'm looking at developing a little space sim kinda game rather than a FPS so I'm hoping Quats can help me out. I've done a few 1st/3rd person games and my code for that works great, but when y isn't always up my current stuff fails. Hence I'm now looking to quaternions. Also I like learning new stuff so it'll be fun anyway :D


I have code in my Quaternion class to convet it backto a matrix. If I do that then my up, right and forward vectors are just the colums (or rows) of that matrix? thats awsome if it works. Any way to know weather I want the columns or rows? I'll trial and error it for now, see what I get - ty for the info!

/me goes off to try it

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Currently I'm looking at developing a little space sim kinda game rather than a FPS so I'm hoping Quats can help me out. I've done a few 1st/3rd person games and my code for that works great, but when y isn't always up my current stuff fails. Hence I'm now looking to quaternions. Also I like learning new stuff so it'll be fun anyway :D
Indeed :) And yeah, quats are not a bad choice for a space game.
Quote:
I have code in my Quaternion class to convet it backto a matrix. If I do that then my up, right and forward vectors are just the colums (or rows) of that matrix? thats awsome if it works. Any way to know weather I want the columns or rows?
You should be able to find out whether your math library uses row or column vectors easily enough. As for the quat-to-matrix function, look at elements (0,1) and (1,0). One of the expressions will have a +, and the other a -. If (0,1) is the '+', the code was written for row vectors; if (1,0) is the '+', it was written for column vectors.

If the quat-to-matrix function doesn't match the convention of the library (which is possible), transpose it so that it does match; then you should be good to go.

Share this post


Link to post
Share on other sites
DrEvil    1148
You could also transform a vector by the units quat. Like a unit vector that represents forward in object space(-z or whatever). If you multiply that with the quat that should give you your objects forward. May be cheaper than building a full matrix from the quat if you only need 1-2 vectors from it.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by DrEvil
You could also transform a vector by the units quat. Like a unit vector that represents forward in object space(-z or whatever). If you multiply that with the quat that should give you your objects forward. May be cheaper than building a full matrix from the quat if you only need 1-2 vectors from it.
Although you could do this, I would say there's no point, because:

- Transforming a single vector by a quat directly isn't much faster than converting the quat to a matrix (it might even be slower - I can't remember the exact op count).

- If you need 2 of the direction vectors, just constructing the matrix directly will definitely be faster.

- If you only want a particular direction vector, you can write a function to extract single direction vectors directly from a quat, which will be faster than rotating a cardinal vector by the quat.

- It's usually necessary to have a matrix version of your orientation available, so it's a good idea to convert it to a matrix whenever it's updated anyway.

- If we're talking about setting up the transformation or getting the direction vectors for an object or camera here (something that will usually be done only once per update) then little performance quibbles like how exactly you extract the direction vectors are of practically no consequence.

- Transforming a cardinal axis with a quat is always a waste of ops due to the 1's and 0's involved; if you're really concerned about squeazing out every possible operation, you won't do it that way.

[Edit: typo]

[Edited by - jyk on May 22, 2006 12:38:43 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this