# Extract direction verctor from matrix

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

## 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 on other sites
That doesn't make sense. Can you give us an example?

##### Share on other sites

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