5
votes

I just came to strange problem with my project in 3D. Everyone knows algorythm of calculating LookAt vector, but it is not so easly to calculate "up" vector from transformation matrix (or at least maybe I simple missed something).

The problem is following:

"Up" vector is (0, 1, 0) for identity rotation matrix and rotate with matrix, but do not scale nor translate. If you have simple rotation matrix procedure is easy (multiply vector and matrix). BUT if matrix contains also translation and rotation (e.g. it was produced by multiplying several other matrices), this won't work, as vector would be translated and scaled.

My question is how to get this "up" vector from single transformation matrix, presuming vector (0, 1, 0) correspond to identity rotation matrix.

5
There is no such thing as an "identity rotation matrix." By definition, an identity matrix leaves a vector untraformed within a particular basis.Janie
I don't understand your question. Do you want the world's "up" or the local "up"? Can you walk through what you're doing with some example matrices?Nosredna
matrix Y-axis vector (your up) in world coordinates is obtained by the way you describe or by direct extraction from matrix see stackoverflow.com/a/25216549/2521214 if it is not what you looking for after applying more matrices then you do not want the up-vector but something else instead. in that case specify closer and add imagesSpektre

5 Answers

9
votes

Translation actually does affect it. Let's say in the example the transformation matrix didn't do any scaling or rotation, but did translate it 2 units in the Z direction. Then when you transform (0,1,0) you get (0,1,2), and then normalizing it gives (0,1/sqrt(5), 2/sqrt(5)).

What you want to do is take the difference between the transformation of (0,1,0) and the transformation of (0,0,0), and then normalize the resulting vector. In the above example you would take (0,1,2) minus (0,0,2) (0,0,2 being the transformation of the zero vector) to get (0,1,0) as desired.

5
votes

Apply your matrix to both endpoints of the up vector -- (0, 0, 0) and (0, 1, 0). Calculate the vector between those two points, and then scale it to get a unit vector. That should take care of the translation concern.

2
votes

Simply multiply the up vector (0,1,0) with the transformation, and normalize. You'll get the new calculated up vector that way.

0
votes

I'm no expert at matrix calculations, but it strikes me as a simple matter of calculating the up vector for the multiplied matrix and normalizing the resulting vector to a unit vector. Translation shouldn't affect it at all, and scaling is easily defeated by normalizing.

0
votes

I am aware this is an OLD thread, but felt it was necessary to point this out to anyone else stumbling upon this question.

In linear Algebra, we are taught to look at a Matrix as a collection of Basis Vectors, Each representing a direction in space available to describe a relative position from the origin.

The basis vectors of any matrix (the vectors that describe the cardinal directions) can be directly read from the associated matrix column. Simply put your first column is your "x++" vector, your second is the "y++" vector, the third is the "z++" vector. If you are working with 4x4 Matrices in 3d, the last elements of these columns and the last column are relating to translation of the origin. In this case, the last element of each of these vectors and the last column of any such matrix can be ignored for the sake of simplicity.

Example: Let us consider a matrix representing a 90 degree rotation about the y axis.
[0, 0, -1]
[0, 1, 0]
[1, 0, 0]

The up vector can be plainly extracted from the third column as (-1, 0, 0), because the matrix is applying a 90 degree rotation about the y axis the up vector now points down the x axis (as the vector says), You can acquire the basis vectors to acquire the positive cardinal directions, and negating them will give you their opposite counterparts.

Once you have a matrix from which to extract the directions, no non-trivial calculations are necessary.