Simple smooth and stable camera-follow for games

In my latest game I’ve used a camera system that allows for very smooth, yet stable movements. I call it stable because it never loses the player no matter how fast he moves. It is probably a pretty common system, but I’ll write about it anyway, as it might help.

The camera uses an absolute point and a relative vector. The point is in the center of the entity the camera follows, and the vector represents direction (and distance) in which the entity is looking.

The image above shows the point at the player’s head, and the vector (of required length) that represents the look at. The circle represents the viewing distance. Camera is then simply set as:

camera.x = entity.x + entity.look_at.x
camera.y = entity.y + entity.look_at.y

Each frame I set the look at to be at the natural direction (always forward from current orientation). When the player is on the ladder, I set the direction to go up or down (see image below), so it naturally follows player’s head and eyes.

Now the smoothness trick is in how the look at is updated. In my current system I have additional vector called look_at_smoothed, which is every frame calculated like this:

entity.look_at_smoothed.x += (entity.look_at.x - entity.look_at_smoothed.x) / 15
entity.look_at_smoothed.y += (entity.look_at.y - entity.look_at_smoothed.y) / 15

And the camera uses the smoothed version instead of the “hard” look at vector, so the camera code above changes to:

camera.x = entity.x + entity.look_at_smoothed.x
camera.y = entity.y + entity.look_at_smoothed.y

So the final result (in work-in-progress game) looks like this:

External image