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

293 lines
12 KiB
Plaintext

#! Contains the tests of how `physics3d` keeps what moves fast from passing
#! through what's thin, what a space is asked of the shapes in it, heightfields,
#! and the joints that slide and limit: gaps between shapes, shapes cast along
#! motions, times of impact, bodies swept through a step, which colliders a
#! shape overlaps or a cast meets, a terrain's heights, rays, and what rolls on
#! it, a slider, a ball joint's cone and twist, and six-way joints.
import std::testing;
import std::containers;
import std::math;
import malunal::linear;
import malunal::physics3d;
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(vec3{ 0.5, 0.5, 0.5 });
val apart := gap_between(&crate, pose::identity, &crate, pose::of(vec3{ 2, 0.25, -0.1 }));
t.expects(near(apart.distance, 1, 1e-4) && near(apart.normal.x, 1, 1e-4) && near(apart.on_a.x, 0.5, 1e-4));
# A ball off a box's corner, along its diagonal.
val ball := shape::sphere(0.25);
val corner := gap_between(&crate, pose::identity, &ball, pose::of(vec3{ 1.5, 1.5, 1.5 }));
t.expects(near(corner.distance, math::sqrt(3f) - 0.25, 1e-3) && near(corner.normal.x, corner.normal.z, 1e-3));
# A capsule lying across a turned box's top edge.
val pill := shape::capsule(vec3{ 0, 0, -2 }, vec3{ 0, 0, 2 }, 0.2);
val turned := quat::axis_angle(vec3::back, math::pi as f32 / 4);
val edge := gap_between(&crate, pose::of(vec3::zero, turned), &pill, pose::of(vec3{ 0, 2, 0 }));
t.expects(near(edge.distance, 2 - math::sqrt(0.5f) - 0.2, 1e-3) && near(edge.normal.y, 1, 1e-3));
val within := gap_between(&crate, pose::identity, &ball, pose::of(vec3{ 0.3, 0, 0 }));
t.expects(within.distance == 0);
}
@test def casts_shapes_along_motions(t: &mut tester): void {
val wall := shape::box(vec3{ 0.1, 2, 2 });
val ball := shape::sphere(0.5);
val hit := shape_cast(&wall, pose::of(vec3{ 5, 0, 0 }), &ball, pose::identity, vec3{ 10, 0, 0 }) else { t.expects(false); return; };
t.expects(near(hit.fraction, 0.44, 2e-3) && near(hit.normal.x, -1, 1e-3) && near(hit.point.x, 4.9, 2e-3));
t.expects(!(shape_cast(&wall, pose::of(vec3{ 5, 0, 0 }), &ball, pose::identity, vec3{ 0, 10, 0 }) is some(_)));
# A box dropped onto a box turned on its corner meets the corner.
val crate := shape::box(vec3{ 0.5, 0.5, 0.5 });
val tipped := quat::axis_angle(vec3::back, math::pi as f32 / 4) * quat::axis_angle(vec3::right, math::atan(math::sqrt(0.5f)));
val down := shape_cast(&crate, pose::of(vec3::zero, tipped), &crate, pose::of(vec3{ 0, 4, 0 }), vec3{ 0, -4, 0 }) else { t.expects(false); return; };
t.expects(down.fraction > 0.5 && near(down.normal.y, 1, 0.02));
}
# A thin wall, and a ball thrown at it faster than it's thick each step.
def thrown(continuous: bool): f32 {
var s := space::new(vec3::zero);
s.continuous = continuous;
val wall := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 5.5, 0, 0 }) });
_ = s.attach(wall, collider{ shape = shape::box(vec3{ 0.02, 3, 3 }) });
val ball := s.add(body{ velocity = vec3{ 300, 0, 0 } });
_ = s.attach(ball, collider{ shape = shape::sphere(0.1) });
run(&mut s, 0.5);
return s.pose_of(ball).at.x;
}
# A square floor of triangles, two to a meter, its top at y 0.
def floor_mesh(): triangle_mesh {
var points := vector::<vec3>::new();
var indices := vector::<u32>::new();
val side: u32 = 20;
for z in 0..side + 1 {
for x in 0..side + 1 {
points.append(vec3{ x as f32 - 10, 0, z as f32 - 10 });
}
}
for z in 0..side {
for x in 0..side {
val i := (z * (side + 1) + x) as u32;
indices.append(i);
indices.append(i + side + 1);
indices.append(i + 1);
indices.append(i + 1);
indices.append(i + side + 1);
indices.append(i + side + 2);
}
}
return triangle_mesh::new(points.span, indices.span);
}
@test def keeps_fast_bodies_from_passing_through(t: &mut tester): void {
t.expects(thrown(true) < 5.45);
t.expects(thrown(false) > 10);
# Dropped onto a mesh's floor fast enough to pass it in a step, a crate
# lands on it.
var s := space::new();
val level := s.add(body{ kind = body_kind::static });
_ = s.attach_mesh(level, floor_mesh(), collider{});
val crate := s.add(body{ at = pose::of(vec3{ 0.3, 3, 0.2 }), velocity = vec3{ 0, -150, 0 } });
_ = s.attach(crate, collider{ shape = shape::box(vec3{ 0.25, 0.25, 0.25 }) });
var lowest: f32 = 3;
for i in 0..120u {
s.step(1f / 60f);
lowest = math::min(lowest, s.pose_of(crate).at.y);
}
t.expects(lowest > 0.2 && near(s.pose_of(crate).at.y, 0.25, 0.02));
}
@test def asks_what_shapes_overlap_and_meet(t: &mut tester): void {
var s := space::new();
val level := s.add(body{ kind = body_kind::static });
_ = s.attach_mesh(level, floor_mesh(), collider{});
val crate := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 3, 0.5, 0 }) });
val held := s.attach(crate, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), layers = 2 });
var found: u32 = 0;
s.overlap(&shape::sphere(0.3), pose::of(vec3{ 2.4, 0.1, 0 }), 0xFFFFFFFF, def(c: collider_id, b: body_id): bool {
found++;
return true;
});
t.expects(found == 2);
found = 0;
s.overlap(&shape::sphere(0.3), pose::of(vec3{ 2.4, 0.1, 0 }), 2, def(c: collider_id, b: body_id): bool {
found++;
return true;
});
t.expects(found == 1);
# A ball cast down meets the floor's triangle; cast along it, the crate.
val ball := shape::sphere(0.25);
val down := s.shape_cast(&ball, pose::of(vec3{ -2.3, 3, 1.6 }), vec3{ 0, -5, 0 }) else { t.expects(false); return; };
t.expects(near(down.point.y, 0, 1e-3) && near(down.normal.y, 1, 1e-3) && down.triangle != 0xFFFFFFFF);
val along := s.shape_cast(&ball, pose::of(vec3{ 0, 0.5, 0 }), vec3{ 10, 0, 0 }) else { t.expects(false); return; };
t.expects(along.collider == held && near(along.point.x, 2.5, 0.01));
}
# A terrain of 33 by 33 heights a meter apart, of a slope and a bump.
def terrain(): heightfield {
var heights := vector::<f32>::new();
for z in 0..33u {
for x in 0..33u {
val dx := x as f32 - 16;
val dz := z as f32 - 16;
heights.append(0.1 * x as f32 + 2 * math::exp(-(dx * dx + dz * dz) / 20));
}
}
return heightfield::new(33, 33, heights.span);
}
@test def keeps_heightfields(t: &mut tester): void {
val field := terrain();
t.expects(field.length == 32 * 32 * 2);
# Its height between its heights is as its triangles lie.
val at := field.height_at(16, 16) else { t.expects(false); return; };
t.expects(near(at, 1.6 + 2, 1e-4));
val between := field.height_at(4.5, 2.25) else { t.expects(false); return; };
t.expects(between > 0.4 && between < 0.5);
t.expects(!(field.height_at(-1, 3) is some(_)));
var s := space::new();
val ground := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ -16, 0, -16 }) });
_ = s.attach_heightfield(ground, terrain(), collider{});
# A ray down meets it where its height says.
val hit := s.ray_cast(vec3{ 0, 10, 0 }, vec3::down, 20) else { t.expects(false); return; };
t.expects(near(hit.point.y, 3.6, 1e-3) && hit.normal.y > 0.9);
# A ball dropped by the bump rolls off it, down the slope, resting on the
# ground below, never sunk in it.
val ball := s.add(body{ at = pose::of(vec3{ 1.3, 5, 0.4 }) });
_ = s.attach(ball, collider{ shape = shape::sphere(0.5), friction = 0.5 });
var sunk := false;
for i in 0..300u {
s.step(1f / 60f);
val p := s.pose_of(ball).at;
if field.height_at(p.x + 16, p.z + 16) is some(h) {
if p.y < h + 0.3 {
sunk = true;
}
}
}
t.expects(!sunk && s.pose_of(ball).at.x > 3);
}
@test def slides_along_sliders(t: &mut tester): void {
var s := space::new();
val ground := s.add(body{ kind = body_kind::static });
val block := s.add(body{ at = pose::of(vec3{ 0, 2, 0 }) });
_ = s.attach(block, collider{ shape = shape::box(vec3{ 0.5, 0.25, 0.25 }) });
val rail := s.slider(ground, block, vec3{ 0, 2, 0 }, vec3{ 1, 0, 1 });
var made := s.joint_of(rail);
made.motor = true;
made.motor_speed = 2;
made.max_motor_force = 100;
made.limit = true;
made.lower = -1;
made.upper = 3;
s.change(rail, made);
run(&mut s, 3);
val at := s.pose_of(block);
val along := 3 / math::sqrt(2f);
t.expects(near(at.at.x, along, 0.03) && near(at.at.z, along, 0.03) && near(at.at.y, 2, 0.01));
t.expects(near(at.turn.w, 1, 1e-3));
}
@test def limits_ball_joints_by_cone_and_twist(t: &mut tester): void {
var s := space::new();
val post := s.add(body{ kind = body_kind::static });
# An arm hanging from a shoulder held within 30 degrees of straight out,
# sideways, sags no further than its cone.
val arm := s.add(body{ at = pose::of(vec3{ 1, 3, 0 }) });
_ = s.attach(arm, collider{ shape = shape::box(vec3{ 1, 0.1, 0.1 }) });
_ = s.cone_twist(post, arm, vec3{ 0, 3, 0 }, vec3::right, math::pi as f32 / 6, 0.2);
run(&mut s, 3);
val tip := s.pose_of(arm).apply(vec3{ 1, 0, 0 });
val sag := math::atan2(3 - tip.y, tip.x);
t.expects(near(sag, math::pi as f32 / 6, 0.03));
# Spun about its length, it twists no further than its limit.
var twisty := space::new(vec3::zero);
val base := twisty.add(body{ kind = body_kind::static });
val bar := twisty.add(body{ at = pose::of(vec3{ 1, 0, 0 }), spin = vec3{ 5, 0, 0 } });
_ = twisty.attach(bar, collider{ shape = shape::box(vec3{ 1, 0.1, 0.3 }) });
_ = twisty.cone_twist(base, bar, vec3::zero, vec3::right, 0.5, 0.4);
var most: f32 = 0;
for i in 0..120u {
twisty.step(1f / 60f);
val q := twisty.pose_of(bar).turn;
most = math::max(most, 2 * math::atan2(math::abs(q.x), math::abs(q.w)));
}
t.expects(most < 0.45 && most > 0.35);
}
@test def holds_six_way_joints(t: &mut tester): void {
# Locked every way, it holds as a fixed joint: a beam out from a wall stays.
var s := space::new();
val wall := s.add(body{ kind = body_kind::static });
val beam := s.add(body{ at = pose::of(vec3{ 1, 2, 0 }) });
_ = s.attach(beam, collider{ shape = shape::box(vec3{ 1, 0.1, 0.1 }) });
val held := s.six_dof(wall, beam, vec3{ 0, 2, 0 });
run(&mut s, 2);
t.expects(near(s.pose_of(beam).at.y, 2, 0.02) && near(s.pose_of(beam).turn.w, 1, 2e-3));
# Its twist freed and driven, it turns about x as a hinge would; its swing
# about z limited, freed, it sags to its limit.
var made := s.joint_of(held);
made.holds[3] = axis_hold::free;
made.drives[3] = 2;
made.forces[3] = 200;
made.holds[5] = axis_hold::limited;
made.lows[5] = -0.3;
made.highs[5] = 0.3;
s.change(held, made);
run(&mut s, 2);
val turn := s.pose_of(beam).turn;
val tip := s.pose_of(beam).apply(vec3{ 1, 0, 0 });
t.expects(near(s.spin_of(beam).x, 2, 0.2) && near(math::atan2(2 - tip.y, tip.x), 0.3, 0.04));
# Its place freed along x, a slider: pulled by gravity along a slope.
var slope := space::new();
val anchor := slope.add(body{ kind = body_kind::static });
val car := slope.add(body{ at = pose::of(vec3{ 0, 2, 0 }) });
_ = slope.attach(car, collider{ shape = shape::box(vec3{ 0.2, 0.2, 0.2 }) });
val tilt := quat::axis_angle(vec3::back, -math::pi as f32 / 6);
val rail := slope.six_dof(anchor, car, vec3{ 0, 2, 0 }, tilt);
var free := slope.joint_of(rail);
free.holds[0] = axis_hold::free;
slope.change(rail, free);
run(&mut slope, 1);
val p := slope.pose_of(car).at;
t.expects(p.x > 1 && near((p.y - 2) / p.x, -math::tan(math::pi as f32 / 6), 0.02));
}
# 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(vec3{ 0, -1, 0 }) });
_ = s.attach(ground, collider{ shape = shape::box(vec3{ 50, 1, 50 }) });
var top := body_id{};
for i in 0..8u {
for j in 0..2u {
for k in 0..2u {
top = s.add(body{ at = pose::of(vec3{ j as f32 * 1.1, 0.5 + i as f32, k as f32 * 1.1 }) });
_ = s.attach(top, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }) });
}
}
}
val ball := s.add(body{ at = pose::of(vec3{ -8, 3, 0.5 }), velocity = vec3{ 20, 0, 0 } });
_ = s.attach(ball, collider{ shape = shape::sphere(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);
}