Victor's Devblog

If you are interested to receive those weekly articles as a newsletter, poke me offline! Automated subscription is disabled because of bots...

Otherwise, Atom feed is here! Copy feed URL

3C part 2 – Camera

As promised, this week is about camera! As far as I could go while getting sick on Friday...

The Goal

Of course my game will allow an "interior" view, so here the goal is to define how exterior camera(s) will follow the car.

Before wasting my time implementing any algorithm, I had to figure out what I want the camera to help with:
- see road ahead at high speed
- know how inputs translate to steering
- work while car is drifting
- not motion-sickening

And those constraints lay down a few rules:
- the camera must be above the car and looking down, increasing the space the road takes on the screen
- if the car is grounded, the camera's up is the same as the car's
- the camera cares about where the car is going, not how it's facing
- movement must be smooth and fps-agnostic

The Solution

Those rules in mind, I went for a simple algorithm. Each frame, the presentation thread updates the camera's position as follows:
1. the camera is placed on a rig, sort of a rail, attached to the car
2. the rig's up vector converges towards the grounded car's up vector
3. the rig's forward vector converges towards the car's velocity vector, within the plane defined by the rig's up
4. the camera is then placed in this rig's space at a given offset behind the car, and aims at some other offset from the car

In red is where the car is heading, in blue is the rig that attempts to follow this direction, and in pink and green are the two offsets.

The Oomph!

Ok but if I implement just this, it feels a little static. The key element is to also move the camera closer or further from the vehicle depending on whether the vehicle is accelerating or decelerating. Bonus: this doubles as a way to visually know the state of the car. Additionally, the tighter the turn, the closer the camera gets to the car to give better visibility near obstacles it may be attempting to dodge.

This is simply achieved by placing the camera on a rail along the rig, and changing its desired distance behind the car based on what the player is doing (throttle, brake, reverse, etc.).

And in video it gives:

Bonus: you can see at the end of the recording that I also adapted the algorithm to somewhat work when the car is not grounded; the camera simply preserved its previous `up` vector and continue following where the car is going.