Fix lobby/HUD UI pinned at (0,0); shrink player hitbox below visual size

The lobby connect menu and three pieces of the HUD (hit-flash overlay, boss
bar centering, death-message centering, hint label) were all silently broken
by the same Godot gotcha: set_anchors_preset(preset) with the default
keep_offsets=false does NOT zero the offsets to the preset's margins -- it
recomputes them to preserve the control's *current* rect, which for a
freshly constructed Control is (0,0). Anchors end up correct; the actual
rect stays pinned to the top-left corner regardless. Fixed by switching to
set_anchors_and_offsets_preset() everywhere a Control is built in code, and
by using offset_left/offset_top (anchor-relative) instead of .position
(absolute) for the HUD hint label. Documented as gotcha #5 in CLAUDE.md.

Also split PLAYER_RADIUS into two constants: PLAYER_RADIUS (6.0, the
authoritative hitbox used by SimWorld) and PLAYER_VISUAL_RADIUS (13.0,
view-only, used by world_view.gd). The client renders every ship a little
late relative to the server -- interpolation delay, reconciliation
smoothing -- so a hitbox that matched the sprite would let bullets connect
against a ship the player watched dodge clear of them. A smaller hitbox
means the occasional bullet visibly clips the sprite without a hit, which
reads as more forgiving of latency than the reverse.

Verified headlessly: a throwaway scene instantiating MainMenu/HUD under a
real 1280x720 viewport, asserting the panel is centered and _canvas.size /
hint position resolve correctly, before and after each fix. check.sh,
test.sh (78/78) and smoke.sh all pass.
This commit is contained in:
2026-09-03 16:58:42 +02:00
parent c4beeae38f
commit d9a59fff03
5 changed files with 60 additions and 11 deletions
+15 -3
View File
@@ -16,15 +16,27 @@ var client: ClientRuntime = null
func _ready() -> void:
layer = 10
_canvas = Control.new()
_canvas.set_anchors_preset(Control.PRESET_FULL_RECT)
# set_anchors_preset() alone leaves the offsets at whatever preserves the
# control's rect at the moment of the call -- (0,0)-sized for a brand new
# node, regardless of the anchors -- so _canvas.size silently stayed
# (0, 0) forever. That took the hit-flash overlay, the boss bar centering
# and the death-message centering down with it, since all three read
# _canvas.size. set_anchors_and_offsets_preset() sets both correctly.
_canvas.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
_canvas.mouse_filter = Control.MOUSE_FILTER_IGNORE
_canvas.draw.connect(_draw_hud)
add_child(_canvas)
_status = _make_label(Vector2(MARGIN, MARGIN))
_hint = _make_label(Vector2(MARGIN, 0.0))
_hint.set_anchors_preset(Control.PRESET_BOTTOM_LEFT)
_hint.position = Vector2(MARGIN, -56.0)
_hint.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_LEFT)
# `.position` assigns an ABSOLUTE coordinate regardless of anchors (that's
# what stranded this label off-screen despite the anchor being correct).
# offset_left/offset_top are the anchor-relative ones -- negative offset_top
# here means "56px above the bottom anchor", which is what "-56" was meant
# to say in the first place.
_hint.offset_left = MARGIN
_hint.offset_top = -56.0
func _make_label(pos: Vector2) -> Label:
+18 -4
View File
@@ -12,13 +12,27 @@ var _status: Label
func _ready() -> void:
set_anchors_preset(Control.PRESET_FULL_RECT)
# set_anchors_preset(preset, keep_offsets=false) does NOT zero the offsets
# to the preset's canonical margins -- despite the parameter name, false
# means "recompute offsets to keep the control's CURRENT rect visually
# unchanged". For a freshly constructed Control that rect is (0,0)-sized
# at the origin, so the offsets come out as whatever preserves exactly
# that: a zero rect pinned to (0,0), regardless of the anchors. This is
# what actually caused the "menu stuck in the top-left corner" bug --
# set_anchors_and_offsets_preset() sets both halves correctly in one call.
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
# A CenterContainer recomputes its child's centered position itself, every
# time the child's minimum size or the window size changes -- no manual
# offset math to get wrong.
var center := CenterContainer.new()
center.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
add_child(center)
var panel := VBoxContainer.new()
panel.set_anchors_preset(Control.PRESET_CENTER)
panel.custom_minimum_size = Vector2(360.0, 0.0)
panel.add_theme_constant_override("separation", 8)
panel.position = Vector2(-180.0, -160.0)
add_child(panel)
center.add_child(panel)
var title := Label.new()
title.text = "TRANSCIENCE"