← Pixel Dreamer Studio

A ghost that drives the lap at real speed

Store a time with every sample and play it back by time, blending between samples.

A ghost replay that stores one pose per frame and plays one pose per frame only runs at the right speed when playback has the same frame rate as the recording. In our Godot 4.7 test a car lapped a 50 m circle at 25 m/s, 12.57 s. Recorded at 60 fps, the ghost took 5.24 s at 144 fps and 25.13 s at 30 fps. Looking poses up by time × 60 broke when the recording ran at 144 fps: 30.18 s. Recording with time stamps and playing back by time took 12.57 to 12.60 s at every frame rate, never more than 0.7 cm off the car's path.

The code

Save it as ghost_replay.gd and keep one with var rec := preload("res://ghost_replay.gd").new(). While the player races, call rec.record(delta, car.global_transform) every physics frame, and rec.finish(car.global_transform) on the finish line. For the ghost, add delta to a ghost_time each frame and set ghost.global_transform = rec.sample(ghost_time). It keeps one sample every 0.05 s and blends between them, position and rotation together.

extends RefCounted
## Records a car (or any node) with time stamps and plays it back by time,
## so a ghost runs the lap in the same time at any frame rate. It keeps one
## sample every 0.05 s and blends between them, so the recording stays small.
##   var rec := preload("res://ghost_replay.gd").new()
##   while racing:   rec.record(delta, car.global_transform)
##   at the finish:  rec.finish(car.global_transform)
##   ghost:          ghost_time += delta
##                   ghost.global_transform = rec.sample(ghost_time)


var sample_every := 0.05
var times := PackedFloat64Array()
var poses: Array[Transform3D] = []
var _clock := 0.0


func clear() -> void:
	times.clear()
	poses.clear()
	_clock = 0.0


func record(delta: float, pose: Transform3D) -> void:
	if times.is_empty():  # the first call keeps the start pose at time 0
		times.append(0.0)
		poses.append(pose)
		return
	_clock += delta
	if _clock - times[times.size() - 1] >= sample_every:
		times.append(_clock)
		poses.append(pose)


func finish(pose: Transform3D) -> void:  # keep the exact finish moment
	if times.is_empty() or _clock > times[times.size() - 1]:
		times.append(_clock)
		poses.append(pose)


func duration() -> float:
	return times[times.size() - 1] if times.size() > 0 else 0.0


func sample(time: float) -> Transform3D:
	if times.is_empty():
		return Transform3D.IDENTITY
	var last := times.size() - 1
	if time <= 0.0:
		return poses[0]
	if time >= times[last]:
		return poses[last]
	var i := times.bsearch(time)  # the first sample at or after this time
	var a := i - 1
	var w := (time - times[a]) / (times[i] - times[a])
	return poses[a].interpolate_with(poses[i], w)

What we measured

A car driving a 50 m circle at 25 m/s, 12.57 s per lap, recorded and played back at different frame rates. Two runs gave the same numbers.

Why it happens

A list of poses has no time in it, so playing one per frame replays frames, not seconds: at 144 fps the 60 fps lap plays 2.4 times too fast. Working out the index from time with a fixed 60 assumes the recording frame rate, and every player records at whatever their machine runs. Storing the time of each sample makes playback independent of both frame rates, and blending with interpolate_with() fills the gaps smoothly, including the car's rotation, so the ghost glides instead of stepping from one stored pose to the next.

What to change

What the race looks like: Racing & Kart VFX for Godot 4 has 28 ready-made racing effects in Stylized and Retro PSX looks, including Drift Smoke, mini-turbo Drift Sparks, Nitro and Backfire, plus skid marks drawn as a ribbon on the ground.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Racing & Kart VFX for Godot 4 has 28 ready-made racing effects in Stylized and Retro PSX looks, including Drift Smoke, mini-turbo Drift Sparks, Nitro and Backfire, plus skid marks drawn as a ribbon on the ground.

More how-tos for Racing & Kart VFX for Godot 4: Particles left behind by a moving object · Tire smoke that only shows when the car drifts

More how-tos: Game systems