CRACK123 235 Report post Posted May 22, 2007 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 0 Share this post Link to post Share on other sites
Vorpy 869 Report post Posted May 22, 2007 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. 0 Share this post Link to post Share on other sites
Omega147 536 Report post Posted May 22, 2007 Found two previous threads here on GameDev that might help:Thread 1Thread 2 0 Share this post Link to post Share on other sites
CRACK123 235 Report post Posted May 22, 2007 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] 0 Share this post Link to post Share on other sites
S1CA 1418 Report post Posted May 22, 2007 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 4112 22 32 4213 23 33 4314 24 34 44V = 13,23,33 OR V = 31,32,33V 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 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 0 Share this post Link to post Share on other sites
CRACK123 235 Report post Posted May 22, 2007 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 0 Share this post Link to post Share on other sites