• Create Account

## Extract direction verctor from matrix

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Ed Welch  Members

Posted 07 January 2013 - 12:49 PM

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, 07 January 2013 - 12:50 PM.

### #2Álvaro  Members

Posted 07 January 2013 - 12:51 PM

That doesn't make sense. Can you give us an example?

### #3C0lumbo  Members

Posted 07 January 2013 - 12:55 PM

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.

### #4Burnt_Fyr  Members

Posted 07 January 2013 - 01:43 PM

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, 07 January 2013 - 01:47 PM.

### #5Ed Welch  Members

Posted 08 January 2013 - 04:44 AM

### #6JohnnyCode  Members

Posted 08 January 2013 - 02:16 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.