#! Contains the tests of `physics2d`: rotations and poses; shapes, their boxes, #! and what they hold; manifolds between circles, capsules, and polygons, a #! box resting on another at two points, a corner at one; rays meeting each #! shape; a tree of boxes, sound as it's changed, finding what a brute search #! finds; and a grid of solid cells, met but for the faces they share, and #! rays passing through it. import std::testing; import std::containers; import std::math; import malunal::linear; import malunal::physics2d; # Whether two numbers are within a hundred-thousandth or so of one another. def near(a: f32, b: f32): bool { return math::abs(a - b) < 1e-4; } def near(a: vec2, b: vec2): bool { return near(a.x, b.x) && near(a.y, b.y); } @test def turns_and_places_by_poses(t: &mut tester): void { val quarter := rot::of(math::pi as f32 / 2); t.expects(near(quarter.apply(vec2{ 1, 0 }), vec2{ 0, 1 }) && near(quarter.unapply(vec2{ 0, 1 }), vec2{ 1, 0 })); t.expects(near(quarter.angle, math::pi as f32 / 2) && near((quarter * quarter).apply(vec2{ 1, 0 }), vec2{ -1, 0 })); val at := pose::of(vec2{ 3, 1 }, math::pi as f32 / 2); t.expects(near(at.apply(vec2{ 1, 0 }), vec2{ 3, 2 }) && near(at.unapply(vec2{ 3, 2 }), vec2{ 1, 0 })); # A pose within another, and found again within it. val inner := pose::of(vec2{ -2, 0.5 }, 0.7); val both := at * inner; t.expects(near(both.apply(vec2{ 1, 1 }), at.apply(inner.apply(vec2{ 1, 1 })))); val found := at.unapply_pose(both); t.expects(near(found.at, inner.at) && near(found.turn.angle, 0.7)); } @test def makes_shapes(t: &mut tester): void { # A box's corners counterclockwise from its bottom left, each edge's normal # facing out. val box := shape::box(vec2{ 2, 1 }); t.expects(box.kind == shape_kind::polygon && box.count == 4 && near(box.point(0), vec2{ -2, -1 }) && near(box.point(2), vec2{ 2, 1 })); t.expects(near(box.normal(0), vec2{ 0, -1 }) && near(box.normal(1), vec2{ 1, 0 }) && near(box.normal(2), vec2{ 0, 1 }) && near(box.normal(3), vec2{ -1, 0 })); # A polygon around places leaves out what's within and along its edges. val places: vec2[7] = [vec2{ 0, 0 }, vec2{ 2, 0 }, vec2{ 1, 0 }, vec2{ 2, 2 }, vec2{ 1, 1 }, vec2{ 0, 2 }, vec2{ 0, 2 }]; val square := shape::polygon(places[:]).expect("A square is made."); t.expects(square.count == 4 && square.contains(pose::identity, vec2{ 1, 1 }) && !square.contains(pose::identity, vec2{ 2.1, 1 })); # A line holds no area, and nine corners are too many. val line: vec2[3] = [vec2{ 0, 0 }, vec2{ 1, 1 }, vec2{ 2, 2 }]; t.expects(shape::polygon(line[:]) is none); var round := vector::::new(); for i in 0..9u { round.append(rot::of(i as f32 * 2 * math::pi as f32 / 9).apply(vec2{ 1, 0 })); } t.expects(shape::polygon(round.view()) is none); # A capsule of one place is a circle; a pill stands up. t.expects(shape::capsule(vec2{ 1, 1 }, vec2{ 1, 1 }, 0.5).kind == shape_kind::circle); val pill := shape::pill(1, 3); t.expects(pill.kind == shape_kind::capsule && near(pill.point(0), vec2{ 0, -1 }) && near(pill.point(1), vec2{ 0, 1 }) && near(pill.radius, 0.5)); # The box holding a shape where a pose places it. val turned := box.bounds(pose::of(vec2{ 10, 0 }, math::pi as f32 / 2)); t.expects(near(turned.min, vec2{ 9, -2 }) && near(turned.max, vec2{ 11, 2 })); val held := pill.bounds(pose::of(vec2{ 0, 5 })); t.expects(near(held.min, vec2{ -0.5, 3.5 }) && near(held.max, vec2{ 0.5, 6.5 })); t.expects(pill.contains(pose::identity, vec2{ 0.4, 1.2 }) && !pill.contains(pose::identity, vec2{ 0.45, 1.4 })); } @test def collides_circles_and_hulls(t: &mut tester): void { val ball := shape::circle(0.5); val other := shape::circle(1); val met := collide(&ball, pose::identity, &other, pose::of(vec2{ 1.2, 0 })).expect("They meet."); t.expects(met.count == 1 && near(met.normal, vec2{ 1, 0 }) && near(met.points[0].depth, 0.3) && near(met.points[0].point, vec2{ 0.35, 0 })); t.expects(collide(&ball, pose::identity, &other, pose::of(vec2{ 1.6, 0 })) is none); # Touching, they meet, at a depth of 0. t.expects(collide(&ball, pose::identity, &other, pose::of(vec2{ 1.5, 0 })) is some(m) && near(m.depth, 0)); # A ball over a box's face, by its corner, and within it. val slab := shape::box(vec2{ 1, 0.5 }); val above := collide(&slab, pose::identity, &ball, pose::of(vec2{ 0, 0.9 })).expect("It rests on the box."); t.expects(near(above.normal, vec2{ 0, 1 }) && near(above.depth, 0.1) && near(above.points[0].point, vec2{ 0, 0.45 })); val corner := collide(&slab, pose::identity, &ball, pose::of(vec2{ 1.3, 0.8 })).expect("It meets the corner."); val diagonal := math::sqrt(0.5f); t.expects(near(corner.normal, vec2{ diagonal, diagonal }) && near(corner.depth, 0.5 - math::sqrt(0.18f))); val small := shape::circle(0.1); val within := collide(&slab, pose::identity, &small, pose::of(vec2{ 0.8, 0 })).expect("It's within the box."); t.expects(near(within.normal, vec2{ 1, 0 }) && near(within.depth, 0.3)); # The other way around, the normal is turned around. val back := collide(&small, pose::of(vec2{ 0.8, 0 }), &slab, pose::identity).expect("It's within the box."); t.expects(near(back.normal, vec2{ -1, 0 }) && near(back.depth, 0.3)); # A capsule and a ball meet as the ball nears its segment. val pill := shape::capsule(vec2{ -1, 0 }, vec2{ 1, 0 }, 0.25); val side := collide(&pill, pose::identity, &ball, pose::of(vec2{ 0.3, 0.7 })).expect("It meets the capsule's side."); t.expects(near(side.normal, vec2{ 0, 1 }) && near(side.depth, 0.05)); } @test def collides_hulls_at_one_point_or_two(t: &mut tester): void { # A box resting flat on another meets it at two points, midway between their # surfaces. val slab := shape::box(vec2{ 1, 0.5 }); val crate := shape::box(vec2{ 0.5, 0.5 }); val resting := collide(&slab, pose::identity, &crate, pose::of(vec2{ 0, 0.9 })).expect("The crate rests on the slab."); t.expects(resting.count == 2 && near(resting.normal, vec2{ 0, 1 })); t.expects(near(resting.points[0].depth, 0.1) && near(resting.points[1].depth, 0.1)); t.expects(near(resting.points[0].point.y, 0.45) && near(math::abs(resting.points[0].point.x), 0.5) && near(resting.points[0].point.x, -resting.points[1].point.x)); t.expects(collide(&slab, pose::identity, &crate, pose::of(vec2{ 0, 1.1 })) is none); val flipped := collide(&crate, pose::of(vec2{ 0, 0.9 }), &slab, pose::identity).expect("The slab is under the crate."); t.expects(flipped.count == 2 && near(flipped.normal, vec2{ 0, -1 })); # A crate turned onto its corner meets the slab at the corner alone. val tipped := collide(&slab, pose::identity, &crate, pose::of(vec2{ 0, 1.2 }, math::pi as f32 / 4)).expect("Its corner meets the slab."); t.expects(tipped.count == 1 && near(tipped.normal, vec2{ 0, 1 }) && near(tipped.depth, 0.5 * math::sqrt(2f) - 0.7)); # A capsule lying on the slab meets it at two points; one standing by its # corner, at one, along the line from the corner to its end. val lying := shape::capsule(vec2{ -0.5, 0 }, vec2{ 0.5, 0 }, 0.5); val flat := collide(&slab, pose::identity, &lying, pose::of(vec2{ 0, 0.9 })).expect("The capsule lies on the slab."); t.expects(flat.count == 2 && near(flat.normal, vec2{ 0, 1 }) && near(flat.points[0].depth, 0.1) && near(flat.points[0].point.y, 0.45)); val standing := shape::capsule(vec2{ 0, 0 }, vec2{ 0, 1 }, 0.5); val by := collide(&slab, pose::identity, &standing, pose::of(vec2{ 1.3, 0.8 })).expect("The capsule's end meets the corner."); val diagonal := math::sqrt(0.5f); t.expects(by.count == 1 && near(by.normal, vec2{ diagonal, diagonal }) && near(by.depth, 0.5 - math::sqrt(0.18f))); t.expects(!overlaps(&slab, pose::identity, &standing, pose::of(vec2{ 1.4, 0.9 }))); # Two capsules side by side, and crossing. val beside := collide(&lying, pose::identity, &lying, pose::of(vec2{ 0, 0.8 })).expect("They lie together."); t.expects(near(beside.normal, vec2{ 0, 1 }) && near(beside.depth, 0.2)); t.expects(overlaps(&lying, pose::identity, &lying, pose::of(vec2{ 0, 0 }, 1.2))); # A segment, of no thickness, across a box. val wire := shape::segment(vec2{ -2, 0 }, vec2{ 2, 0 }); t.expects(overlaps(&wire, pose::identity, &crate, pose::of(vec2{ 0, 0.3 })) && !overlaps(&wire, pose::identity, &crate, pose::of(vec2{ 0, 0.6 }))); } @test def casts_rays_at_shapes(t: &mut tester): void { val up := vec2{ 0, 1 }; val ball := shape::circle(1); val struck := ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2::zero, vec2{ 1, 0 }, 10).expect("It meets the ball."); t.expects(near(struck.distance, 2) && near(struck.point, vec2{ 2, 0 }) && near(struck.normal, vec2{ -1, 0 })); t.expects(ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2::zero, up, 10) is none); t.expects(ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2::zero, vec2{ 1, 0 }, 1.5) is none); val inside := ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2{ 3, 0 }, up, 10).expect("It begins within the ball."); t.expects(inside.distance == 0 && near(inside.normal, vec2{ 0, -1 })); # A box, turned a quarter, its long side up and down. val box := shape::box(vec2{ 1, 0.5 }); val roof := box.ray_cast(pose::of(vec2{ 0, 5 }, math::pi as f32 / 2), vec2::zero, up, 10).expect("It meets the box."); t.expects(near(roof.distance, 4) && near(roof.normal, vec2{ 0, -1 }) && near(roof.point, vec2{ 0, 4 })); t.expects(box.ray_cast(pose::of(vec2{ 0, 5 }), vec2::zero, up, 4) is none); t.expects(box.ray_cast(pose::of(vec2{ 0, 5 }), vec2{ 2, 0 }, up, 10) is none); # A capsule, by its side and its round end. val pill := shape::capsule(vec2{ -1, 3 }, vec2{ 1, 3 }, 0.5); val side := pill.ray_cast(pose::identity, vec2::zero, up, 10).expect("It meets the capsule's side."); t.expects(near(side.distance, 2.5) && near(side.normal, vec2{ 0, -1 })); val end := pill.ray_cast(pose::identity, vec2{ 3, 3 }, vec2{ -1, 0 }, 10).expect("It meets the capsule's end."); t.expects(near(end.distance, 1.5) && near(end.normal, vec2{ 1, 0 })); # A segment, from either side. val wire := shape::segment(vec2{ -1, 2 }, vec2{ 1, 2 }); val from_below := wire.ray_cast(pose::identity, vec2::zero, up, 10).expect("It meets the segment from below."); val from_above := wire.ray_cast(pose::identity, vec2{ 0, 5 }, vec2{ 0, -1 }, 10).expect("It meets the segment from above."); t.expects(near(from_below.distance, 2) && near(from_below.normal, vec2{ 0, -1 }) && near(from_above.distance, 3) && near(from_above.normal, up)); } # Numbers from 0 to 1, each of the last, as an LCG makes them. struct dice { var state: u64; } def dice::next(&mut this): f32 { this.state = math::wrapping_add(math::wrapping_mul(this.state, 6364136223846793005), 1442695040888963407); return (this.state >> 40) as f32 / (1u << 24) as f32; } # A box somewhere within 100 of the origin, as large as 3. def some_box(seed: &mut dice): aabb { val at := vec2{ seed.next() * 200 - 100, seed.next() * 200 - 100 }; return aabb::around(at, vec2{ 0.1 + seed.next() * 1.4, 0.1 + seed.next() * 1.4 }); } @test def sorts_boxes_in_a_tree(t: &mut tester): void { var seed := dice{ state = 7 }; var tree := aabb_tree::new(0.25); var boxes := vector::::new(); var leaves := vector::::new(); for i in 0..500u { val box := some_box(&mut seed); boxes.append(box); leaves.append(tree.insert(box, i)); } t.expects(tree.length == 500 && tree.is_sound() && tree.height <= 18); # Moved a little, a leaf stays; moved further, it's put again. Half are # removed. t.expects(!tree.move(leaves[0], aabb{ boxes[0].min + vec2{ 0.1, 0 }, boxes[0].max + vec2{ 0.1, 0 } })); for i in 0..500u { if i % 2 == 0 { boxes[i] = some_box(&mut seed); _ = tree.move(leaves[i], boxes[i]); } else if i % 4 == 1 { tree.remove(leaves[i]); } } t.expects(tree.length == 375 && tree.is_sound()); # What a box meets is what a brute search finds, of the leaves' grown boxes. for k in 0..20u { val asked := aabb::around(vec2{ seed.next() * 200 - 100, seed.next() * 200 - 100 }, vec2{ 8, 8 }); var found := vector::::new(); tree.query(asked, def(leaf: u32, data: u64): bool { found.append(data); return true; }); var expected: u64 = 0; for i in 0..500u { if i % 4 != 1 && tree.box_of(leaves[i]).overlaps(asked) { expected++; } } t.expects(found.length == expected); } # The nearest box along a ray, the search narrowing as each is met. val from := vec2{ -120, 3 }; val way := vec2{ 1, 0.1 }.normalized(); var nearest: f32 = 1000; tree.ray_cast(from, way, 1000, def(leaf: u32, data: u64, reach: f32): f32 { if boxes[data].ray_hit(from, way, reach) is some(at) { nearest = math::min(nearest, at); return at; } return reach; }); var expected: f32 = 1000; for i in 0..500u { if i % 4 != 1 && boxes[i].ray_hit(from, way, 1000) is some(at) { expected = math::min(expected, at); } } t.expects(near(nearest, expected) && expected < 1000); for i in 0..500u { if i % 4 != 1 { tree.remove(leaves[i]); } } t.expects(tree.length == 0 && tree.is_sound() && tree.height == 0); } # Six cells across and four up: a floor along the bottom, and a wall up the # right side. def walled(): grid { var made := grid::new(6, 4); for x in 0..6u { made.set(x as u32, 0, true); } for y in 0..4u { made.set(5, y as u32, true); } return made; } # The manifolds a shape meets a grid with. def met(g: &grid, at: pose, s: &shape, at_s: pose): vector { var found := vector::::new(); g.collide(at, s, at_s, def(x: u32, y: u32, m: manifold): bool { found.append(m); return true; }); return found; } @test def meets_a_grid_of_solid_cells(t: &mut tester): void { val g := walled(); t.expects(g.is_solid(2, 0) && !g.is_solid(2, 1) && g.is_solid(5, 3) && !g.is_solid(-1, 0) && !g.is_solid(6, 0)); val crate := shape::box(vec2{ 0.4, 0.4 }); # On the floor, across two cells, pushed up by both. val resting := met(&g, pose::identity, &crate, pose::of(vec2{ 2, 1.35 })); t.expects(resting.length == 2 && near(resting[0].normal, vec2{ 0, 1 }) && near(resting[1].normal, vec2{ 0, 1 }) && near(resting[0].depth, 0.05)); # Sunk into the floor and just over a seam: the cell past the seam would # push it back across the floor, along the face two cells share, so it's # passed over. val sunk := met(&g, pose::identity, &crate, pose::of(vec2{ 1.61, 0.75 })); t.expects(sunk.length == 1 && near(sunk[0].normal, vec2{ 0, 1 }) && near(sunk[0].depth, 0.65)); # Against the wall, pushed back from it. val leaning := met(&g, pose::identity, &crate, pose::of(vec2{ 4.65, 2 })); t.expects(leaning.length == 2 && near(leaning[0].normal, vec2{ -1, 0 }) && near(leaning[0].depth, 0.05)); t.expects(g.overlaps(pose::identity, &crate, pose::of(vec2{ 4.65, 2 })) && !g.overlaps(pose::identity, &crate, pose::of(vec2{ 2, 2 }))); # The grid placed elsewhere is met there. val moved := pose::of(vec2{ 10, 0 }); t.expects(met(&g, moved, &crate, pose::of(vec2{ 12, 1.35 })).length == 2 && met(&g, moved, &crate, pose::of(vec2{ 2, 1.35 })).is_empty); # A ray down to the floor, through the cells above it. val down := vec2{ 1, -1 }.normalized(); val floor := g.ray_cast(pose::identity, vec2{ 0.3, 3.5 }, down, 10).expect("It meets the floor."); t.expects(floor.x == 2 && floor.y == 0 && near(floor.hit.distance, 2.5 * math::sqrt(2f)) && near(floor.hit.normal, vec2{ 0, 1 })); t.expects(near(floor.hit.point, vec2{ 2.8, 1 })); # From outside the grid, into the wall. val wall := g.ray_cast(pose::identity, vec2{ -2, 2.5 }, vec2{ 1, 0 }, 10).expect("It meets the wall."); t.expects(wall.x == 5 && wall.y == 2 && near(wall.hit.distance, 7) && near(wall.hit.normal, vec2{ -1, 0 })); t.expects(g.ray_cast(pose::identity, vec2{ -2, 2.5 }, vec2{ 1, 0 }, 6.5) is none); t.expects(g.ray_cast(pose::identity, vec2{ 1, 2 }, vec2{ 0, 1 }, 10) is none); val within := g.ray_cast(pose::identity, vec2{ 5.5, 2.5 }, vec2{ -1, 0 }, 10).expect("It begins in the wall."); t.expects(within.hit.distance == 0 && within.x == 5 && within.y == 2); val there := g.ray_cast(moved, vec2{ 8, 2.5 }, vec2{ 1, 0 }, 10).expect("It meets the moved wall."); t.expects(near(there.hit.distance, 7) && near(there.hit.point, vec2{ 15, 2.5 })); }