Skip to main content
gamedev.net
🔒 Locked

Generating a Camera Matrix???

Started by laeuchli 6 replies 1.1k views
Original Post
laeuchli
laeuchli
Hey all, I''ve been trying to figure out how to generate a Camera Matrix. I''ve read about rotation matrixes, translation matrixes, etc, but I can''t figure out how to combine it all into a Camera Matrix. I''ve tried doing a google search, but I can''t locate anything :-(. Could someone please help? Thanks, Jesse
Ozz
Ozz
www.iol.ie/~hennessye

Eoin.
Moe
Moe
D3DX has some nice functions for this. You can probably dig through the source for them (at least I think you can) and take a look...

Moe''s Site
Gammastrahler
Gammastrahler
you have a view matrix that holds four vectors,

- three unit vectors to specify each axis of the camera´s orientation (forward, side and up)

- the fourth vector specifiys the camera´s world position.

a modelview matrix could then look like this:

| 1, 0, 0, 0 | side(x) vector
| 0, 1, 0, 0 | up(y) vector
| 0, 0, 1, 0 | foward(z) vector
| 0, 0, 0, 1 | x, y, and z of camera´s position

this is actually the identity matrix, which specifys that there is no rotation and no translation, so the camera is at the origin (0, 0, 0)

also remember that z is negative moving into the screen, so to face your camera in forward direction, set the z vector to

|0, 0, -1|
laeuchli
laeuchli
Hey all,
Thanks for the help so far.
I went to the site MrBurns gave, and it looks good. I''ve found the section I need, the Relative Motion section.
However, when I do add the Relative Motion code, my roation stops working right. When I take it out the rotation works right, but my motion doesn''t :-(. Below is my code:
rotationmatrix(xa,ya,0,rotationm);
translationm[0][3]=rotationm[2][0]*forward;
translationm[1][3]=rotationm[2][1]*forward;
translationm[2][3]=rotationm[2][2]*forward;
multiplymatrixes(rotationm,translationm,rtmatrix);
is this not the right way?
Thanks,
Jesse
Mr_Burns
Mr_Burns
>> Below is my code:
>> rotationmatrix(xa,ya,0,rotationm);
>> translationm[0][3]=rotationm[2][0]*forward; // I think these 3 lines are wrong
>> translationm[1][3]=rotationm[2][1]*forward;
>> translationm[2][3]=rotationm[2][2]*forward;
>> multiplymatrixes(rotationm,translationm,rtmatrix);

What you want to do is build the rotation matrix first, then the translation matrix *separately*, then multiply them. Assuming your rotation matrix is right, I think you are building the translation matrix wrong. Here''s how I would build it:

Given a vector that is the camera *position*, this is what the translation matrix would look like:
| 1 0 0 -X |
| 0 1 0 -Y |
| 0 0 1 -Z |
| 0 0 0 1 |

Note the negative signs. Once this is built, you can just multiply the 2 matrices.

Of course you can optimize the matrix multiplication, since the first 3 columns of the translation matrix are an Idenity matrix.

If you read that article carefully it should explain everything, atleast it did for me.

Hope this helps
Mr_Burns
Mr_Burns
I want to add that when you multiply then, you have to put the rotation matrix first: rotation*translation, not translation*rotation. This is because you have to rotate *before* translating.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.