# Thread: Camera rotation/movement in 3D world

1. ## Camera rotation/movement in 3D world

hmm.. I seem to be having some troubles with moving a camera in 3D space.. I've read through some tutorials and tried writing some code but I just cannot get the effect I want..

1) When up or down is pressed on keyboard I want the camera & view to move up or down (like someone is wearing a jetpack and is flying)

2) When left or right is pressed on the keyboard i want the camera to rotate around a point vertically

Can someone tell me the steps i need to accomplish this?

1) Check for keyboard input
2) Find the direction between camera & view
3) Find the cross product between direction & upvector
4) Normalize the cross vector
5) who knows ???

Am i even on the right track? I've looked at code that rotates the camera around a point horizontally and strafes the camera left and right but i can't understand the math.. anyone have a good site that explains the math involved with some diagrams or something? I can't picture the upvector or the cross product vector in my mind... its making it difficult to imagine what axises i need to rotate about... thnx for any help

[EDIT] i've messed around a bit more and i got the camera to act like someone is wearing a jetpack.. though i doubt i did it correctly...
Code:
1) Camera view - camera position = camera direction
2) Camera position = Camera position + camera direction * constant speed
3) Camera view = Camera view + camera direction * constant speed
can someone tell me if this is the correct way to go about this? it works though i haven't tried rotating the camera so i don't know if this code will still work since i noticed in camera strafing left/right you find the cross product of the camera's direction and camera's up vector.. though i couldn't get camera to go up/down unless i removed the cross product thing...

2. Okay, lets assume you have a camera class that stores information about your camera. Lets say you have the following things defined.

Point3D pPosition; // Location
Vector3D vRight; // Right of camera
Vector3D vUp; // Up from camera
Vector3D vBack; // Back of camera

So, when you say move up or down I assume you mean move the camera along the Y-Axis. If you actually mean Look up and Look down, then you can get that effect by using matrices.

Moving your camera around can be accomplished by use of matrices. You'll need matrices for X, Y and Z rotations. Then, Y Rotations will be looking left and looking right, X rotation will be looking up and down. If you need the specific matrices let me know but they are fairly easy to find and derive from scratch. Use your vRight vUp and vBack vectors in these calculations for the axis's though. For camera movement simply increment the pPosition by some scalar multiple of the back vector. I can go into more detail if you want. I'm not sure how much you know of Linear Algebra but it certainly helps. Let me know if you need more steps or something and I can write out some more.

3. X rotation will be looking up and down
how do you figure?

EDIT:
though i couldn't get camera to go up/down unless i removed the cross product thing...
I'd like to be able to explain all of this to you but I'm not sure I've got it all down myself, so I'm going to hold off. I can, however, tell you that when you are moving up/down, only move along the up orientation axis(which is almost always Y or Z).

EDIT1: I'd really appreciate it if you could send those matricies or send me to them or something. I'm not looking for them myself right now because I'm going to bed and I'm lazy and I don't want to do the work if someone already has them

4. Rotation with respect to the X axis of the camera will be equivalent to looking up/down. Think about the X axis. Then imagine something rotating with respect to that axis. It should be clear to see this is how you can make the camera look up / down. Here are the rotation matrices with respect to each axis.

I had them typed out but they looked real ugly cause I couldn't get the formatting quite right. Instead I'll give you a link to a site.

http://216.239.57.100/search?q=cache...hl=en&ie=UTF-8

5. You really only need 1 single matrix to represent any rotation about an axis which goes through the origin -- you don't need one for each axis.

6. Yup, I know. That matrix is a little hairy though and for people just starting out its easier to tell them the standard ones. They are easy to derive etc. The arbitrary vector one isn't hard to derive though. I learned them in order ( i.e. the 3 std. rots , then the arb. rot ) so thats generally the order in which I tell people.

7. I guess I'm confused and I would like someone to clear some things up for me. In first person shooters, you are constantly moving and therefore the vector you rotate up/down about is also constantly changing, hence the reason you must find the cross product between the view vector and the up vector, correct?
You really only need 1 single matrix to represent any rotation about an axis which goes through the origin -- you don't need one for each axis
What about an arbitrary vector? Will the rotation about the x axis work for any cross product between the view and up vectors? I guess I'm really shaky on this stuff, so I thiank you Mr. Wizard for sending the link, looks very useful.

EDIT: Do either of you have a book on just this type of stuff that describes matrices of different rotations/translations/scalings etc etc?
Any suggestions on this?
http://www.amazon.com/exec/obidos/se...674404-5770368

8. ## Hello

Vector3D vRight; // Right of camera
Vector3D vUp; // Up from camera
Vector3D vBack; // Back of camera

i found myself completely lost already ... so i decided to do some searching and I think I've come across a good tutorial that talks about Rotation/Translate/Transform matrices in the realm of 3D programming.. I haven't finished it yet but it has "DIAGRAMS" to show what they are talking about.. HOORAY... from what I've read so far it seems like a pretty good tutorial...

Rotation Matrices Tutorial For 3D Programming

I'm sure when I'm done reading it i'll have more questions and i'll post 'em here..

9. Originally posted by Silvercord
Yes, a single matrix can represent any rotation about any axis (even arbitrary axes) which go through the origin.

You can derive that matrix on paper by writing up the rotation matrices for x rotation, y rotation, and z rotation (actually, you can represent any rotation with rotations around 2 standard axes as well) and then multiplying them all together. Once you get that matrix, you can just plug in any vector axis and angle and you'll get a valid rotation matrix about an arbitrary axis.

10. So does that mean that you must always translate to the origin to ensure the vector will cross through it?

11. Originally posted by Silvercord
So does that mean that you must always translate to the origin to ensure the vector will cross through it?
Well if you are deriving the matrix then yes. Here are the steps you would go through to get the final arbitrary vector matrix.

1. Translate so that rotation axis passes through origin
2. Rotate so that the rotation axis is aligned with one of the principle coordinate axes
3. Perform rotation of object about coordinate axis
4. Perform inverse rotation of step 2
5. Perform iInverse translation of step 1

You will then compose all of these together to get one final matrix.

So to answer your question yes you do need to translate so that is crosses through the origin but when using the std. matrix it is done "behind the scenes" for you.

Near the end of this page you will find the matrix you desire.

http://216.239.57.100/search?q=cache...n&ie=UTF-8</a>

As far as a book goes, I read through a lot of this book one day and it was pretty good imo. You should probably just flip through it one day at a store before you buy it ( like any other book ).

http://search.barnesandnoble.com/boo...84500379&itm=1

This book covers a wide variety of things such as vectors and matrices, BSP Trees, quaternions, intersections ( i.e. line/plane , ray/sphere ).

12. Just from reading the description offered at that link, that books seems incredibly good and I am going to go to barnes and noble to check it out. Thanks.