#### Archived

This topic is now archived and is closed to further replies.

# Help with Matrices Please

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

## Recommended Posts

Here''s my situation: I have several different guns sitting in the lower left corner of the screen. When the user selects one, I have the gun tween to a spot just in front of the camera, so it appears that the user is ''sighting in'' the gun. To do this, I have stored an ''sight matrix'' in the gun class--basically, an offset position/rotation from the view matrix (position/rotation of the camera) that the gun will move to when it is sighted in. Basically, the gun becomes a ''child'' of the camera when it''s being sighted in. I''ve run into a snag, however. When I build the view matrix at program initialization, I give the camera a position and rotation. I also give a position and rotation for the sight matrix for each gun. When I start tweening the gun toward the camera, I multiply the view matrix with the gun''s sight matrix to obtain the target position/rotation for the gun. If the gun''s sight matrix has no rotation in it, it sights in just fine. However, if it has any rotation in it, the gun ends up way off target. I think this happens because in both matrices, I already have rotation/translation put into them, then I multiply them together. When I''m doing matrix math, aren''t I supposed to do scaling/rotation before I do translation? I think this means that I will need to extract the individual rotations and translations, then do both rotations, then do both translations. Would this work? Could someone give me a hint on how to extract the current position/rotation from the view matrix? I can''t help but think that there''s GOT to be a better way to do this. Any suggestions? Thanks in advance!

##### Share on other sites
DOH!!

I figured out what I did wrong. When I multiplied the two matrices together, I multiplied them in the wrong order (forgot that with matrices, A x B doesn''t necessarily equal B x A).

D3DXMatrixMultiply (&TargetGunMatrix, &ViewMatrix, &SightOffsetMatrix);

I switched it to:

D3DXMatrixMultiply (&TargetGunMatrix, &SightOffsetMatrix, &ViewMatrix);

And it worked perfectly. Question: Why does it matter so much the order in which you multiply matrices? I want to understand this so I can avoid this pitfall in the future.

##### Share on other sites
That''s just the way the math works out. But think of it this way...

You know that a matrix can rotate (call this the R matrix)
You know that a matrix can translate (call this the T matrix)

Now you''re standing in a hallway. Think to yourself: Is it different if you turn left (rotate) and walk 10 feet, or walk 10 feet and then turn left. The obvious answer is "yes" therefore

Alternitively sit down and make two 3x3 matrices and go through the math. You should see fairly quickly it doesn''t work out...

The first element of (A*B) = a11*b11 + a12*b21 + a13*b31
However first element of (B*A) = a11*b11 + a21*b12 + a31*b13

Don''t know how else to explain it.

##### Share on other sites
Yes, that makes sense--your hallway example is pretty clear. I guess I''ve got to sit down and do the matrix math by hand to get my head around what the proper multiplication order should be.

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633345
• Total Posts
3011441
• ### Who's Online (See full list)

There are no registered users currently online

×