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.
int(atan2(d.y, d.x) / slot_size): 7 different numbers for 8 slots, from -3 to 3, negative for 37.5% of the directions. It also counts from the right, so its numbers did not match the slots as drawn at any direction.- Counting clockwise from straight up with fposmod(), without half a slot: wrong for 50.0% of the directions, one half of every slot.
- The script: 0.0% wrong, and -1 for a mouse 10 pixels from the center.
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
slots: any number of slots, the math does not change.dead_zone: how far the mouse must move from the middle before a slot is picked.
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
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total