# Up/Right/Look

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

## Recommended Posts

Hi guys, I'm trying to extract the 3 Up, Right, LookAt vectors from a D3DXMATRIXA16 rotation matrix. I do the following: eyeX = mat._11; eyeY = mat._12; eyeZ = mat._13; upX = mat._21; upY = mat._22; upZ = mat._23; atX = mat._31; atY = mat._32; atZ = mat._33; Then im fiddling with the vectors and placing them back into the matrix but I'm getting some crazy results! So my question is, is it the EYE/UP/LOOKAT vectors that are in the matrix and am i extracting the elements correctly (such as eye vector is in _11, _12, _13, or is it _11, _21, _31?)? Thanks, Meshy

##### Share on other sites
Quote:
 Original post by MeshManThen im fiddling with the vectors and placing them back into the matrix but I'm getting some crazy results!

Not too surprising [smile]

A matrix isn't just a storage for the raw values, it is itself a composition of various equations. It's often difficult, if not impossible, to "decode" a given 4x4 back down to the original parameters.

Quote:
 Original post by MeshManSo my question is, is it the EYE/UP/LOOKAT vectors that are in the matrix and am i extracting the elements correctly (such as eye vector is in _11, _12, _13, or is it _11, _21, _31?)?

If you're specifically messing with them on a regular basis, why not reconstruct the matrix. In truth, unless you're doing ridiculously complex view-transform operations you're unlikely to hit any performance problems by just calling another D3DXMatrixLookAtLH() / D3DXMatrixLookAtRH()...

hth
Jack

##### Share on other sites
Hey there Jack,

I've always had a fear of matrices because everytime I try to fiddle with them, I cannot get the kind of results I want (camera and object transformations oh my!).

I'm trying to get the 'Direction of Facing' of an object and apply it onto another object.
So what I'm trying to do is grab the positive Z axis from the matrix (this is usually referred to as the look-at vector) and stuff it into another matrix making sure I rebuild the +X (right) axis in the matrix by doing a cross-product with the Z-Y axies.

##### Share on other sites
It's not difficult to decompose the matrix into vectors. Here's an example of calculating a matrix based on the 'lookat' of a character. Assuming the character is unable to 'roll' so his right axis is always perpendicular to world up and such.

D3DXMATRIX mat;
D3DXVECTOR3 worldUpVec(0, 1, 0);
D3DXVECTOR3 xAxis;
D3DXVECTOR3 yAxis;
D3DXVECTOR3 zAxis = lookAtVector;

D3DXVec3Normalize( &zAxis, &zAxis );
D3DXVec3Cross( &xAxis, &worldUpVec, &zAxis );
D3DXVec3Normalize( &xAxis, &xAxis );

D3DXVec3Cross( &yAxis, &zAxis, &xAxis );
D3DXVec3Normalize( &yAxis, &yAxis);

mat._11 = xAxis.x;
mat._12 = xAxis.y;
mat._13 = xAxis.z;
mat._14 = 0;

mat._21 = yAxis.x;
mat._22 = yAxis.y;
mat._23 = yAxis.z;
mat._24 = 0;

mat._31 = zAxis.x;
mat._32 = zAxis.y;
mat._33 = zAxis.z;
mat._34 = 0;

mat._41 = position.x;
mat._42 = position.y;
mat._43 = position.z;
mat._44 = 1;

It's a simple example, and probably isn't correct if there should be scale taken into account, not sure.

##### Share on other sites
Right, I understand all that.

Why doesn't anyone implement any camera classes or physics movement with Z-Axis roll taken into account? Is it hard?

##### Share on other sites
Most games don't allow camera roll, and it gets more complex if you don't have a known axis to use in the computations(in this case world up)

##### Share on other sites
Ah I see.
I'm hoping to just pull the inverse positive Z-Axis vector from my camera matrix, send it to the other players (in my multiplayer FPS) which they can apply to my model as they see me.

I'm also going to want to do camera shake where I roll my camera on the Z like Quake3 did when you walk. That shouldn't affect the look-at vector at all though should it?

##### Share on other sites
Quote:
 Original post by MeshManRight, I understand all that.Why doesn't anyone implement any camera classes or physics movement with Z-Axis roll taken into account? Is it hard?

Take a look at D3DSmartCamera.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015827
×