Sign in to follow this  

getting the direction vector from a matrix

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

So for your 4x4 matrix:
11 21 31 41
12 22 32 42
13 23 33 43
14 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).


Summary using your coding style:
Matrix m = m_Wheel[0].GetMatrix();

TV3D v;
v.x = m[0][2]; // or the other way around or using a different axis
v.y = m[1][2]; // if your coordinate system is different
v.z = m[2][2];

//v.Normalize(v); <- only necessary if matrix isn't orthonormal

TV3D 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 this post


Link to post
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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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