• Advertisement
Sign in to follow this  

Extract direction verctor from matrix

This topic is 1837 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

Does anyone know how to extract a direction vector from a 4x4 matrix? (by direction vector, I mean (0,0,1) would point down) Edited by Ed Welch

Share this post


Link to post
Share on other sites
Advertisement

Assuming your 4x4 matrix is defined as something like m_fElements[4][4], and it's an orientation matrix (or orientation matrix with translation) the forward vector can be extracted as either:

 

Vec3 vForward(m_fElements[2][0], m_fElements[2][1], m_fElements[2][2]);

 

or

 

Vec3 vForward(m_fElements[0][2], m_fElements[1][2], m_fElements[2][2]);

 

Depending on the implementation of your maths lib.

Share this post


Link to post
Share on other sites

To add to, and clear up some of what columbo said, a 4x4 matrix can represent a set of orthonormal basis and a position relative to some other coordinate system. Depending on matrix layout, as in Row/Column majorness, Row/Column vectors, as well as handedness or the coordinate frame this can be setup in a few different ways. In Row Major, left handed system( that which directx uses by default) these are laid out like this:

[ x.x, x.y, x.z, 0] // X axis vector
[ y.x, y.y, y.z, 0] // Y axis
[ z.x, z.y, z.z, 0] // Z axis
[ P.x, P.y, P.z, 1] // Point in parent frame to translate local origin to.

I tend to call these the Right, Up, and Forward vectors. The x axis points to the right of your object, the Y axis points up out of your object, and the Z axis points in front of the object. So as Columbo said, your desired vector can be pulled from one of the rows of the matrix. Whether or not it is the one you define as up is up to you(pun intended) In reality, Up down left right front back, are all terms we use to describe things relative to our coordinate frame.

 

If this is puzzling to you, realize that the difference between row or column vectors has to deal with how we manipulate the vectors(it affects matrix multiplication)the majorness of a matrix has to deal with how it is indexed in memory(does the first index point to a row, or a column) and that the handedness really has no effect on the math until it comes to projecting onto a 2d frame.

 

 

http://fgiesen.wordpress.com/2012/02/12/row-major-vs-column-major-row-vectors-vs-column-vectors/

 

This article should help clear out some confusion that I may have induced.

Edited by Burnt_Fyr

Share this post


Link to post
Share on other sites

transform a vector of your choice (x,y,z,0) by matrix, this will leave out translation part - the addition to the vector. This will only rotate (and scale too if matrix is not uniform). Pick the vector so that it aligns with the matrix rotation basement, like for example (1,0,0,0) ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement