← Pixel Dreamer Studio

Spin a magic circle forever at the same speed

Wrap the angle between -PI and PI every frame, so it never grows big enough to lose precision.

Node2D keeps rotation exactly as you set it, so rotation += speed * delta grows forever, and a big number cannot take small steps exactly. In our Godot 4.7 test at 60 degrees a second, with rotation set to where it would be after 1 hour, each frame's step was 0.01733 instead of 0.01745 radians. After 10 hours 0.01563, 10% slow. After 100 hours 0.03125, nearly twice as fast. Wrapping the angle between -PI and PI kept every step at 0.01745.

The code

Put the script on the Node2D or Sprite2D that spins, and set degrees_per_second. Use a negative value to spin the other way.

extends Node2D
## Spins forever (magic circles, loading rings, portals) at the same speed, even
## after hours, by keeping the angle between -PI and PI.

@export var degrees_per_second := 60.0


func _process(delta: float) -> void:
	rotation = wrapf(rotation + deg_to_rad(degrees_per_second) * delta, -PI, PI)

What we measured

A Node2D at 60 degrees a second, 0.01745 radians a frame at 60 fps. Rotation set to the angle it reaches after 0, 1, 10 and 100 hours, then 60 frames of +=. Two runs with the same numbers.

Why it happens

The angle is stored with limited precision, and the bigger it is, the coarser the steps it can hold. At 37,699 radians the nearest step it can store to 0.01745 is 0.01563, so every frame adds that instead. Keeping the angle between -PI and PI keeps it small, where steps are exact. The sprite looks the same, since a full turn is a full turn.

What to change

The shader version of this: scrolling texture jumps once an hour. Spinning circles that look grainy when small: mipmaps for magic circles.

Magic circles to spin: PD Sigil Forge Pro generates magic circles in 8 styles and exports large PNGs, SVG and spinning animations.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: PD Sigil Forge Pro generates magic circles in 8 styles and exports large PNGs, SVG and spinning animations.

More how-tos: Auras, rings, beams and weather