# Axis angle to Vector

## Recommended Posts

Nanook    537
I got an angle axis from a quoternion for an object in my game and need to find the direction vector for where the object is facing.. how can I find this?

##### Share on other sites
Buckeye    10747
A quaternion represents a rotation about an axis angle, not an absolute direction. So, you can't find the direction the object is currently facing unless you know the original direction it faced.

If you know the original direction:
- create a 3x3 rotation matrix from the quaternion
- multiply the original direction of the object by the matrix

##### Share on other sites
alvaro    21246
Why would you go through the 3x3 matrix? Just take the original direction and rotate it using the quaternion normally (i.e., by conjugation).

##### Share on other sites
Nanook    537
Im still not sure how to do this? When my game starts the original orientation would be the same as the current orientation wouldn't it?

##### Share on other sites
Buckeye    10747
Quote:
 When my game starts the original orientation would be the same as the current orientation wouldn't it?

Yes, before any changes are made. At that point, you should know which direction your object is facing. That's the direction you would use for the calc later.

Perhaps if you're a bit more specific about what language you're programming in, what the "object" is, etc., you can get more specific help.

##### Share on other sites
Nanook    537
My problem is that I need to add an acceleration force to my spaceship so I need to know where its facing.. so I need to get the orientation vector so I can multiply that with my acceleration force.. Im using c++

I just started using quaternions so I need a good explenation..

I guess the original orientation is 0,0,-1 as I'm using opengl.. I've set it up so everytime a new orientation quaternion is set I multiply it by the previous orientation vector, but this doesn't seem to give me the right vector..

orientationDir is the vector (set to 0,0,-1 at startup) and orientation is the quaternion:

orientationDir = orientation * orientationDir;
orientationDir.normalize();

##### Share on other sites
alvaro    21246
I guess you just don't know how to apply to a vector a rotation specified by certain quaternion. You should probably read and understand the Wikipedia page about quaternions and rotation. Basicaly you need to multiply
rotated_vector = quaternion * original_vector * inverse(quaternion)

This operation is called "conjugation". Since the quaternion in question is a unit quaternion, you can compute its inverse just by flipping the sign of the three imaginary coefficients.

##### Share on other sites
Nanook    537
Yeah I know I'm running out of time for this project right now though, but I will use time during my break to read on quaternions.. We're abit ahead with quaternions, we didn't realy need to use them.. so I just need to get this working now..

but does it matter what order I multiply them? Is this supposed to work?

orientationDir = orientation * orientation.inverse() * orientationDir;

##### Share on other sites
Brother Bob    10344
Yes, order matters. Multiplication of quaternions is not commutative.

##### Share on other sites
Buckeye    10747
Quote:
 everytime a new orientation quaternion is set I multiply it by the previous orientation vector

Is the "new" orientation quat the total orientation of the object, or a change in orientation?

If it's the total orientation, then don't multiply it by the previous orientation.

##### Share on other sites
Nanook    537
but in alvaro's example rotated_vector is the new direction?

##### Share on other sites
Nanook    537
I got a function to do quaternion * Vector3 , but how do I do Vector3 * quaternion? I've tried to look around for a formula, but cant find one?

##### Share on other sites
alvaro    21246
Quote:
 Original post by NanookI got a function to do quaternion * Vector3 , but how do I do Vector3 * quaternion? I've tried to look around for a formula, but cant find one?

In the formula I posted, both multiplications are simply quaternion multiplications. The Vector3 in the middle is to be interpreted as a quaternion with 0 real part, x*i+y*j+z*k.

##### Share on other sites
lightbringer    1070
I think what alvaro wrote might be a bit cryptic - what it means is that you pretend the vector is a quaternion - make a new quaternion, set x,y,z directly from the vector and set w=0. Then you can multiply as normal.

##### Share on other sites
Nanook    537
but that would return a quaternion though? And I need the direction vector..

##### Share on other sites
jyk    2094
Quote:
 but that would return a quaternion though? And I need the direction vector..
It does return a quaternion, but you can convert this quaternion back to vector form by dropping the 'w' component.

You mentioned though that you have a 'quaternion * vector3' function available. I'm guessing that this function in fact rotates the input vector using the specified quaternion. If so, it's a clear case of operator overloading abuse (IMO), but is nevertheless probably the easiest way to perform the rotation that you need. Or, you could do it 'manually' as described previously.

To reiterate, all you need to do to find the current direction vector for your object is to transform the local-space forward direction vector using the object's transform. If this isn't working for you, perhaps you could post the relevant code and describe exactly how it's not working.

##### Share on other sites
Nanook    537
I got it working now.. I found out my quaternion class was not working properly.. been having trouble with this for a long time.. as quaternions are a bit ahead of what we've learned so far we downloaded a quatarnion class from the net so I thought it was me doing something wrong, but it was actualy the class :p