🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

3D Camera Algorithms

Started by
2 comments, last by Omalacon 24 years, 5 months ago
I am currently working on a camera for my 3D engine in Direct3D. I can move it no problem, so long as the camera remains withing a 2d plain, but the second I adjust the pitch and try to move things get weird. Here is the order of my alogrithm to move the camera in the XZ plane. 1.) Create a unit vector at (0, 0, -1) (x, y, z) 2.) Rotate that vector according to the Yaw Pitch and Roll of my camera using a 3x3 rotation matrix(note, roll and pitch take no part since it is just in the XZ plane and yaw is the only factor, but I included them in the matrix assuming I would be able to use them if I allowed the camera''s roll and pitch to be adjusted) 3.) Flip the x component of the vector since the Z axis is flipped (I used to have troubles with this until I figured out why my x was negative to what it should have been) 4.)Scale the vector to the distance to be traveled 5.)Add each component of the vector (x y z) to my camera''s origin. Then when I render... 1.) Create a tranlation matrix multiply it by the camera''s rotation matrix, every thing shows up fine But things don''t work quite right when I do adjust the pitch and roll, it moves in directions it shouldn''t, backwards, sideways, wierd diagonals. Any idea how to fix this, or know any good sites on camera rotation and movement alogrithms? -Omalacon
Advertisement
There is a snippet of code by Michael Abrash (id) kicking around somewhere which demonstrates 3D-clipping. It was originally published in Dr. Dobbs Journal I think - this includes a function called UpdateViewPos() which contains some vectors for movement which change with pitch, yaw etc..

I think this might help you, Paulcoz.
As usual, with a little tinkering I found the solution to my problem on my own, just to give a few lessons learned...

1. When I rotate the vector to move the camera, the order of rotations doesn''t matter (don''t ask me why, I played around with them and nothing changed when I changed the order)

2. HOWEVER, when creating the view matrix the order DOES matter depending on the effect you want. You have to order the Yaw, Pitch, and Roll matrices according to how you want the camera to turn... Example if you want look up, but be able to turn remaining in the same "standing" position, you need to adjust Yaw first then Pitch. If you want to look up but then start standing on walls (basically) do the Pitch first... adding in roll to all this adds a whole new level of complexity which is difficult to put to words.

Someone got an explenation for all this? I could probably understand it even if the explination is kind of complicated, I would prefere to know because I would be better able to deal with it in the future (for whatever reason).

-Omalacon
Yeah, I know exactly what you are talking about. This is one of the main problems with using angles to specify your camera''s orientation. Another problem very close to what you are talking about is called Gimbal Lock. This is where rotations appear to cancel each other out. There''s an article on this site with explains cameras pretty well.

Programming -> Math and Physics -> Viewing Systems for 3d Engines

I never was good at explaining this, but I''ll try. First, make a paper airplane and hold it. Now rotate the airplane around ONE axis keeping in mind that the original coordinate system does not move. Now rotate the plane along one of the other axes. (Remember to use the original axes). Finally, put the plane back in the original position and repeat the two rotations you did before expect do them in reverse.

Do you see what''s going on? Once you move the plane from it''s origianl position, other rotations use the modified plane position instead of what you are expecting. For example, if you pitch the plane up 45 degrees, then try to change the yaw, the plane doesn''t move around the line through the middle of the plane, it moves around the line that used to be in the middle of the plane.

God I hope that helps, becuase I don''t know if I can explain it any better.

This topic is closed to new replies.

Advertisement