Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-14 15:41:09 -06:00
commit 11d94dba85
84 files changed
+18825

No files matched your search

+359
View File
@@ -0,0 +1,359 @@
#! Contains the tests of `physics2d`'s space: shapes' masses; a box dropped
#! onto the ground, coming to rest on it; a tower of boxes standing still;
#! bodies sleeping and waking; bouncing and sliding by restitution and
#! friction; layers, sensors, and what began and stopped touching; kinematic
#! bodies carrying what rests on them; joints holding bodies; and rays.
import std::testing;
import std::containers;
import std::math;
import malunal::linear;
import malunal::physics2d;
def close(a: f32, b: f32, within: f32): bool {
return math::abs(a - b) <= within;
}
# A space with a ground whose top is at y = 0, wide and flat.
def grounded(s: &mut space): body_id {
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 }) });
return ground;
}
def steps(s: &mut space, seconds: f32): void {
val n := (seconds * 60) as u32;
for i in 0..n {
s.step(1f / 60f);
}
}
@test def finds_masses(t: &mut tester): void {
val box := shape::box(vec2{ 1, 0.5 }).mass(1);
t.expects(close(box.mass, 2, 1e-5) && close(box.center.length, 0, 1e-5) && close(box.inertia, 2f * 5f / 12f, 1e-5));
val ball := shape::circle(1, vec2{ 2, 0 }).mass(2);
t.expects(close(ball.mass, 2 * math::pi as f32, 1e-5) && close(ball.center.x, 2, 1e-5) && close(ball.inertia, math::pi as f32, 1e-5));
# A capsule as thin as a rod: its round ends weigh nearly nothing.
val rod := shape::capsule(vec2{ -1, 0 }, vec2{ 1, 0 }, 0.001).mass(1);
t.expects(close(rod.mass, 0.004, 1e-4) && close(rod.inertia, rod.mass * 4 / 12, 1e-5));
# A box moved off its body's origin weighs the same, about its own center.
val off := shape::box(vec2{ 1, 0.5 }, vec2{ 3, 1 }).mass(1);
t.expects(close(off.mass, 2, 1e-5) && close(off.center.x, 3, 1e-5) && close(off.center.y, 1, 1e-5) && close(off.inertia, box.inertia, 1e-4));
# Two shapes together, about their joint center.
val both := box.add(&off);
t.expects(close(both.mass, 4, 1e-5) && close(both.center.x, 1.5, 1e-5) && close(both.inertia, 2 * box.inertia + 4 * (1.5 * 1.5 + 0.25), 1e-3));
}
@test def drops_a_box_onto_the_ground(t: &mut tester): void {
var s := space::new();
_ = grounded(&mut s);
val crate := s.add(body{ at = pose::of(vec2{ 0, 4 }) });
_ = s.attach(crate, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
t.expects(close(s.mass_of(crate).mass, 1, 1e-5));
var began: u32 = 0;
for i in 0..120u {
s.step(1f / 60f);
for e in s.events() {
if e.began {
began++;
}
}
}
# It rests on the ground, within the slop contacts allow, and still.
val at := s.pose_of(crate);
t.expects(close(at.at.y, 0.5, 0.01) && close(at.at.x, 0, 1e-3) && close(at.turn.angle, 0, 1e-3));
t.expects(s.velocity_of(crate).length < 0.01 && began == 1 && s.contact_count == 1);
# Touching the ground still, as the space visits it.
var touching: u32 = 0;
s.each_touching(def(x: collider_id, y: collider_id, sensor: bool): bool {
touching++;
return !sensor;
});
t.expects(touching == 1);
}
@test def stands_a_tower_still(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
_ = grounded(&mut s);
var top := body_id{};
for i in 0..10u {
top = s.add(body{ at = pose::of(vec2{ 0, 0.5 + i as f32 }) });
_ = s.attach(top, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
}
steps(&mut s, 5);
val at := s.pose_of(top);
t.expects(close(at.at.x, 0, 0.01) && close(at.at.y, 9.5, 0.05) && close(at.turn.angle, 0, 0.01));
t.expects(s.velocity_of(top).length < 0.01);
}
@test def sleeps_and_wakes(t: &mut tester): void {
var s := space::new();
_ = grounded(&mut s);
val lower := s.add(body{ at = pose::of(vec2{ 0, 0.5 }) });
_ = s.attach(lower, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
val upper := s.add(body{ at = pose::of(vec2{ 0, 1.5 }) });
_ = s.attach(upper, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
val apart := s.add(body{ at = pose::of(vec2{ 10, 0.5 }) });
_ = s.attach(apart, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
steps(&mut s, 2);
t.expects(!s.is_awake(lower) && !s.is_awake(upper) && !s.is_awake(apart));
# A blow wakes what it strikes, and what it rests with, but not what's apart.
s.apply_impulse(upper, vec2{ 0.1, 0 }, s.center_of(upper));
t.expects(s.is_awake(lower) && s.is_awake(upper) && !s.is_awake(apart));
steps(&mut s, 2);
t.expects(!s.is_awake(upper));
# What rested on a body taken away wakes, and falls.
s.remove(lower);
t.expects(s.is_awake(upper) && !s.has(lower));
steps(&mut s, 1);
t.expects(close(s.pose_of(upper).at.y, 0.5, 0.02));
}
@test def pulls_bodies_by_gravities_of_their_own(t: &mut tester): void {
# A space of no gravity, but a world in its middle pulling a rock toward
# its middle wherever the rock is.
var s := space::new(vec2::zero);
val world := s.add(body{ kind = body_kind::static });
_ = s.attach(world, collider{ shape = shape::circle(10) });
val rock := s.add(body{ at = pose::of(vec2{ 14, 0 }), gravity = vec2{ -10, 0 } });
_ = s.attach(rock, collider{ shape = shape::circle(0.5) });
for i in 0..240u {
s.set_gravity(rock, s.center_of(rock).normalized() * -10);
s.step(1f / 60f);
}
# It fell onto the world's side, and rests there.
t.expects((s.center_of(rock) - vec2{ 10.5, 0 }).length < 0.05 && (s.gravity_of(rock) - vec2{ -10, 0 }).length < 0.01);
steps(&mut s, 2);
t.expects(!s.is_awake(rock));
# Its pull found again, as it was, leaves it asleep; turned away, it wakes
# it, and the rock falls away.
s.set_gravity(rock, vec2{ -10.001, 0 });
t.expects(!s.is_awake(rock));
s.set_gravity(rock, vec2{ 10, 0 });
steps(&mut s, 1);
t.expects(s.center_of(rock).x > 13);
}
@test def bounces_and_slides(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
_ = grounded(&mut s);
# A ball of restitution 1 bounces back about as high as it fell.
val ball := s.add(body{ at = pose::of(vec2{ 0, 2.5 }) });
_ = s.attach(ball, collider{ shape = shape::circle(0.5), restitution = 1 });
var highest: f32 = 0;
var fell := false;
for i in 0..90u {
s.step(1f / 60f);
val y := s.pose_of(ball).at.y;
if y < 0.6 {
fell = true;
}
if fell {
highest = math::max(highest, y);
}
}
t.expects(fell && close(highest, 2.5, 0.15));
# On a slope of 30°, a box of friction 1 stays, and of none slides.
var hill := space::new();
hill.sleeping = false;
val slope := hill.add(body{ kind = body_kind::static, at = pose::of(vec2::zero, math::pi as f32 / 6) });
_ = hill.attach(slope, collider{ shape = shape::box(vec2{ 20, 0.5 }) });
val up := rot::of(math::pi as f32 / 6).apply(vec2{ 0, 1 });
val sticky := hill.add(body{ at = pose::of(up * 1.0, math::pi as f32 / 6) });
_ = hill.attach(sticky, collider{ shape = shape::box(vec2{ 0.5, 0.5 }), friction = 1 });
val slick := hill.add(body{ at = pose::of(up * 1.0 + rot::of(math::pi as f32 / 6).apply(vec2{ 5, 0 }), math::pi as f32 / 6) });
_ = hill.attach(slick, collider{ shape = shape::box(vec2{ 0.5, 0.5 }), friction = 0 });
steps(&mut hill, 1);
t.expects(hill.velocity_of(sticky).length < 0.05 && close(hill.velocity_of(slick).length, 5, 0.25));
}
@test def meets_by_layers_and_sensors(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
_ = grounded(&mut s);
# On a layer the ground's mask doesn't hold, it falls through.
val ghost := s.add(body{ at = pose::of(vec2{ -5, 1 }) });
_ = s.attach(ghost, collider{ shape = shape::box(vec2{ 0.5, 0.5 }), layers = 2, mask = 2 });
# A sensor notes what falls through it.
val zone := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 5, 4 }) });
val field := s.attach(zone, collider{ shape = shape::box(vec2{ 1, 1 }), sensor = true });
val stone := s.add(body{ at = pose::of(vec2{ 5, 7 }) });
val heavy := s.attach(stone, collider{ shape = shape::circle(0.25) });
var entered := false;
var left := false;
for i in 0..120u {
s.step(1f / 60f);
for e in s.events() {
if e.sensor && ((e.a == field && e.b == heavy) || (e.a == heavy && e.b == field)) {
entered = entered || e.began;
left = left || !e.began;
}
}
}
t.expects(s.pose_of(ghost).at.y < -5 && entered && left && close(s.pose_of(stone).at.y, 0.25, 0.01));
}
@test def senses_what_moves_and_filters_queries(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
# A sensor standing still notes a kinematic body moving through it, as a coin
# notes a character walking, but never what stands still beside it.
val coin := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, 0 }) });
val field := s.attach(coin, collider{ shape = shape::circle(0.5), sensor = true, data = 7 });
val post := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0.5, 0 }) });
val pole := s.attach(post, collider{ shape = shape::box(vec2{ 0.25, 0.25 }), data = 8 });
val walker := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec2{ -3, 0 }), velocity = vec2{ 3, 0 } });
val feet := s.attach(walker, collider{ shape = shape::circle(0.25), data = 9 });
var walked_in := false;
var walked_out := false;
var post_met := false;
for i in 0..120u {
s.step(1f / 60f);
for e in s.events() {
val of_field := e.a == field || e.b == field;
if of_field && (e.a == feet || e.b == feet) {
walked_in = walked_in || e.began;
walked_out = walked_out || !e.began;
}
post_met = post_met || (of_field && (e.a == pole || e.b == pole));
}
}
t.expects(walked_in && walked_out && !post_met);
# Queries pass over sensors unless asked of them, and the asker's own.
val probe := shape::circle(0.1);
var met: u32 = 0;
s.overlap(&probe, pose::identity, 0xFFFFFFFF, def(c: collider_id, b: body_id): bool { met++; return true; });
t.expects(met == 0);
s.overlap(&probe, pose::identity, query_filter{ sensors = true }, def(c: collider_id, b: body_id): bool { met++; return true; });
t.expects(met == 1);
t.expects(s.ray_cast(vec2{ -2, 0 }, vec2{ 1, 0 }, 10) is some(&hit) && hit.collider == pole);
t.expects(s.ray_cast(vec2{ -2, 0 }, vec2{ 1, 0 }, 10, query_filter{ sensors = true }) is some(&hit) && hit.collider == field);
t.expects(s.ray_cast(vec2{ -2, 0 }, vec2{ 1, 0 }, 10, query_filter{ ignore = 8 }) is some(&hit) && hit.collider == feet);
}
@test def carries_what_rests_on_a_kinematic_body(t: &mut tester): void {
var s := space::new();
_ = grounded(&mut s);
val lift := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec2{ 0, 2 }) });
_ = s.attach(lift, collider{ shape = shape::box(vec2{ 2, 0.25 }) });
val crate := s.add(body{ at = pose::of(vec2{ 0, 2.75 }) });
_ = s.attach(crate, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
steps(&mut s, 1);
s.set_velocity(lift, vec2{ 1, 0 });
steps(&mut s, 1);
# The lift moves as set, and carries the crate, by friction, along.
t.expects(close(s.pose_of(lift).at.x, 1, 0.02) && close(s.pose_of(lift).at.y, 2, 1e-4));
t.expects(close(s.pose_of(crate).at.x, 1, 0.1) && close(s.velocity_of(crate).x, 1, 0.1));
}
@test def holds_bodies_by_joints(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
val ground := grounded(&mut s);
# A pendulum swings about its pin, its bob kept as far from it.
val bob := s.add(body{ at = pose::of(vec2{ 3, 5 }) });
_ = s.attach(bob, collider{ shape = shape::circle(0.25) });
val pin := s.pin(ground, bob, vec2{ 0, 5 });
var farthest: f32 = 0;
var nearest: f32 = 10;
for i in 0..120u {
s.step(1f / 60f);
val reach := (s.pose_of(bob).at - vec2{ 0, 5 }).length;
farthest = math::max(farthest, reach);
nearest = math::min(nearest, reach);
}
t.expects(close(farthest, 3, 0.02) && close(nearest, 3, 0.02) && s.has(pin));
# A wheel on an axle, turned by a motor at its speed.
val wheel := s.add(body{ at = pose::of(vec2{ 10, 3 }), gravity_scale = 0 });
_ = s.attach(wheel, collider{ shape = shape::circle(0.5) });
val axle := s.pin(ground, wheel, vec2{ 10, 3 });
var turning := s.joint_of(axle);
turning.motor = true;
turning.motor_speed = 2;
turning.max_motor_force = 100;
s.change(axle, turning);
steps(&mut s, 1);
t.expects(close(s.spin_of(wheel), 2, 0.01) && close(s.pose_of(wheel).at.x, 10, 1e-3));
# A door limited to a quarter turn.
val door := s.add(body{ at = pose::of(vec2{ 21, 4 }) });
_ = s.attach(door, collider{ shape = shape::box(vec2{ 1, 0.1 }) });
val hinge := s.pin(ground, door, vec2{ 20, 4 });
var limited := s.joint_of(hinge);
limited.limit = true;
limited.lower = -math::pi as f32 / 4;
limited.upper = 0;
s.change(hinge, limited);
steps(&mut s, 2);
t.expects(close(s.angle_of(hinge), -math::pi as f32 / 4, 0.02));
# A rod keeps two bodies apart; a weld keeps them as one.
val left := s.add(body{ at = pose::of(vec2{ -10, 6 }) });
_ = s.attach(left, collider{ shape = shape::circle(0.25) });
val right := s.add(body{ at = pose::of(vec2{ -8, 6 }) });
_ = s.attach(right, collider{ shape = shape::circle(0.25) });
_ = s.tie(left, right, vec2{ -10, 6 }, vec2{ -8, 6 });
val stuck := s.add(body{ at = pose::of(vec2{ -10, 7 }) });
_ = s.attach(stuck, collider{ shape = shape::box(vec2{ 0.25, 0.25 }) });
_ = s.weld(left, stuck, vec2{ -10, 6.5 });
steps(&mut s, 2);
t.expects(close((s.pose_of(right).at - s.pose_of(left).at).length, 2, 0.02));
val held := s.pose_of(left).unapply(s.pose_of(stuck).at);
t.expects(close(held.x, 0, 0.02) && close(held.y, 1, 0.02) && close(unturned(s.pose_of(stuck).turn.angle - s.pose_of(left).turn.angle), 0, 0.02));
}
def unturned(angle: f32): f32 {
val pi := math::pi as f32;
return angle < -pi ? angle + 2 * pi : angle > pi ? angle - 2 * pi : angle;
}
@test def slides_across_a_grid_without_catching(t: &mut tester): void {
var s := space::new();
s.sleeping = false;
# A floor of 40 cells and a wall, its top at y = 0.
var cells := grid::new(40, 4);
val wide: u32 = 40;
val high: u32 = 4;
for x in 0..wide {
cells.set(x, 0, true);
}
for y in 0..high {
cells.set(39, y, true);
}
val floor := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ -20, -1 }) });
val ground := s.attach_grid(floor, move(cells), collider{ friction = 0 });
val crate := s.add(body{ at = pose::of(vec2{ -18, 0.5 }), velocity = vec2{ 4, 0 } });
_ = s.attach(crate, collider{ shape = shape::box(vec2{ 0.5, 0.5 }), friction = 0 });
var lowest: f32 = 1;
var slowest: f32 = 4;
for i in 0..180u {
s.step(1f / 60f);
lowest = math::min(lowest, s.pose_of(crate).at.y);
slowest = math::min(slowest, s.velocity_of(crate).x);
}
# Across 12 seams it neither sank, bounced, nor slowed, nor turned.
t.expects(close(s.pose_of(crate).at.x, -6, 0.1) && close(lowest, 0.5, 0.01) && close(slowest, 4, 0.05) && close(s.spin_of(crate), 0, 0.01));
# Then it meets the wall and stops at it, within a few centimeters, and as
# it slides freely, pushed back out of the wall no faster than a little.
var farthest: f32 = 0;
for i in 0..420u {
s.step(1f / 60f);
farthest = math::max(farthest, s.pose_of(crate).at.x);
}
t.expects(close(farthest, 18.5, 0.05) && math::abs(s.velocity_of(crate).x) < 0.25);
val hit := s.ray_cast(vec2{ 5, 3 }, vec2{ 0, -1 }, 10).expect("It meets the floor.");
t.expects(hit.collider == ground && close(hit.distance, 3, 1e-4));
}
@test def casts_rays_in_space(t: &mut tester): void {
var s := space::new();
val ground := grounded(&mut s);
val crate := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 3, 0.5 }) });
val side := s.attach(crate, collider{ shape = shape::box(vec2{ 0.5, 0.5 }), layers = 2 });
val hit := s.ray_cast(vec2{ 0, 0.5 }, vec2{ 1, 0 }, 10).expect("It meets the crate.");
t.expects(hit.collider == side && hit.body == crate && close(hit.distance, 2.5, 1e-4) && close(hit.normal.x, -1, 1e-4));
val down := s.ray_cast(vec2{ 3, 5 }, vec2{ 0, -1 }, 10, 1).expect("It passes the crate to the ground.");
t.expects(down.body == ground && close(down.distance, 5, 1e-4));
t.expects(s.ray_cast(vec2{ 0, 5 }, vec2{ 0, 1 }, 10) is none);
}