Answered by:
Camera
Question

I'm currently working on a sandbox game similiar to GTA. I have the camera working semicorrect. It works in 3rd person as I expect and pressing the right stick up and down moves the camera properly while maintaining focus on the players back. My problem is implementing the "swing". When you press right on the stick, it should spin around in a circular motion maintaining focus on the players chest.
I have the variables for the players location, the translated camera, and a int between 0360 for the degrees the camera should be set at, but I can make the camera render things properly.
Thanks in advance,
DerekThursday, January 18, 2007 10:53 PM
Answers

I am no expert, Derek, but I think I would solve this by keeping track of:
player_location (XYZ in world units)
camera_distance (in units, e.g. pixels)
camera_angle (in degrees)
, like the way you described. For now, let's consider the camera is at a fixed height from the ground at all times.
You can then calculate the camera postion like this (pseudo code):
radians = camera_angle * pi/180.0
camera_swing = camera_distance * vector(cos(radians), 0, sin(radians))
camera_location = player_location + camera_swing + vector(0, camera_height, 0)
All you have to do then is create a "pointTo" or "lookAt" matrix (I believe there is a standard function for that in XNA) from the camera_location, the player_location and the upvector, and use it as the transformation matrix of your camera. I hope you get what I mean.
There should be better ways than this, but my solution might help you for now.Friday, January 19, 2007 4:15 PM
All replies

I am no expert, Derek, but I think I would solve this by keeping track of:
player_location (XYZ in world units)
camera_distance (in units, e.g. pixels)
camera_angle (in degrees)
, like the way you described. For now, let's consider the camera is at a fixed height from the ground at all times.
You can then calculate the camera postion like this (pseudo code):
radians = camera_angle * pi/180.0
camera_swing = camera_distance * vector(cos(radians), 0, sin(radians))
camera_location = player_location + camera_swing + vector(0, camera_height, 0)
All you have to do then is create a "pointTo" or "lookAt" matrix (I believe there is a standard function for that in XNA) from the camera_location, the player_location and the upvector, and use it as the transformation matrix of your camera. I hope you get what I mean.
There should be better ways than this, but my solution might help you for now.Friday, January 19, 2007 4:15 PM 
Something like this:
Vector3 CameraPos(Matrix playerMatrix, float back, float up, float rotateDegrees)
{
return playerMatrix.Translation + (playerMatrix.Backward * back + playerMatrix.Up * up) * Matrix.CreateFromAxisAngle(playerMatrix.Up, rotateDegrees * Math.PI / 180);
}
Note that the control is player relative; if you want the rotation to be uprelative, pass in "Vector3.Y" instead of playerMatrix.Up to CreateFromAxisAngle.Friday, January 19, 2007 8:31 PM