# getting the direction vector from a matrix

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

## Recommended Posts

Hey, Is it possible to get the direction vector from a Matrix. I have a 4x4 matrix and once i rotate an object I want to move the object in the direction its facing. Thanks

##### Share on other sites
The way that is sure to work is to take a vector that points in the forward direction of the model and then multiply it by the model matrix. The forward vector is probably something simple like (1,0,0,0), which means that an easy optimization in this example is to just take the first row or column of the matrix, depending on if you're using row or column vectors.

##### Share on other sites
Found two previous threads here on GameDev that might help:

##### Share on other sites
Hi,

I tried what you said but I am still facing the same problems. I have the following code :-

Matrix mat;mat.Identity();mat.Translate(Vector3D(0, 0, -1);Matrix m = m_Wheel[0].GetMatrix();	m.Multiply(mat);	pos = m.GetPosition();m_Wheel[0].SetPosition(pos);

However this seems to not work at all. Is there something wrong with the way its being done ?

Thanks

[Edited by - CRACK123 on May 22, 2007 5:26:17 PM]

##### Share on other sites
1. As described in the links Omega147 posted, depending on how your matrix is stored (and what spaces it transforms between) extract the first 3 values of the 3rd row OR 3rd column from the matrix as a vector.

11 21 31 4112 22 32 4213 23 33 4314 24 34 44

V = 13,23,33
OR
V = 31,32,33

V is a vector that points either positively or negatively along the Z axis after rotation for a coordinate system where Y is up and X is sideways. If your coordinate system uses Y (or X) as forwards or backwards, extract that axis from the matrix instead of the Z axis.

2. If your matrix has any scaling in it, normalize V to remove the scaling so that the length of V is 1.

3. Multiply V by the distance you want to move in the 'forward' direction.

4. Add V to the position of the object and it should move forwards; if it moves backwards (it depends on which coordinate system you're using), add -V (or subtract V).

Matrix m = m_Wheel[0].GetMatrix();TV3D v; v.x = m[0][2]; // or the other way around or using a different axisv.y = m[1][2]; // if your coordinate system is differentv.z = m[2][2];//v.Normalize(v);  <- only necessary if matrix isn't orthonormalTV3D pos = m.GetPosition();pos += v * distance;m_Wheel[0].SetPosition(pos);

'Orthogonal', 'orthonormal', and 'basis' are good words to learn about when you need to understand what a matrix actually is [smile].

HTH

##### Share on other sites
Thanks for the help guys.

I got it fixed. I see what I was doing wrong and I have used S1CA's method and eventually got it working.

Thanks

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631787
• Total Posts
3002351
×