Smoother camera

Recently I came across an interesting problem where I had to follow a moving game object with the camera. The object was physical based with rigid body and the camera movement had to be delayed with a spring effect. I am using Unity Engine. The implementation was pretty straightforward, the camera position is continuously interpolated to the object position. The object is moving, the camera is following, simple. 

Now the problem was that there was a visible object jittering. When the spring effect was turned off and the camera was in fixed distance from the object the jittering disappeared. How the spring effect on camera is related to object jittering?

The answer is frequency. The physics subsystem use a different updating frequency. Game physics is usually updated with a fixed time step and does not depend of the frame rate. Camera on the other hand is a part of the main game loop with variable speed.


Frequency graph


The graph shows the difference between physical subsystem and the game loop. Physics is updated constantly regardless of the frame rate, game loop frequency is only limited by the performance of the game (or the limit is set by design).

But how does this help with jittering? To understand the problem let's look at the next two examples. On the first image there is a moving capsule that represents a physical based object followed by a camera with fixed distance (no spring effect). The capsule is moving in constant time steps and because the camera is in fixed distance it always points on the object correctly without jittering.


Fixed camera distance


Second image shows the camera on the spring. The capsule is being moved with fixed time step same as before but the camera distance is now more dynamic. The spring makes the camera delayed while the capsule is moving, when the capsule is stopped the camera is still moving towards the original distance. Now because of the camera position is updated on a game frequency and the capsule position is updated on a frequency of physics the capsule object start being jittery.

Camera with variable distance (spring effect)


The jittering is more obvious when the the strength of the spring is smaller. In the world space everything seems to look correct, the capsule is moving forward and the camera is moving towards the capsule. The jittering is a side effect of the fact that we are seeing the world with a camera eye object and everything outside of its frequency becomes jittery.

Fixing the issue wasn't so obvious, I tried to update the spring with fixed time step same as the capsule but it didn't make anything better, the jittering became even worse. After some research I came to these conclusions:

  • Camera must be updated on the game update for the smoothest possible feeling of the game
  • Spring effect must be calculated on the game update for the same reason
  • Capsule must be updated on the physics update in order to work properly

After going back and forward I realized that the capsule (or any moving object) can be split into 2 parts - physical body and visual geometry. Physical body can have all the physical properties like rigid body, collision geometry etc; and the visual geometry object is the actual capsule that you see on the screen. Now the physical body is updated with fixed time step and the visual body is interpolated to physical body within the game loop. The camera is now moving towards the the visual body without any jittering side effects.
Smooth camera with spring effect.


This solution has proved to be robust in variable framerate due to the fact that the camera and the capsule geometry are updated with the same time step.