Need Orbit feature for 3rd person camera
I have a working 3rd person camera, which allows me to follow a character around, however i want to add a mouse look feature to this also, which will allow me to look around (360 degrees) the character, while keeping the model (car) in the center, can anyone help me with this?
void FollowCamera(void)()
{
// Get the position of the thing we are looking at, and the orientation of it
vec3_Copy(cg.car.position, carpos);
vec3_Copy(cg.car.axis[1], cary);
// Work out the 2D direction from the car to the camera
vec3_Sub(cg.camerapos, carpos, viewdir);
viewdir[2] = 0;
vec3_Normalise(viewdir);
// move out along this vector the correct distance
vec3_AddScale(carpos, viewdir, cipher_cvar_Float(cg_thirdpersonrange), cg.camerapos);
cg.camerapos[2] += cipher_cvar_Float(cg_thirdpersonheight);
// Set the camera orientation
// Set the camera up vector
vec3_Set(0,0,1, up);
// figure out a focus position ( a point ahead of the car)
cary[2] = 0;
vec3_Normalise(cary);
vec3_AddScale(carpos, cary, cipher_cvar_Float(cg_thirdpersonfocus), focus);
vec3_AddScale(focus, up, cipher_cvar_Float(cg_thirdpersonfocusheight), focus);
// set the view info structure with the camera position and axis
vec3_Copy(focus, cg.camerafocus);
cg_LookAtToAxisUp(cg.camerapos, focus, up, cg.view.axis[0], cg.view.axis[1], cg.view.axis[2]);
}
Use sin/cos to place the camera on an arc at the angle determined by the movement, the rotate the camera back toward the main character at the same angle.
It''s all sin/cos pythagorean stuff.
It''s all sin/cos pythagorean stuff.
Use this, with graph :
Car -( 0, Rorbit, 0 )-> CameraOrbitCenter -( Tcam, Rcam, Scam )-> Camera
Use Rorbit to rotate the camera around. Tcam, Rcam and Scam should be positioning the camera behind the car when the car is at (0,0,0).
Car -( 0, Rorbit, 0 )-> CameraOrbitCenter -( Tcam, Rcam, Scam )-> Camera
Use Rorbit to rotate the camera around. Tcam, Rcam and Scam should be positioning the camera behind the car when the car is at (0,0,0).
rotation += mousex_speed;elevation += mousey_speed;float cos_rot = (float) cos(rotation);float sin_rot = (float)-sin(rotation);float cos_elv = (float)cos(elevation);float sin_elv = (float)sin(elevation);//--------------------------------------// Limit elevation to an angle//--------------------------------------float elv_limit = RadToDeg(85.0f);elevation = (elevation < -elv_limit)? -elv_limit : (elevation > elv_limit)? elv_limit : elevation;//--------------------------------------// calcualte camera position (on the spehre around the player)//--------------------------------------camera_position = player_position - Vector(cos_rot*sin_elv, cos_elv, sinrot*sin_elv) * camera_radius;camera_target = player_position; // camera positioncamera_up = Vector(0, 1, 0); // requested up vectorcamera_dir = camera_target - camera_pos; // calculate dircamera_dir /= camera_dir.Length(); // normalisecamera_side = camera_up.Cross(camera_dir); // calculate side vectorcamera_side /= camera_side.Length(); // normalisecamera_up = camera_dir.Cross(camera_side); // recompute up vector
so, for you....
//--------------------------------------
// rotate angles with mouse
//--------------------------------------
rotation += mousex_speed;
elevation += mousey_speed;
float cos_rot = (float) cos(rotation);
float sin_rot = (float)-sin(rotation);
float cos_elv = (float)cos(elevation);
float sin_elv = (float)sin(elevation);
//--------------------------------------
// Limit elevation to an angle
//--------------------------------------
float elv_limit = RadToDeg(85.0f);
elevation = (elevation < -elv_limit)? -elv_limit : (elevation > elv_limit)? elv_limit : elevation;
// set the view vector
vec3_Set((cos_rot*sin_elv), (cos_elv), (sinrot*sin_elv), viewdir);
vec3_AddScale(carpos, viewdir, cipher_cvar_Float(cg_thirdpersonrange), cg.camerapos);
vec3_Set(0,0,1, up);
cg_LookAtToAxisUp(cg.camerapos, carpos, up, cg.view.axis[0], cg.view.axis[1], cg.view.axis[2]);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement