Sign in to follow this  

Third Person Camera Psuedocode

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

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