A hanging lantern, shop sign or charm that swings when hit is usually a small spring: pull the angle back to 0, and lose a bit of speed so it settles. Written the first way that comes to mind, moving with the old speed and keeping 98% of the speed every frame, it depends on the frame rate twice over. In our Godot 4.7 test, pushed to 29 degrees, at 30 fps it gained energy every frame and after 10 s was spinning round, 874 degrees. At 144 fps it lost 2% 144 times a second and was down to 1.9 degrees after 2 s, against 16.5 at 60 fps. One long 0.25 s frame at 60 fps kicked it up to 35 degrees. The script below swung to 6.3 degrees at 2 s at 30, 60 and 144 fps and settled fully by 10 s.
The code
Save it as swing.gd and load it with const Swing := preload("res://swing.gd"). Make one per lantern with Swing.new(), call step(delta) in _process() and put the angle it returns on the lantern's rotation. To push it, add to its velocity, in radians per second. stiffness sets how quick the swings are, damping how soon it comes to rest.
extends RefCounted
## A hanging lantern, sign or charm that swings back to rest the same way at any frame rate.
## const Swing := preload("res://swing.gd")
## var swing := Swing.new()
## func _process(delta):
## $Lantern.rotation.z = swing.step(delta)
## func on_hit(push: float):
## swing.velocity += push # radians per second
var angle := 0.0 # radians, 0 = hanging straight down
var velocity := 0.0 # radians per second
var stiffness := 40.0 # how hard it is pulled back: higher = quicker swings
var damping := 1.5 # how fast it settles, per second
const MAX_STEP := 1.0 / 60.0 # a long frame is cut into steps no longer than this
func step(delta: float) -> float:
var left := delta
while left > 0.0:
var dt := minf(left, MAX_STEP)
velocity += -stiffness * angle * dt # speed first
velocity *= exp(-damping * dt) # settle by time, not by frames
angle += velocity * dt # then move with the new speed
left -= dt
return angle
What we measured
A swing started at 0.5 radians, about 29 degrees, with the same stiffness of 40 in both versions, run for 10 s at steady 30, 60 and 144 fps, then at 60 fps with one 0.25 s frame at 1 s, like a loading hitch. We read the angle at 2 s and the biggest swing in the last second. Two runs gave the same numbers.
- The first try: at 2 s, 56.9 degrees at 30 fps, 16.5 at 60, 1.9 at 144. In the last second, 873.8 degrees at 30 fps (spinning round), 2.4 at 60, 0.0 at 144.
- The script: 6.3, 6.3 and 6.4 degrees at 2 s, and 0.0 in the last second, at all three frame rates.
- One 0.25 s frame at 60 fps: the first try swung up to 35.2 degrees after it, more than it started with, and was still at 23.3 in the last second of 4 s. The script: 9.2 after it and 3.0 in the last second.
Why it happens
Moving the angle with the speed from before the pull, then adding the pull, adds a little energy every step. The longer the step, the more it adds, so at 30 fps it gains more than the damping takes away and the swing grows until it goes round. Taking off 2% every frame takes off more per second the more frames there are, so at 144 fps it stops far too soon. Updating the speed first and moving with the new speed keeps the energy steady, exp(-damping * dt) takes off the same share per second at any frame rate, and cutting a long frame into steps of 1/60 s stops one hitch from throwing it.
What to change
- The same frame rate trap in other places: regen that stops at high fps and a torch that flickers faster at high fps.
Ready-made lanterns and signs: Neon Signs & Area Lights for Godot 4.7 has 41 ready-made 3D neon signs and light fixtures that light the walls and wet ground around them, including OPEN, BAR, HOTEL and RAMEN signs, paper lanterns and string lights.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Neon Signs & Area Lights for Godot 4.7 has 41 ready-made 3D neon signs and light fixtures that light the walls and wet ground around them, including OPEN, BAR, HOTEL and RAMEN signs, paper lanterns and string lights.
More how-tos for Neon Signs & Area Lights for Godot 4.7: Only 8 lights show on a long wall in Compatibility · Make a glowing sign or screen actually light the wall around it · Neon signs that share a material but not a colour
More how-tos: Game feel
- Screen shake that returns to center
- 3D camera shake that lands back on center
- Hit stop and slow motion
- Confetti particles with random colors
- Choppy particles: fixed_fps and interpolate
- Play animations in 12 steps a second, at the right speed on any screen
- Keep a smooth follow camera, but jump with the player when they teleport
- Make a harvested crop pop out in an arc
- Shake a rock or tree when hit, without it drifting
- Pickups that fly to the player in the same time at any frame rate
- A cooldown that waits while the game is paused
- Keep attack presses made just before the swing ends
- A dash that covers the same distance at any frame rate
- A hold-to-craft bar that takes the same time at any frame rate and finishes once