# vector from quaternion?

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a quaternion class that works (finally) and rotates my object with ease. Now, i want to be able to get a vector from my quaternion, finding out what direction it is pointing... i want to do this to be able to move the object in the direction it is pointing (for a space sim). i also want to use this so i can have a lookat vector so that i can point my camera in the direction the ship is pointing. Any ideas? Cheers

##### Share on other sites
For this to work your object just needs a front facing vector (a vector pointing in the direction your object is facing when it is not rotated). Then rotate this vector using the quaternion as you would normally rotate a vector (for example by using the rotation matrix from the quaternion)...

Tom

##### Share on other sites
ok i created a create4x4Matrix, mult4x4Matrix and disp4x4Matrix method. I create a (0,0,4) vector and feeds it into the matrix, then multiply that one agains the current quaternion matrix, but i get goofy results... im missing something, i know it. i just can't put my finger on it.

##### Share on other sites
I think i found it out... im just multiplying the end result quaternion matrix to the vector matrix... is there something else i should be doing to use the quaternion matrix to actually rotate the vector?

cheers

##### Share on other sites
Quote:
 Original post by Pirosanok i created a create4x4Matrix, mult4x4Matrix and disp4x4Matrix method. I create a (0,0,4) vector and feeds it into the matrix, then multiply that one agains the current quaternion matrix, but i get goofy results... im missing something, i know it. i just can't put my finger on it.

I'm sorry but I don't understand. What do you mean with 'feeding a vector to a matrix'?
You should just get the matrix and multiply it with the vector (using matrix vector multiplication. All you need is a createMatrixFromQaternion function and a multplyVectorByMatrix function.

NB:
Technically, because it's a vector not a vertex, you should multiply it with the transposed of the inverse of the matrix, but as long as the 4x4 matrix contains only a rotation and no other transformations that shouldn't be necessary.

Tom

EDIT: ah, now I understand, you put the vector in a matrix before multiplication. Why are you not using matrix/vector multiplication?

##### Share on other sites
Appologies, its 3 am here :)

How would i do matrix/vector multiplication? Is it just as easy as:

[0,0,0,0]---[x]
[0,0,0,0]---[y]
[0,0,0,0]-*-[z] ?
[0,0,0,0]---[1]

and just to make sure this is clear, this quaternion matrix is the very same matrix that i send to the glMultMatrix() method to rotate the object.

##### Share on other sites
Quote:
 Original post by PirosanAppologies, its 3 am here :)How would i do matrix/vector multiplication? Is it just as easy as:[0,0,0,0]---[x][0,0,0,0]---[y][0,0,0,0]-*-[z] ?[0,0,0,0]---[1]and just to make sure this is clear, this quaternion matrix is the very same matrix that i send to the glMultMatrix() method to rotate the object.

This is the matrix/vector multiplication from my library:
/** * matrix-vector multiplication (apply matrix to vector): matrix = matrix * vector **/inline avector operator*(const avector& v){	if (identity)		return v;	avector r; 	r.x = m[0] * v.x + m[4] * v.y + m[8]  * v.z + m[12] * v.w; 	r.y = m[1] * v.x + m[5] * v.y + m[9]  * v.z + m[13] * v.w; 	r.z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14] * v.w; 	r.w = m[3] * v.x + m[7] * v.y + m[11] * v.z + m[15] * v.w; 	return r; }

Note that I use OpenGL style matrices (where m[0], m[1], m[2] and m[3] are the first column).

And yes, the matrix should be the same. I'll just post my function for that as well for completness (it's a function of the same matrix class):
/** * make rotation matrix (out of a quaternion) * * @param q quaternion **/inline void make_rotation(const aquaternion& q){ 	float xx = q.x * q.x; 	float yy = q.y * q.y; 	float zz = q.z * q.z; 	float xy = q.x * q.y; 	float xz = q.x * q.z; 	float yz = q.y * q.z; 	float sx = q.s * q.x; 	float sy = q.s * q.y; 	float sz = q.s * q.z; 	m[0]  = 1.0f - 2.0f * (yy + zz); 	m[1]  = 2.0f * (xy + sz); 	m[2]  = 2.0f * (xz - sy); 	m[3]  = 0.0f; 	m[4]  = 2.0f * (xy - sz); 	m[5]  = 1.0f - 2.0f * (xx + zz); 	m[6]  = 2.0f * (yz + sx); 	m[7]  = 0.0f; 	m[8]  = 2.0f * (xz + sy); 	m[9]  = 2.0f * (yz - sx); 	m[10] = 1.0f - 2.0f * (xx + yy); 	m[11] = 0.0f; 	m[12] = 0.0f; 	m[13] = 0.0f; 	m[14] = 0.0f; 	m[15] = 1.0f; 	identity = false;};

Tom

##### Share on other sites
thank you very much! that code worked beautifully!

cheers,

chris

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009849
• ### Who's Online (See full list)

There are no registered users currently online

×