Jump to content
  • Advertisement
Sign in to follow this  

Third Person Camera Psuedocode

This topic is 4350 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

I'm testing around with gluLookAt() and WM_MOUSEMOVE to develop a third-person shooter camera. I'm working on the psuedocode right now and here's what I have: When the camera moves, back up the old x,y mouse coordinate and record the new x,y mouse coordinate. Calculate the average change by subtracting the old x&y coordinate by the new one and dividing by two. Store the average variable in a different x&y coordinate. Calculate the new position of the camera using the averaged x&y coordinate The section that's bolded is the portion I need help with; I don't know how to get the camera to move in a circle around the character. The third person camera will be a usual one, move the mouse up and down and you aim up and down, move it left to right - so on and so forth. Can someone help me develop the psuedocode for the equation of the camera position? Centering the camera on the person won't be a problem since gluLookAt() already takes care of that problem.

Share this post

Link to post
Share on other sites
Store the camera as a horizontal angle (unbounded), a vertical angle (locked between 90 and -90 degrees), and a distance from a look at point (probably the hero's head). A change in the mouse x coord should add or subtract from the horizontal angle, and a change in y should change the vertical angle. From there it is just trig to find the actual camera location. You may have to play with negating either or both of the deltas in order to influence your angles in the correct directions.

// scale the change according to a
// sensitivity that feels good,
// then add to our angles
ax += mouse_delta_x * mouse_sensitivity_scalar;
ay += mouse_delta_y * mouse_sensitivity_scalar;

// lock ay so that camera doesn't turn upside down
if (ay > 90) ay = 90;
if (ay < -90) ay = -90;

// find the point on the unit sphere
pos.y = sin(ay);
pos.x = cos(ay) * sin(ax);
pos.z = cos(ay) * cos(ax);

// increase the radius as desired
pos *= dist;

// center around the lookat point
pos += lookat;

Share this post

Link to post
Share on other sites
Sign in to follow this  

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