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.
- One pose per frame, recorded at 60 fps (755 poses) and played one pose per frame: 12.57 s at 60 fps, 5.24 s at 144 fps and 25.13 s at 30 fps.
- Pose index = playback time × 60, with the recording made at 144 fps: 30.18 s at 60 fps and 30.17 s at 144.
- The script, recorded at 60 fps (216 samples) or 144 fps (228 samples) and played at 30, 60 and 144 fps: 12.57 to 12.60 s, the difference being where the last playback frame falls, and never more than 0.7 cm from the car's real path. With one 0.25 s frame during the recording, the ghost was up to 14.1 cm off across that frame, because nothing was recorded inside it.
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
- Counting laps so a ghost or a player cannot cut back over the line: checkpoints in order. Random events in a race that should play out the same in a replay: same seed, same results.
- Tire smoke only when the car really slides: drift smoke from sideways speed.
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
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene