Files
physics/tests/motion2d.sakura
2026-11-14 15:41:09 -06:00

251 lines
11 KiB
Plaintext

#! Contains the tests of how `physics2d` keeps what moves fast from passing
#! through what's thin, and what a space is asked of the shapes in it: gaps
#! between shapes, shapes cast along motions, times of impact, bodies swept
#! through a step, bullets, chains of segments, one-way platforms, and which
#! colliders a shape overlaps or a cast meets.
import std::testing;
import std::containers;
import std::math;
import malunal::linear;
import malunal::physics2d;
def near(a: f32, b: f32, within: f32): bool {
return math::abs(a - b) <= within;
}
def run(s: &mut space, seconds: f32): void {
val n := (seconds * 60) as u32;
for i in 0..n {
s.step(1f / 60f);
}
}
@test def finds_gaps_between_shapes(t: &mut tester): void {
val crate := shape::box(vec2{ 0.5, 0.5 });
# Two boxes side by side, a meter between their faces.
val apart := gap_between(&crate, pose::identity, &crate, pose::of(vec2{ 2, 0.25 }));
t.expects(near(apart.distance, 1, 1e-5) && near(apart.normal.x, 1, 1e-5) && near(apart.on_a.x, 0.5, 1e-5) && near(apart.on_b.x, 1.5, 1e-5));
# A ball off a box's corner: along the diagonal.
val ball := shape::circle(0.25);
val corner := gap_between(&crate, pose::identity, &ball, pose::of(vec2{ 1.5, 1.5 }));
t.expects(near(corner.distance, math::sqrt(2f) - 0.25, 1e-4) && near(corner.normal.x, corner.normal.y, 1e-4));
# A turned box against a capsule, by their radii.
val pill := shape::capsule(vec2{ 0, -1 }, vec2{ 0, 1 }, 0.3);
val turned := gap_between(&pill, pose::of(vec2{ -2, 0 }), &crate, pose::of(vec2::zero, math::pi as f32 / 4));
t.expects(near(turned.distance, 2 - 0.3 - math::sqrt(0.5f), 1e-4) && near(turned.normal.x, 1, 1e-4));
# Overlapping: no distance.
val within := gap_between(&crate, pose::identity, &ball, pose::of(vec2{ 0.3, 0 }));
t.expects(within.distance == 0);
}
@test def casts_shapes_along_motions(t: &mut tester): void {
val wall := shape::box(vec2{ 0.1, 2 });
val ball := shape::circle(0.5);
# A ball cast 10 meters toward a wall 5 meters off meets it halfway, by its
# face facing back.
val hit := shape_cast(&wall, pose::of(vec2{ 5, 0 }), &ball, pose::identity, vec2{ 10, 0 }) else { t.expects(false); return; };
t.expects(near(hit.fraction, (4.9 - 0.5) / 10, 1e-3) && near(hit.normal.x, -1, 1e-4) && near(hit.point.x, 4.9, 1e-3));
# Cast short of it, or away, it meets nothing.
t.expects(!(shape_cast(&wall, pose::of(vec2{ 5, 0 }), &ball, pose::identity, vec2{ 4, 0 }) is some(_)));
t.expects(!(shape_cast(&wall, pose::of(vec2{ 5, 0 }), &ball, pose::identity, vec2{ -10, 0 }) is some(_)));
# Begun within it, it meets it at once.
val inside := shape_cast(&wall, pose::of(vec2{ 5, 0 }), &ball, pose::of(vec2{ 4.8, 0 }), vec2{ 1, 0 }) else { t.expects(false); return; };
t.expects(inside.fraction == 0);
# A box cast down onto a turned box meets its corner.
val crate := shape::box(vec2{ 0.5, 0.5 });
val down := shape_cast(&crate, pose::of(vec2::zero, math::pi as f32 / 4), &crate, pose::of(vec2{ 0, 3 }), vec2{ 0, -3 }) else { t.expects(false); return; };
t.expects(near(3 - down.fraction * 3, math::sqrt(0.5f) + 0.5, 3e-3) && near(down.normal.y, 1, 1e-3));
}
@test def finds_times_of_impact(t: &mut tester): void {
val wall := shape::box(vec2{ 0.05, 2 });
val stick := shape::box(vec2{ 1, 0.05 });
# A stick turning nearly a half turn about its middle, under a shelf off to
# its right, meets it as its end rises to it.
val shelf := shape::box(vec2{ 0.9, 0.02 });
val turning := sweep{ c1 = vec2::zero, c2 = vec2::zero, q1 = rot::identity, q2 = rot::of(math::pi as f32 * 0.99) };
val at := time_of_impact(&shelf, &sweep::still(pose::of(vec2{ 1.1, 0.85 })), &stick, &turning, 0.005, 0.001);
val angle := math::pi as f32 * 0.99 * at;
t.expects(at > 0.2 && at < 0.45 && near(math::sin(angle + 0.05), 0.83, 0.03));
# One moving away never meets it.
val leaving := sweep{ c1 = vec2{ -1.5, 0 }, c2 = vec2{ -3, 0 } };
t.expects(time_of_impact(&wall, &sweep::still(pose::identity), &stick, &leaving, 0.005, 0.001) == 1);
}
# A thin wall, and a ball thrown at it faster than it's thick each step.
def thrown(continuous: bool, bullet: bool): f32 {
var s := space::new(vec2::zero);
s.continuous = continuous;
val wall := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 5.5, 0 }) });
_ = s.attach(wall, collider{ shape = shape::box(vec2{ 0.02, 3 }) });
val ball := s.add(body{ velocity = vec2{ 300, 0 }, bullet = bullet });
_ = s.attach(ball, collider{ shape = shape::circle(0.1) });
run(&mut s, 0.5);
return s.pose_of(ball).at.x;
}
@test def keeps_fast_bodies_from_passing_through(t: &mut tester): void {
# Swept, it stops at the wall; not swept, it passes it.
t.expects(thrown(true, false) < 5.45);
t.expects(thrown(false, false) > 10);
# Dropped flat from high onto a thin floor, a crate lands on it; dropped on
# its corner, it's thrown about, but never through it.
t.expects(near(dropped(0), 0.25, 0.01));
t.expects(dropped(0.3) > 0);
}
# A crate dropped onto a floor as thin as a line at 200 meters a second, turned
# so, and the lowest its middle came in two seconds, or where it ended flat.
def dropped(angle: f32): f32 {
var s := space::new();
val floor := s.add(body{ kind = body_kind::static });
_ = s.attach(floor, collider{ shape = shape::segment(vec2{ -100, 0 }, vec2{ 100, 0 }) });
val crate := s.add(body{ at = pose::of(vec2{ 0, 2 }, angle), velocity = vec2{ 0, -200 } });
_ = s.attach(crate, collider{ shape = shape::box(vec2{ 0.25, 0.25 }) });
var lowest: f32 = 2;
for i in 0..120u {
s.step(1f / 60f);
lowest = math::min(lowest, s.pose_of(crate).at.y);
}
return angle == 0 ? s.pose_of(crate).at.y : lowest;
}
@test def sweeps_bullets_against_moving_bodies(t: &mut tester): void {
var s := space::new(vec2::zero);
val plank := s.add(body{ at = pose::of(vec2{ 5, 0 }) });
_ = s.attach(plank, collider{ shape = shape::box(vec2{ 0.02, 1 }) });
# Not a bullet, a shot passes through a crate; a bullet strikes it, pushing
# it on.
val shot := s.add(body{ velocity = vec2{ 400, 0 }, bullet = true });
_ = s.attach(shot, collider{ shape = shape::circle(0.05), density = 10 });
run(&mut s, 0.25);
t.expects(s.pose_of(shot).at.x < s.pose_of(plank).at.x && s.velocity_of(plank).x > 1);
var t2 := space::new(vec2::zero);
val other := t2.add(body{ at = pose::of(vec2{ 5, 0 }) });
_ = t2.attach(other, collider{ shape = shape::box(vec2{ 0.02, 1 }) });
val passing := t2.add(body{ velocity = vec2{ 400, 0 } });
_ = t2.attach(passing, collider{ shape = shape::circle(0.05), density = 10 });
run(&mut t2, 0.25);
t.expects(t2.pose_of(passing).at.x > 20);
}
# Ground of segments a meter wide, running right to left so they face up.
def chained_ground(s: &mut space, count: u32): void {
var places := vector::<vec2>::new();
var i := count as i64;
while i >= 0 {
places.append(vec2{ i as f32 - 2, 0 });
i--;
}
val ground := s.add(body{ kind = body_kind::static });
_ = s.attach_chain(ground, places.span, false, collider{ friction = 0 });
}
@test def slides_along_chains(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
chained_ground(&mut s, 40);
val crate := s.add(body{ at = pose::of(vec2{ 0, 0.25 }), velocity = vec2{ 5, 0 } });
_ = s.attach(crate, collider{ shape = shape::box(vec2{ 0.25, 0.25 }), friction = 0 });
var lowest: f32 = 1;
var slowest: f32 = 5;
for i in 0..240u {
s.step(1f / 60f);
lowest = math::min(lowest, s.pose_of(crate).at.y);
slowest = math::min(slowest, s.velocity_of(crate).x);
}
# Across the joints it slides as along one floor: never caught, never sunk.
t.expects(slowest > 4.9 && lowest > 0.24 && near(s.pose_of(crate).turn.angle, 0, 1e-3));
# From below, a ball rises through the chain, met only from above.
var up := space::new(vec2{ 0, -1 });
chained_ground(&mut up, 4);
val ball := up.add(body{ at = pose::of(vec2{ 0, -1 }), velocity = vec2{ 0, 4 } });
_ = up.attach(ball, collider{ shape = shape::circle(0.2) });
run(&mut up, 1);
t.expects(up.pose_of(ball).at.y > 0.5);
# A ray up through it passes; one down meets it.
t.expects(!(up.ray_cast(vec2{ 0.5, -2 }, vec2{ 0, 1 }, 4) is some(_)));
t.expects(up.ray_cast(vec2{ 0.5, 2 }, vec2{ 0, -1 }, 4) is some(_));
}
@test def jumps_through_one_way_platforms(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
val ledge := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, 2 }) });
_ = s.attach(ledge, collider{ shape = shape::box(vec2{ 2, 0.1 }), one_way = true });
# Thrown up from below it, a ball passes through, then lands on it.
val ball := s.add(body{ velocity = vec2{ 0, 9 } });
_ = s.attach(ball, collider{ shape = shape::circle(0.25) });
var above := false;
for i in 0..180u {
s.step(1f / 60f);
if s.pose_of(ball).at.y > 2.4 {
above = true;
}
}
t.expects(above && near(s.pose_of(ball).at.y, 2.35, 0.02) && s.velocity_of(ball).length < 0.05);
}
@test def asks_what_shapes_overlap_and_meet(t: &mut tester): void {
var s := space::new();
val ground := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, -1 }) });
_ = s.attach(ground, collider{ shape = shape::box(vec2{ 10, 1 }) });
var cells := grid::new(4, 4);
cells.set(1, 0, true);
val walls := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 6, 0 }) });
_ = s.attach_grid(walls, move(cells), collider{});
val crate := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ -3, 0.5 }) });
val held := s.attach(crate, collider{ shape = shape::box(vec2{ 0.5, 0.5 }), layers = 2 });
# A ball over the crate's corner overlaps it and the ground.
var found: u32 = 0;
var crate_found := false;
s.overlap(&shape::circle(0.3), pose::of(vec2{ -2.4, 0.1 }), 0xFFFFFFFF, def(c: collider_id, b: body_id): bool {
found++;
if c == held {
crate_found = true;
}
return true;
});
t.expects(found == 2 && crate_found);
# Masked from the crate's layer, only the ground.
found = 0;
s.overlap(&shape::circle(0.3), pose::of(vec2{ -2.4, 0.1 }), 1, def(c: collider_id, b: body_id): bool {
found++;
return true;
});
t.expects(found == 1);
# A ball cast right along the ground meets the grid's cell at x 7 to 8.
val ball := shape::circle(0.25);
val hit := s.shape_cast(&ball, pose::of(vec2{ 0, 0.5 }), vec2{ 20, 0 }) else { t.expects(false); return; };
t.expects(near(hit.point.x, 7, 0.01) && near(hit.normal.x, -1, 1e-3) && hit.body == walls);
# Cast left, it meets the crate.
val left := s.shape_cast(&ball, pose::of(vec2{ 0, 0.5 }), vec2{ -20, 0 }) else { t.expects(false); return; };
t.expects(left.collider == held && near(left.point.x, -2.5, 0.01));
}
# A tower of crates and a ball thrown into it, stepped a second on so many
# threads, and where its top crate ends.
def toppled(threads: u32): pose {
var s := space::new();
s.threads = threads;
val ground := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, -1 }) });
_ = s.attach(ground, collider{ shape = shape::box(vec2{ 50, 1 }) });
var top := body_id{};
for i in 0..12u {
for j in 0..3u {
top = s.add(body{ at = pose::of(vec2{ j as f32 * 1.1 - 1.1, 0.5 + i as f32 }) });
_ = s.attach(top, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
}
}
val ball := s.add(body{ at = pose::of(vec2{ -8, 4 }), velocity = vec2{ 20, 0 } });
_ = s.attach(ball, collider{ shape = shape::circle(0.4), density = 5 });
run(&mut s, 1);
return s.pose_of(top);
}
@test def solves_the_same_on_several_threads(t: &mut tester): void {
val one := toppled(1);
val four := toppled(4);
t.expects(one == four && one.at.x != 1.1f);
}