← Pixel Dreamer Studio

Pick the radial menu slot the mouse is pointing at

Measure the angle from straight up, wrap it with fposmod, and add half a slot before dividing.

atan2() returns -PI to PI, starting from the right, so dividing it by the slot size gives negative numbers, and int() rounds -0.5 and 0.5 both to 0. In our Godot 4.7 test with 8 slots it returned only 7 different numbers, from -3 to 3. Even counted from the top with fposmod(), the slot under the mouse was wrong half of the time, because slot 0 is drawn centered on the top, not starting there. Add half a slot first: 0% wrong over 3,600 directions.

The code

Call slot_at(wheel_center, mouse_position) every frame while the wheel is open, highlight that slot, and use it when the button is released. -1 means the mouse is in the middle: pick nothing, so the player can close the wheel without choosing.

extends Node
## Which slot of a radial menu (emote wheel, weapon wheel) the mouse points at.
## Slot 0 is centered straight up, then clockwise. Returns -1 inside the dead zone.

@export var slots := 8
@export var dead_zone := 40.0  # pixels from the center where nothing is picked


func slot_at(center: Vector2, mouse: Vector2) -> int:
	var d := mouse - center
	if d.length() < dead_zone:
		return -1
	var a := fposmod(atan2(d.x, -d.y), TAU)  # 0 = up, growing clockwise (screen y points down)
	var size := TAU / slots
	return int(fposmod(a + size * 0.5, TAU) / size) % slots  # + half a slot: slot 0 is centered on up

What we measured

8 slots, slot 0 drawn centered straight up, and the mouse at 3,600 directions around the wheel, one every 0.1 degrees. The right slot is the one whose center is closest to the mouse direction. Two runs with the same numbers.

Why it happens

atan2(y, x) measures from the right, and on screen y points down, so it turns clockwise and goes negative above the center. int() rounds toward zero, which merges the two slots on either side of 0. Slots are drawn centered on their direction, so slot 0 reaches from half a slot left of straight up to half a slot right of it. Adding half a slot before dividing lines the math up with the drawing.

What to change

Show the chosen emote over the character: icon above a 3D character's head. NPCs that emote on their own: a crowd that never reacts in step.

Ready-made emotes: Emote Bubbles & Comic Pops VFX for Godot 4 has 33 effects that float over 3D characters' heads, including Love Hearts, Surprise, Anger, Dizzy Stars and comic POW! and KO! pops.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Emote Bubbles & Comic Pops VFX for Godot 4 has 33 ready-made emote and comic effects that float over 3D characters' heads, including Love Hearts, Surprise, Anger, Dizzy Stars and comic POW! and KO! pops.

More how-tos for Emote Bubbles & Comic Pops VFX for Godot 4: A crowd of NPCs that never emote or bob in step

More how-tos: UI and HUD