← Pixel Dreamer Studio

A swing that settles the same at any frame rate

Update the speed before the angle, settle it with exp(-damping * delta), and cut long frames into short steps.

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.

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

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