Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Camera Controls, Pt. 1

Sign in to follow this  


Well, my camera took a bit longer than expected, but I'm pretty happy with it. Its an FPS style camera, where the mouse looks around and W, A, S, and D move you parrallel and perpendicular to the cameras view direction. This was implemented using spherical/polar coordinates, which worked quite well.

Since its pretty hard to take screenshots of a camera system, you lucky people get some derivations accompanied by shitty MSPaint diagrams (well, only one shitty MSPaint diagram, but when you've seen one, you've seen them all). Enjoy!

The whole idea with spherical coordinates is that you map cartesian coords to the surfaces of a sphere and vise versa. The camera sits at the center of the sphere, and looks out at the point on the surface. This requires a little bit of trig, but nothing especially complicated.

Points in spherical coords are not represented with (x, y, z) values, but rather (r, ?, ?). r is the sphere's radius, ? is your angle from the x axis, and ? is your angle from the z axis (usings D3D's left-handed coordinate system):

Basically, all I need to do for my camera is represent a target in spherical coordinates. When its time to draw, I can convert this point to cartesian coordinates, which will in effect give me a vector from my camera to the target. The conversian goes something like this:

sin(?) = y / r -> y = rsin(?).
cos(?) = r' / r -> r' = rcos(?).

sin(?) = z / r' -> z = r'sin(?) -> z = rcos(?)sin(?).
cos(?) = x / r' -> x = r'cos(?) -> x = rcos(?)cos(?).

So, your cartesian point P = (x, y, z) is:
(rcos(?)cos(?), rsin(?), rcos(?)sin(?)), where ? / 2 > ? > -? / 2 and 2? >= ? >= 0.

By keeping a cartesian point Q = (x, y, z) that represents my camera's location in world space, I can get the 'at' vector for the lookat matrix simply by adding Q + P (Note that, even though P and Q are points, it is possible to simply treat them as vectors).

Moving the camera parrallel to this 'at' vector is easy: just scale 'at' by however much you want to move and add or subtract it to Q to move forward or backward. Moving perpendicular requires one extra step, but its not hard. The cross product of 'at' with your global 'up' vector (I use (0, 1, 0) in D3D) will give you the necessary perpendicular vector (but be carefull, this camera will break if you allow it to look straight up or down). Now, just scale and add/subtract that perpendicular vector to Q and you're all set.

Not exactly groundbreaking stuff, but I thought it was pretty nifty, and hopefully you got a little something out of this post too.

Sign in to follow this  


Recommended Comments

There are no comments to display.

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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!