Files
tidewall/test/sim/test_sim_ground.gd
claudeandClaude Opus 5.5 5db53de4e8 W5: Vegetation and harvestable props
PropSet places about 3,000 pines, scrub, boulders, wrecks and crates per
island from the seed, by elevation band with grove noise, clear of points
of interest. Each prop is one collision box in SimGround with health from
Tuning.PROP_KINDS; destroying it removes the box at once
(SimGround.remove_solid). Prop health is part of the state hash.

PropView draws them as one MultiMesh per kind per 128 m square (about 150
instanced draws), with per-kind visibility ranges, procedural flat-shaded
meshes fitted to the collision boxes, and cheap sync of felled props.

Closes #12

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:17:42 -05:00

133 lines
6.3 KiB
GDScript

extends TestCase
func _tilted() -> SimGround:
# h = 0.5 * x, a plane rising along +x.
var values: PackedFloat32Array = PackedFloat32Array()
for z: int in 4:
for x: int in 4:
values.append(0.5 * x * 2.0)
return SimGround.from_heights(values, 4, 2.0)
func test_flat_ground() -> void:
var ground: SimGround = SimGround.flat(3.0)
assert_eq(ground.terrain_height(10.0, 900.0), 3.0)
assert_true(ground.terrain_normal(10.0, 10.0).is_equal_approx(Vector3.UP))
assert_true(ground.walkable_terrain(10.0, 10.0))
func test_interpolation_and_edges() -> void:
var ground: SimGround = _tilted()
assert_near(ground.terrain_height(1.0, 1.0), 0.5, 0.0001)
assert_near(ground.terrain_height(3.0, 5.0), 1.5, 0.0001)
# Outside the grid, the edge sample is used.
assert_near(ground.terrain_height(-5.0, 2.0), 0.0, 0.0001)
assert_near(ground.terrain_height(100.0, 2.0), 3.0, 0.0001)
func test_normal_and_walkability() -> void:
var ground: SimGround = _tilted()
var normal: Vector3 = ground.terrain_normal(3.0, 3.0)
assert_near(rad_to_deg(normal.angle_to(Vector3.UP)), rad_to_deg(atan(0.5)), 0.01)
assert_true(normal.x < 0.0, "the normal leans away from the rise")
assert_true(ground.walkable_terrain(3.0, 3.0))
func test_support_from_solids() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 1.0, 2.0)))
ground.add_solid(AABB(Vector3(0.0, 5.0, 0.0), Vector3(2.0, 1.0, 2.0)))
assert_eq(ground.support_height(1.0, 1.0, 1.2), 1.0)
assert_eq(ground.support_height(1.0, 1.0, 10.0), 6.0)
assert_eq(ground.support_height(1.0, 1.0, 0.5), 0.0)
assert_eq(ground.support_height(3.0, 1.0, 10.0), 0.0)
assert_true(ground.support_is_solid(1.0, 1.0, 1.2))
assert_false(ground.support_is_solid(3.0, 1.0, 1.2))
func test_negative_size_solids_are_normalised() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(2.0, 1.0, 2.0), Vector3(-2.0, -1.0, -2.0)))
assert_eq(ground.support_height(1.0, 1.0, 5.0), 1.0)
func test_raycast_against_a_box() -> void:
var ground: SimGround = SimGround.flat(-100.0)
ground.add_solid(AABB(Vector3(4.0, -1.0, -1.0), Vector3(2.0, 2.0, 2.0)))
assert_near(ground.raycast(Vector3.ZERO, Vector3(10.0, 0.0, 0.0)), 0.4, 0.0001)
assert_eq(ground.raycast(Vector3.ZERO, Vector3(3.0, 0.0, 0.0)), 1.0)
assert_eq(ground.raycast(Vector3(0.0, 5.0, 0.0), Vector3(10.0, 5.0, 0.0)), 1.0)
assert_eq(ground.raycast(Vector3(5.0, 0.0, 0.0), Vector3(10.0, 0.0, 0.0)), 0.0, "starting inside")
func test_raycast_against_terrain() -> void:
var ground: SimGround = SimGround.flat(0.0)
var hit: float = ground.raycast(Vector3(0.0, 2.0, 0.0), Vector3(0.0, -2.0, 0.0))
assert_near(hit, 0.5, 0.001)
assert_eq(ground.raycast(Vector3(0.0, 2.0, 0.0), Vector3(5.0, 1.0, 0.0)), 1.0)
assert_eq(ground.raycast(Vector3(0.0, -1.0, 0.0), Vector3(0.0, 3.0, 0.0)), 0.0, "starting below")
# A ray grazing up a slope finds where it enters.
var tilted: SimGround = _tilted()
var slope_hit: float = tilted.raycast(Vector3(0.0, 2.0, 2.0), Vector3(6.0, 2.0, 2.0))
assert_near(slope_hit * 6.0, 4.0, 0.01)
func test_body_blocked_respects_step_and_height() -> void:
var ground: SimGround = SimGround.flat()
ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 0.4, 2.0)))
ground.add_solid(AABB(Vector3(10.0, 2.0, 0.0), Vector3(2.0, 1.0, 2.0)))
assert_false(ground.body_blocked(Vector3(1.0, 0.0, 1.0), 0.4, 1.8, 0.45), "low step")
assert_true(ground.body_blocked(Vector3(1.0, 0.0, 1.0), 0.4, 1.8, 0.0))
assert_true(ground.body_blocked(Vector3(2.3, 0.0, 1.0), 0.4, 1.8, 0.0), "radius overlaps")
assert_false(ground.body_blocked(Vector3(2.5, 0.0, 1.0), 0.4, 1.8, 0.0))
assert_false(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 1.8, 0.0), "fits under")
assert_true(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 2.5, 0.0))
func test_index_finds_solids_across_cells() -> void:
var ground: SimGround = SimGround.flat()
# Spans several index cells, including negative ones.
ground.add_solid(AABB(Vector3(-5.0, 0.0, -5.0), Vector3(30.0, 2.0, 3.0)))
assert_eq(ground.support_height(20.0, -4.0, 5.0), 2.0)
assert_eq(ground.support_height(-4.0, -4.0, 5.0), 2.0)
assert_eq(ground.support_height(20.0, 4.0, 5.0), 0.0)
assert_true(ground.body_blocked(Vector3(12.0, 0.0, -1.7), 0.4, 1.8, 0.0), "radius reaches into the next cell")
# The box's far side from the ray's start is at z = -2, 22 m along a 40 m ray.
assert_near(ground.raycast(Vector3(15.0, 1.0, 20.0), Vector3(15.0, 1.0, -20.0)), 22.0 / 40.0, 0.0001)
func test_index_gives_the_same_answers_as_a_full_scan() -> void:
var ground: SimGround = SimGround.flat()
var random: RandomNumberGenerator = RandomNumberGenerator.new()
random.seed = 5
for _index: int in 200:
var at: Vector3 = Vector3(random.randf_range(0.0, 100.0), random.randf_range(0.0, 4.0), random.randf_range(0.0, 100.0))
ground.add_solid(AABB(at, Vector3(random.randf_range(0.2, 12.0), random.randf_range(0.2, 3.0), random.randf_range(0.2, 12.0))))
for _index: int in 500:
var x: float = random.randf_range(0.0, 100.0)
var z: float = random.randf_range(0.0, 100.0)
var expected: float = 0.0
for box: AABB in ground.solids:
if box.end.y <= 6.0 and x > box.position.x and x < box.end.x and z > box.position.z and z < box.end.z:
expected = maxf(expected, box.end.y)
assert_eq(ground.support_height(x, z, 6.0), expected)
func test_removed_solids_vanish_from_every_query() -> void:
var ground: SimGround = SimGround.flat()
var keep: int = ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 1.0, 2.0)))
var gone: int = ground.add_solid(AABB(Vector3(10.0, 0.0, 10.0), Vector3(20.0, 3.0, 2.0)))
assert_eq(gone, keep + 1)
assert_true(ground.body_blocked(Vector3(15.0, 0.0, 11.0), 0.4, 1.8, 0.0))
assert_true(ground.remove_solid(gone))
assert_false(ground.remove_solid(gone), "removing twice reports false")
assert_false(ground.remove_solid(99), "unknown IDs report false")
assert_false(ground.body_blocked(Vector3(15.0, 0.0, 11.0), 0.4, 1.8, 0.0))
assert_eq(ground.support_height(25.0, 11.0, 10.0), 0.0)
assert_eq(ground.raycast(Vector3(5.0, 1.0, 11.0), Vector3(35.0, 1.0, 11.0)), 1.0)
assert_false(ground.point_inside(Vector3(15.0, 1.0, 11.0)))
# The other solid keeps its ID and still collides, and new IDs never reuse old ones.
assert_eq(ground.support_height(1.0, 1.0, 5.0), 1.0)
assert_eq(ground.add_solid(AABB(Vector3(40.0, 0.0, 40.0), Vector3.ONE)), gone + 1)