Sign in to follow this  

Help me I'm a newbie! -- Camera maths

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK I'm just starting with this whole 3D stuff and I was wondering about how I would do this. I want my camera to rotate about its target point (i.e. (0,0,0) after applying the view matrix) based on input. Input is easy enough; I'm using DirectInput to get it. Since my camera is actually a targeted camera, I can easily determine the actual location of (0,0,0). The position of the camera is just the camera's location minus the target's location. I therefore want my camera to rotate on a sphere about this point. If I press left, I want the camera to start rotating around the world's Y axis. That's easy; just rotate it about Y after putting it in the right place. But I don't know how to cope with up and down. If the camera is on the X,Y plane, then up/down will rotate about the Z axis, but if the camera is on the Y,Z plane, it should be rotating about the X axis. I don't have a clue what the transformations would be if it were arbitrarily between the two. Simply put, left/right rotate the camera along lines of latitude, while the up-down will rotate it along whichever longitude on which it currently rests. Any super-pro help would be appreciated gratefully! :)

Share this post


Link to post
Share on other sites
it's usually done by applying 2 rotations in right oder.

in openGL, with modelview matrix:
glRotate(yangle, 0,1,0);
glRotate(xangle, 1,0,0);
glTranslate(0,0,distance_from_center);
will make camera rotate around 0,0,0 keeping distance_from_center, and always look at 0,0,0;

Share this post


Link to post
Share on other sites
OK, but what's the right order? What if left and up were held at the same time? Would I have to apply Y rotation, then rotate the whole kaboodle so that it's level with the XY, then rotate around Z?

Actually, that might work!

Muchos messingaboutos, though. :s

Is there a better way?

Share this post


Link to post
Share on other sites
I think tutorials with sample code it's what you need - in many examples, camera is done like you want.

you need to store your angles of rotation in xangle and yangle, and when user press left/right , change yangle, when press up/down,change yangle.
When rendering every frame,you'll need to set that matrix from the beginning.(set to identity,preform rotations). It's not a good idea to keep one matrix and change it when player presses key.

As about better, you can do "free" camera but it's bit harder to do.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this