# Chase camera

## Recommended Posts

Hi everyone, I'm currently trying to implement chase camera model for a spaceship simulator. I've read a couple of threads regarding the subject on this site, but none of them addresses my issue. Let's forget about every aspect of camera model besides position. What I'm trying to do is adjust camera's position so that: 1. When the spaceship's velocity is constant, the camera tends to occupy certain spot behind the ship; the distance between that spot and the spaceship is constant and does not depend on spaceship's velocity. 2. When the spaceship is accelerating, the camera starts to lag behind 3. When the spaceship is slowing down, the camera closes on the spaceship I'd like to learn about some popular solutions to this, because what I'm trying to do right now seems a little too complicated and doesn't work too well. Thanks. P.S. Right now I'm trying the following: 1. I think of a camera as of a rigid body connected to the spaceship with a spring. 2. The space "friction" is dependent upon camera's velocity 3. I solve ODE and find x(t) for the camera 4. It works fine in math, but since the spaceship moves in an unpredictable fashion (I don't know what's going to happen the next frame), I have to re-solve ODE every frame, and for some reason the solution "jumps" too much resulting in unacceptable camera behavior. The other problem is that friction makes camera's stable position depend on its speed, which is not what I need. This can be reduced by decreasing friction, but the amplitude of oscillation would grow then.

##### Share on other sites
OK, I have a couple of ideas.

Try recording the ships location over a few seconds, then using linear interpolation? record one frame per 0.5 seconds, using a circular buffer of 3 seconds, then use linear interpolation, which will smooth out the sudden changes in velocity. (I tried this years ago in visual basic, with one robot following another).
This algorithm was inspired by a level in Rayman on the playstation, where an evil Rayman follows the hero.

Heres another one:

Each frame, the camera updates its vector from the ship. Then you calculate the distance you need to be from the ship depending on the speed with a simple function. Then you multiply the vector by this factor, to get the "ideal" distance for the camera to be from the ship.

The camera travels along the vector each frame, modifying its acceleration depending on the actual distance. If this is further away than the IDEAL distance it accelerates, and if closer it decelerates.

[edit: clarified a few sentences]

[Edited by - speciesUnknown on August 12, 2007 1:55:55 PM]

##### Share on other sites
Quote:
 The ship travels along the vector each frame, modifying its acceleration depending on the actual distance. If this is further away than the IDEAL distance it accelerates, and if closer it decelerates."

Do you mean "The CAMERA travels..."?

##### Share on other sites
Chase Camera Sample

^ It's in C# using the XNA Framework, but if you take a look at the source you should be able to port the code into whatever language you're using. It's a pretty good chase camera implementation.

##### Share on other sites
Quote:
Original post by tokaplan
Quote:
 The ship travels along the vector each frame, modifying its acceleration depending on the actual distance. If this is further away than the IDEAL distance it accelerates, and if closer it decelerates."

Do you mean "The CAMERA travels..."?

Sorry, yes i did. Ill edit the post to clarify this.

##### Share on other sites
Quote:
 Original post by FunLogicChase Camera Sample^ It's in C# using the XNA Framework, but if you take a look at the source you should be able to port the code into whatever language you're using. It's a pretty good chase camera implementation.

Thanks for the link, FunLogic, but this sample suffers from the same problem my current implementation does - the camera offset depends on the ship's speed. This is unacceptable if the speed range is wide enough (and it is in my case).

##### Share on other sites
Quote:
 1. When the spaceship's velocity is constant, the camera tends to occupy certain spot behind the ship; the distance between that spot and the spaceship is constant and does not depend on spaceship's velocity.2. When the spaceship is accelerating, the camera starts to lag behind3. When the spaceship is slowing down, the camera closes on the spaceship

just checking: are these the three rules you want your camera to obey?

Exactly

##### Share on other sites

you want your camera to satisfy those three rules.
Your game involves the ship changing velocity suddenly.
Your camera is suddenly changing velocity which you do not want.
You have a vector class or something similar, and can subtract one vector from another, and multiply a vector by a scalar.

briefly, heres what I think you need to do, Rules 2 and 3 are satisfied because the camera accelerates and decelerates at a constant speed rather than suddenly changing velocity. Rule 1 is satisfied because the camera only changes acceleration when it needs to.
If the ship stops dead, the camera moves in to meet the ship at an arbitrary speed, rather than instantly. (in theory)
If the ship accelerates, the camera will decelerate until its the correct distance.

If the ship decelerates, the opposite will occur.

I haven't figured out exactly how the camera will accelerate and decelerate, it might oscillate between too close and too far if the acceleration factor is too much. You might want to have some kind of threshold before the camera changes speeds. If its inside this threshold, its speed matches that of the ship.

DISCLAIMER: I haven't tested this. It may set your motherboard on fire, it may cause you to break out in a rash, it might cause the world to end, and any side effects are not my responsibility. Also I didnt listen in maths lessons and I probably used the wrong names for somethings. But it probably works.

float get_ideal_distance(float V){//whatever relationship you like //e.g. D = 0.5 V + 10; };void update_camera_velocity(){	//get direction vector and scalar distance	camera.direction = (camera.position - ship.position);	float actual_distance = direction.get_length();	camera.direction.normalize();		//decide what to do based on the actual vs ideal distances	float ideal_distance = get_ideal_distance(actual_distance);		if(magnitude(distance - ideal_distance) > const float some_threshold){	//cam needs to change speed			if(distance > ideal_distance){//camera is too far away 			camera.accelerate();		}else 			if(distance < ideal_distance){//camera is too close				camera.decelerate();  			};   	}	else{//otherwise camera is running at optimum distance.		camera.velocity = ship.velocity; 	};};void update_camera(float fps){	update_camera_velocity();	FACE_CAMERA_TOWARDS_SHIP()	MOVE_CAMERA_FORWARD(camera.velocity/fps);};

(p.s. feel free, anybody, to correct me if i got some maths term or equation wrong)

[Edited by - speciesUnknown on August 15, 2007 5:19:03 PM]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627702
• Total Posts
2978712

• 21
• 14
• 12
• 10
• 12