Fix stalagmite orientation + AABB-based model grounding

- Stalagmite: remove erroneous flip; models are spike-at-AABB-bottom so
  no rotation needed — shift down by (tile_h/2 + scaled_h) so the wide
  base lands flush with the tile underside and spike hangs below
- Tile affinity models: revert accidental +tile_h*0.5 lift (those models
  were not the subject of the grounding complaint)
- Unit/card models: replace naive (tile_h - y_offset) offset with
  AABB-based formula that works regardless of model origin placement
  (feet, geometric centre, etc.):
    model.position.y = (tile_h - y_offset) - scale.y * aabb.position.y

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-16 16:24:09 -04:00
co-authored by Claude Happy
parent 112d471533
commit 58ecd8388c
+27 -22
View File
@@ -512,14 +512,8 @@ func _try_load_tile_model(body: StaticBody3D, affinity: String) -> void:
child.queue_free()
b.add_child(model_node)
# Scale so the XZ footprint fits the 0.98-unit tile square, then lift
# the model so its base sits on the tile's TOP surface.
# _fit_model_to_tile seats the AABB bottom at body-local Y=0, which is
# world Y = tile_h/2 (the tile centre). Shifting by +tile_h/2 moves the
# base up to body-local Y = tile_h/2 = world Y = tile_h (tile top).
# Scale so the XZ footprint fits the 0.98-unit tile square.
_fit_model_to_tile(model_node, 0.98)
var tile_h_m : float = float(b.get_meta("tile_height", 1.0))
model_node.position.y += tile_h_m * 0.5
# Attach a random stalagmite bottom model below the tile (if any are configured).
var bottom_url : String = ContentCache.get_random_tile_bottom_url()
@@ -538,18 +532,26 @@ func _try_load_tile_bottom(body_ref: Variant, url: String) -> void:
return
if bottom_node == null:
return
# The body centre is at world Y = tile_h/2. The bottom of the tile is at
# body-local Y = -tile_h/2 (world Y = 0).
# Fit the model to the tile XZ footprint, then rotate 180° around X
# (equivalent to a Y-flip but preserves mesh normals, unlike scale.y=-1).
# After the rotation the wide base is still at body-local Y=0 and the
# narrow spike is at -scaled_height. Setting position.y = -tile_h/2
# shifts the whole thing so the base lands at the tile bottom (world Y=0)
# and the spike hangs further downward.
var tile_h : float = float(b.get_meta("tile_height", 1.0))
# The body centre is at world Y = tile_h/2.
# The tile underside is at body-local Y = -tile_h/2 (= world Y = 0).
#
# The stalagmite models are spike-at-bottom: the AABB minimum (Y=0 after
# _fit_model_to_tile) is the narrow spike tip; the AABB maximum is the
# wide flat base. No flip is needed — we just need to push the model down
# so the AABB top (wide base) lands exactly at the tile underside.
#
# After _fit_model_to_tile:
# AABB bottom (spike) → body-local Y = 0
# AABB top (base) → body-local Y = scaled_h
# We want:
# AABB top (base) → body-local Y = -tile_h/2
# So shift by -(tile_h/2 + scaled_h):
var tile_h : float = float(b.get_meta("tile_height", 1.0))
var btm_aabb := _model_local_aabb(bottom_node)
var btm_fp := max(btm_aabb.size.x, btm_aabb.size.z)
var scaled_h := btm_aabb.size.y * (0.98 / btm_fp) if btm_fp > 0.0 else 0.0
_fit_model_to_tile(bottom_node, 0.98)
bottom_node.rotate_x(PI) # flip upside-down, normals intact
bottom_node.position.y = -tile_h * 0.5 # seat flipped base at tile bottom
bottom_node.position.y -= tile_h * 0.5 + scaled_h
b.add_child(bottom_node)
)
@@ -780,10 +782,13 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false)
if not _move_anims.has(iid):
b.position = wp + Vector3(0, y_offset, 0)
b.add_child(model_node)
# Seat the model's local-Y=0 (feet) on the tile's top surface.
# The body centre is at tile_h + half_height; tile_h - y_offset = -half_height
# shifts the model so its feet land exactly at world Y = tile_h.
model_node.position.y = tile_h - y_offset
# Seat the model's AABB bottom (feet) on the tile surface (world Y = tile_h).
# Using the AABB makes this work regardless of where the model's local origin
# is — at the feet, at the geometric centre, or anywhere else.
# body-local feet = (tile_h - y_offset) - scale.y * aabb.position.y
var m_aabb := _model_local_aabb(model_node)
if m_aabb.size != Vector3.ZERO:
model_node.position.y = (tile_h - y_offset) - model_node.scale.y * m_aabb.position.y
_unit_meshes[iid] = model_node
# Find and store the AnimationPlayer, then start the Idle loop.