← Pixel Dreamer Studio

Spin a HUD ring, radar or icon in place instead of around its corner

A Control rotates and scales around pivot_offset, which starts at its top-left corner. Put the pivot in the middle, and keep it there when the size changes.

In Godot 4.7, set pivot_offset_ratio to (0.5, 0.5). Without it, set control.pivot_offset = control.size / 2, and set it again on every resized signal. In our Godot 4.7 test a 200 by 200 ColorRect with the default pivot of (0, 0) moved its center 200 px when turned 90 degrees and 70.7 px when scaled 1.5 times. With the pivot set to the middle once, it turned in place, until it was resized to 300 by 300: then its center was 100 px off again. Updating the pivot on resize, or using pivot_offset_ratio, kept it at 0.

The code

Save it as spin_pivot.gd. keep_centered() puts the pivot in the middle now and after every resize; calling it twice does not connect twice. center_drift() tells you how many pixels rotation and scale have moved the Control's center: print it when something spins off to the side. For example: const SpinPivot := preload("res://spin_pivot.gd"), SpinPivot.keep_centered($Radar), then turn it with $Radar.rotation += speed * delta.

extends RefCounted
## Spin and scale a Control (a HUD ring, radar, icon or button) around its own center
## instead of its top-left corner.


## Puts the pivot in the middle now, and again every time the Control changes size.
static func keep_centered(control: Control) -> void:
	control.pivot_offset = control.size / 2.0
	if control.has_meta(&"_pivot_centered"):
		return
	control.set_meta(&"_pivot_centered", true)
	control.resized.connect(func(): control.pivot_offset = control.size / 2.0)


## How far rotation and scale have moved the Control's center on screen, in pixels.
## 0 means it turns and grows in place.
static func center_drift(control: Control) -> float:
	var own := control.get_global_transform()
	var parent := own * control.get_transform().affine_inverse()
	var laid_out := parent * (control.position + control.size / 2.0)
	var drawn := own * (control.size / 2.0)
	return laid_out.distance_to(drawn)

What we measured

A ColorRect at (380, 170), 200 by 200, on a CanvasLayer, so its center sits at (480, 270). We compared where its corners were drawn with where its layout puts it. Two runs gave the same numbers.

Why it happens

Every Control turns and scales around its pivot_offset, measured in pixels from its top-left corner, and the default is (0, 0). A Node2D or Sprite2D turns around its own origin, which for a centered sprite is the middle, so the same rotation code works there and not on a TextureRect. pivot_offset is a fixed number of pixels, so anchors, containers or a new screen size that change the Control's size leave the pivot where the old middle was.

What to change

Blips on the radar once it spins: radar blips that turn with the player.

Spinning rings and radars ready to drop in: Godot 4 Ready Kit includes 124 Sci-Fi HUD Elements, whose rings, spinners, radars and reticles carry a spin script that sets the pivot to the middle and again on every resize, the same way as this page.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Godot 4 Ready Kit is 9 products in one bundle, including 124 Sci-Fi HUD Elements whose rings, spinners and radars turn around their own center at any size.

More how-tos: UI and HUD