Initial Commit
This commit is contained in:
84 files changed
+18825
No files matched your search
@@ -0,0 +1,131 @@
|
||||
#! Contains the tests of `physics2d`'s characters: walking ground, stepping up
|
||||
#! a stair but not a wall, walking up a gentle slope but not a steep one,
|
||||
#! riding a moving platform, jumping up through a one-way platform and landing
|
||||
#! on it, and walking a chain of segments.
|
||||
|
||||
import std::testing;
|
||||
import std::containers;
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
import malunal::physics2d;
|
||||
|
||||
def about(a: f32, b: f32, within: f32): bool {
|
||||
return math::abs(a - b) <= within;
|
||||
}
|
||||
|
||||
def standing(s: &mut space, at: vec2): character {
|
||||
val floor := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, -1 }) });
|
||||
_ = s.attach(floor, collider{ shape = shape::box(vec2{ 50, 1 }) });
|
||||
var c := character{ at = at };
|
||||
s.move_character(&mut c, vec2::zero, 1f / 60f);
|
||||
return c;
|
||||
}
|
||||
|
||||
# Walks a character for a time, falling where it's in the air, jumping at
|
||||
# first where it's asked.
|
||||
def walk(s: &mut space, c: &mut character, along: f32, seconds: f32, jump: f32 = 0): void {
|
||||
val n := (seconds * 60) as u32;
|
||||
for i in 0..n {
|
||||
var v := c.velocity;
|
||||
v.x = along;
|
||||
if !c.grounded {
|
||||
v.y -= 10f / 60f;
|
||||
} else if i == 0 && jump > 0 {
|
||||
v.y = jump;
|
||||
}
|
||||
s.move_character(c, v, 1f / 60f);
|
||||
s.step(1f / 60f);
|
||||
}
|
||||
}
|
||||
|
||||
def block(s: &mut space, center: vec2, half: vec2, angle: f32 = 0): void {
|
||||
val made := s.add(body{ kind = body_kind::static, at = pose::of(center, angle) });
|
||||
_ = s.attach(made, collider{ shape = shape::box(half) });
|
||||
}
|
||||
|
||||
@test def walks_and_steps(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec2{ 0, 1 });
|
||||
walk(&mut s, &mut c, 0, 0.5);
|
||||
t.expects(c.grounded && about(c.at.y, 0.92, 0.01));
|
||||
block(&mut s, vec2{ 3, 0.125 }, vec2{ 1, 0.125 });
|
||||
block(&mut s, vec2{ 8, 0.3 }, vec2{ 1, 0.3 });
|
||||
walk(&mut s, &mut c, 2, 4);
|
||||
t.expects(c.grounded && about(c.at.x, 7 - 0.3 - 0.02, 0.05) && about(c.at.y, 0.92, 0.02));
|
||||
}
|
||||
|
||||
@test def walks_around_a_world(t: &mut tester): void {
|
||||
# A world of 20 meters, up away from its middle wherever the character is.
|
||||
var s := space::new(vec2::zero);
|
||||
val world := s.add(body{ kind = body_kind::static });
|
||||
_ = s.attach(world, collider{ shape = shape::circle(20) });
|
||||
var c := character{ at = vec2{ 0, 21 } };
|
||||
# Walking at 3 meters a second along the ground, clockwise around the
|
||||
# world's middle, falling toward it where it's in the air, and on the
|
||||
# ground keeping nothing of how fast it moved up.
|
||||
for i in 0..600u {
|
||||
c.up = c.at.normalized();
|
||||
var v := vec2{ c.up.y, -c.up.x } * 3;
|
||||
if !c.grounded {
|
||||
v = v + c.up * (c.velocity.dot(c.up) - 10f / 60f);
|
||||
}
|
||||
s.move_character(&mut c, v, 1f / 60f);
|
||||
s.step(1f / 60f);
|
||||
}
|
||||
# It stands on the ground, as far from the middle as its feet put it, 30
|
||||
# meters around from where it began, its shape standing along its up.
|
||||
val around := math::pi as f32 / 2 - math::atan2(c.at.y, c.at.x);
|
||||
t.expects(c.grounded && about(c.at.length, 20.92, 0.01) && about(around, 30f / 20.92f, 0.05));
|
||||
t.expects(c.ground_normal.dot(c.up) > 0.99 && (c.pose().turn.apply(vec2{ 0, 1 }) - c.up).length < 0.001);
|
||||
}
|
||||
|
||||
@test def walks_gentle_slopes_but_not_steep_ones(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec2{ 0, 0.92 });
|
||||
block(&mut s, vec2{ 1, 0 } + rot::of(0.35).apply(vec2{ 4, -0.5 }), vec2{ 4, 0.5 }, 0.35);
|
||||
walk(&mut s, &mut c, 2, 2);
|
||||
t.expects(c.grounded && c.at.y > 1.5);
|
||||
var steep := space::new();
|
||||
var d := standing(&mut steep, vec2{ 0, 0.92 });
|
||||
block(&mut steep, vec2{ 1, 0 } + rot::of(1.1).apply(vec2{ 4, -0.5 }), vec2{ 4, 0.5 }, 1.1);
|
||||
walk(&mut steep, &mut d, 2, 2);
|
||||
t.expects(d.at.y < 1.3 && d.at.x < 1.5);
|
||||
}
|
||||
|
||||
@test def rides_moving_platforms(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec2{ 0, 0.92 });
|
||||
val lift := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec2{ 0, 0.25 }) });
|
||||
_ = s.attach(lift, collider{ shape = shape::box(vec2{ 1, 0.1 }) });
|
||||
c.at = vec2{ 0, 0.35 + 0.92 };
|
||||
s.move_character(&mut c, vec2::zero, 1f / 60f);
|
||||
s.set_velocity(lift, vec2{ 1, 0.5 });
|
||||
walk(&mut s, &mut c, 0, 2);
|
||||
t.expects(c.grounded && about(c.at.x, s.pose_of(lift).at.x, 0.1) && about(c.at.y, s.pose_of(lift).at.y + 0.1 + 0.92, 0.03));
|
||||
}
|
||||
|
||||
@test def jumps_up_through_one_way_platforms(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec2{ 0, 0.92 });
|
||||
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 });
|
||||
walk(&mut s, &mut c, 0, 2, 9);
|
||||
t.expects(c.grounded && about(c.at.y, 2.1 + 0.92, 0.02));
|
||||
}
|
||||
|
||||
@test def walks_chains(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
# Ground of a chain, bumpy but gentle, right to left so it faces up.
|
||||
var places := vector::<vec2>::new();
|
||||
var i: i64 = 40;
|
||||
while i >= 0 {
|
||||
places.append(vec2{ i as f32 * 0.5 - 2, i % 2 == 0 ? 0f : 0.05f });
|
||||
i--;
|
||||
}
|
||||
val ground := s.add(body{ kind = body_kind::static });
|
||||
_ = s.attach_chain(ground, places.span, false, collider{});
|
||||
var c := character{ at = vec2{ 0, 1 } };
|
||||
walk(&mut s, &mut c, 0, 0.5);
|
||||
walk(&mut s, &mut c, 3, 3);
|
||||
t.expects(c.grounded && about(c.at.x, 9, 0.2) && c.at.y > 0.9 && c.at.y < 1);
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
#! Contains the tests of `physics3d`'s characters: walking a floor, stepping up
|
||||
#! a stair but not a wall, walking up a gentle ramp but not a steep one,
|
||||
#! sliding along a wall, riding a moving platform, pushed by what moves into
|
||||
#! it, and pushing a crate.
|
||||
|
||||
import std::testing;
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
import malunal::physics3d;
|
||||
|
||||
def about(a: f32, b: f32, within: f32): bool {
|
||||
return math::abs(a - b) <= within;
|
||||
}
|
||||
|
||||
# A space with a floor, its top at y 0, and a character standing on it.
|
||||
def standing(s: &mut space, at: vec3): character {
|
||||
val floor := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 0, -1, 0 }) });
|
||||
_ = s.attach(floor, collider{ shape = shape::box(vec3{ 50, 1, 50 }) });
|
||||
var c := character{ at = at };
|
||||
s.move_character(&mut c, vec3::zero, 1f / 60f);
|
||||
return c;
|
||||
}
|
||||
|
||||
# Walks a character at a velocity across the ground for a time, falling where
|
||||
# it's in the air, the space stepped beside it.
|
||||
def walk(s: &mut space, c: &mut character, along: vec3, seconds: f32): void {
|
||||
val n := (seconds * 60) as u32;
|
||||
for i in 0..n {
|
||||
var v := c.velocity;
|
||||
v.x = along.x;
|
||||
v.z = along.z;
|
||||
if !c.grounded {
|
||||
v.y -= 10f / 60f;
|
||||
}
|
||||
|
||||
|
||||
s.move_character(c, v, 1f / 60f);
|
||||
s.step(1f / 60f);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
def block(s: &mut space, center: vec3, half: vec3, turn: quat = quat::identity): body_id {
|
||||
val made := s.add(body{ kind = body_kind::static, at = pose::of(center, turn) });
|
||||
_ = s.attach(made, collider{ shape = shape::box(half) });
|
||||
return made;
|
||||
}
|
||||
|
||||
@test def walks_floors(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec3{ 0, 1, 0 });
|
||||
walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 0.5);
|
||||
# It fell to the floor, stands on it, and walks across it.
|
||||
t.expects(c.grounded && about(c.at.y, 0.92, 0.01));
|
||||
walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 2);
|
||||
t.expects(c.grounded && about(c.at.x, 5, 0.1) && about(c.at.y, 0.92, 0.01));
|
||||
}
|
||||
|
||||
@test def steps_up_stairs_but_not_walls(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec3{ 0, 0.92, 0 });
|
||||
_ = block(&mut s, vec3{ 3, 0.15, 0 }, vec3{ 1, 0.15, 2 });
|
||||
_ = block(&mut s, vec3{ 8, 0.3, 0 }, vec3{ 1, 0.3, 2 });
|
||||
walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 4);
|
||||
# Up the stair 30 centimeters high, onto it and off it, then stopped by the
|
||||
# block 60 centimeters high.
|
||||
t.expects(c.grounded && about(c.at.x, 7 - 0.3 - 0.02, 0.05) && about(c.at.y, 0.92, 0.02));
|
||||
}
|
||||
|
||||
@test def walks_up_gentle_ramps_but_not_steep_ones(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec3{ 0, 0.92, 0 });
|
||||
# A ramp of 20 degrees rising toward +x, from x 1.
|
||||
val gentle := quat::axis_angle(vec3::back, 0.35);
|
||||
_ = block(&mut s, vec3{ 1, 0, 0 } + gentle * vec3{ 4, -0.5, 0 }, vec3{ 4, 0.5, 2 }, gentle);
|
||||
walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 2);
|
||||
t.expects(c.grounded && c.at.y > 1.5);
|
||||
var steep_space := space::new();
|
||||
var d := standing(&mut steep_space, vec3{ 0, 0.92, 0 });
|
||||
val steep := quat::axis_angle(vec3::back, 1.1);
|
||||
_ = block(&mut steep_space, vec3{ 1, 0, 0 } + steep * vec3{ 4, -0.5, 0 }, vec3{ 4, 0.5, 2 }, steep);
|
||||
walk(&mut steep_space, &mut d, vec3{ 2, 0, 0 }, 2);
|
||||
t.expects(d.at.y < 1.3 && d.at.x < 1.5);
|
||||
}
|
||||
|
||||
@test def slides_along_walls(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec3{ 0, 0.92, 0 });
|
||||
_ = block(&mut s, vec3{ 2, 1, 0 }, vec3{ 0.5, 1, 20 });
|
||||
# Walking into the wall at 45 degrees, it slides along it.
|
||||
walk(&mut s, &mut c, vec3{ 2, 0, 2 }, 2);
|
||||
t.expects(about(c.at.x, 1.5 - 0.3 - 0.02, 0.03) && c.at.z > 3);
|
||||
}
|
||||
|
||||
@test def rides_platforms_and_is_pushed(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec3{ 0, 0.92, 0 });
|
||||
val lift := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec3{ 0, 0.25, 0 }) });
|
||||
_ = s.attach(lift, collider{ shape = shape::box(vec3{ 1, 0.1, 1 }) });
|
||||
c.at = vec3{ 0, 0.35 + 0.92, 0 };
|
||||
s.move_character(&mut c, vec3::zero, 1f / 60f);
|
||||
s.set_velocity(lift, vec3{ 1, 0, 0 });
|
||||
walk(&mut s, &mut c, vec3::zero, 2);
|
||||
# Carried along by the platform it stands on.
|
||||
t.expects(c.grounded && about(c.at.x, s.pose_of(lift).at.x, 0.1) && about(s.pose_of(lift).at.x, 2, 0.05));
|
||||
# A wall moving into it pushes it along.
|
||||
var w := space::new();
|
||||
var d := standing(&mut w, vec3{ 0, 0.92, 0 });
|
||||
val pusher := w.add(body{ kind = body_kind::kinematic, at = pose::of(vec3{ -2, 1, 0 }) });
|
||||
_ = w.attach(pusher, collider{ shape = shape::box(vec3{ 0.5, 1, 2 }) });
|
||||
w.set_velocity(pusher, vec3{ 1, 0, 0 });
|
||||
walk(&mut w, &mut d, vec3::zero, 3);
|
||||
t.expects(d.at.x > 0.5 && d.at.x > w.pose_of(pusher).at.x + 0.5 + 0.25);
|
||||
}
|
||||
|
||||
@test def walks_around_a_world(t: &mut tester): void {
|
||||
# A world of 20 meters, up away from its middle wherever the character is.
|
||||
var s := space::new(vec3::zero);
|
||||
val world := s.add(body{ kind = body_kind::static });
|
||||
_ = s.attach(world, collider{ shape = shape::sphere(20) });
|
||||
var c := character{ at = vec3{ 0, 21, 0 } };
|
||||
# Walking at 3 meters a second along the ground, around the world's middle
|
||||
# toward +x, falling toward it where it's in the air. Up turns as it walks,
|
||||
# so what was along the ground a step ago leans up from it: on the ground,
|
||||
# it keeps nothing of how fast it moved up.
|
||||
for i in 0..600u {
|
||||
c.up = c.at.normalized();
|
||||
var v := c.up.cross(vec3::back).normalized() * 3;
|
||||
if !c.grounded {
|
||||
v = v + c.up * (c.velocity.dot(c.up) - 10f / 60f);
|
||||
}
|
||||
s.move_character(&mut c, v, 1f / 60f);
|
||||
s.step(1f / 60f);
|
||||
}
|
||||
# It stands on the ground, as far from the middle as its feet put it, 30
|
||||
# meters around from where it began: its shape stood along its up all along.
|
||||
val around := math::pi as f32 / 2 - math::atan2(c.at.y, c.at.x);
|
||||
t.expects(c.grounded && about(c.at.length, 20.92, 0.01) && about(around, 30f / 20.92f, 0.05));
|
||||
t.expects(c.ground_normal.dot(c.up) > 0.99 && about(c.at.z, 0, 0.01));
|
||||
# Its pose stands its shape along its up.
|
||||
t.expects((c.pose().turn * vec3::up - c.up).length < 0.001);
|
||||
}
|
||||
|
||||
@test def pushes_crates(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
var c := standing(&mut s, vec3{ 0, 0.92, 0 });
|
||||
val crate := s.add(body{ at = pose::of(vec3{ 1.5, 0.5, 0 }) });
|
||||
_ = s.attach(crate, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), density = 20 });
|
||||
walk(&mut s, &mut c, vec3{ 1.5, 0, 0 }, 3);
|
||||
t.expects(s.pose_of(crate).at.x > 3 && c.at.x < s.pose_of(crate).at.x);
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
#! Contains the tests of `geometry`: planes, and which side of one something
|
||||
#! lies on; boxes and spheres holding places and meeting; rays meeting each
|
||||
#! shape; a camera's frustum reaching what it sees; and the same bits whether
|
||||
#! numbers are worked as lanes or one at a time, which `malunal.geometry.tests`
|
||||
#! and `malunal.geometry.lanes.tests` each check.
|
||||
|
||||
import std::testing;
|
||||
import std::json;
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
import malunal::geometry;
|
||||
|
||||
# Whether two numbers are within a millionth or so of one another.
|
||||
def near(a: f32, b: f32): bool {
|
||||
return math::abs(a - b) < 1e-5;
|
||||
}
|
||||
|
||||
def near(a: vec3, b: vec3): bool {
|
||||
return near(a.x, b.x) && near(a.y, b.y) && near(a.z, b.z);
|
||||
}
|
||||
|
||||
# How far along a ray it meets a shape, or -1 where it doesn't.
|
||||
def hit_at(found: maybe<f32>): f32 {
|
||||
return found is some(t) ? t : -1;
|
||||
}
|
||||
|
||||
@test def lays_out_its_shapes(t: &mut tester): void {
|
||||
t.expects(size_of::<ray>() == 32 && size_of::<plane>() == 20 && size_of::<bounds>() == 32 && size_of::<sphere>() == 20);
|
||||
t.expects(size_of::<frustum>() == 128 && align_of::<frustum>() == 4 && align_of::<bounds>() == 4);
|
||||
}
|
||||
|
||||
@test def writes_and_reads_its_shapes(t: &mut tester): void {
|
||||
# Each is written by its fields' names, its vectors as `linear` writes them.
|
||||
val box := bounds::around(vec3{ 1, 2, 3 }, vec3{ 0.5, 0.5, 0.5 });
|
||||
val text := json::to_text(&box) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(text.span == "{\"min\":{\"x\":0.5,\"y\":1.5,\"z\":2.5},\"max\":{\"x\":1.5,\"y\":2.5,\"z\":3.5}}");
|
||||
t.expects(json::from_text::<bounds>(text.span).value == box);
|
||||
val ball := sphere{ vec3{ 0, 1, 0 }, 0.75 };
|
||||
t.expects(json::from_text::<sphere>(json::to_text(&ball).value.span).value == ball);
|
||||
val floor := plane::through(vec3{ 0, 2, 0 }, vec3::up);
|
||||
t.expects(json::from_text::<plane>(json::to_text(&floor).value.span).value == floor);
|
||||
val look := ray::toward(vec3::zero, vec3{ 0, 0, -4 });
|
||||
t.expects(json::from_text::<ray>(json::to_text(&look).value.span).value == look);
|
||||
}
|
||||
|
||||
@test def measures_planes(t: &mut tester): void {
|
||||
val floor := plane::through(vec3{ 0, 2, 0 }, vec3{ 0, 5, 0 });
|
||||
t.expects(floor.normal == vec3::up && floor.distance == 2);
|
||||
t.expects(floor.distance_to(vec3{ 3, 5, -1 }) == 3 && floor.distance_to(vec3::zero) == -2);
|
||||
t.expects(floor.closest(vec3{ 3, 5, -1 }) == vec3{ 3, 2, -1 } && floor.flipped().distance_to(vec3::zero) == 2);
|
||||
# Turning counterclockwise as seen from +z, it faces +z.
|
||||
val wall := plane::of_points(vec3{ 0, 0, 1 }, vec3{ 1, 0, 1 }, vec3{ 0, 1, 1 });
|
||||
t.expects(wall.normal == vec3::back && wall.distance == 1);
|
||||
t.expects(floor.side_of(bounds::around(vec3{ 0, 4, 0 }, vec3::one)) == side::front);
|
||||
t.expects(floor.side_of(bounds::around(vec3{ 0, 2.5, 0 }, vec3::one)) == side::across);
|
||||
t.expects(floor.side_of(bounds::around(vec3{ 5, -1, 5 }, vec3::one)) == side::back);
|
||||
# A sphere touching it is on both sides.
|
||||
t.expects(floor.side_of(sphere{ vec3::zero, 2 }) == side::across && floor.side_of(sphere{ vec3{ 0, -1, 0 }, 2 }) == side::back);
|
||||
}
|
||||
|
||||
@test def holds_and_meets_boxes_and_spheres(t: &mut tester): void {
|
||||
val b := bounds{ vec3{ -1, 0, -1 }, vec3{ 1, 2, 1 } };
|
||||
t.expects(b.center == vec3{ 0, 1, 0 } && b.size == vec3{ 2, 2, 2 } && b.half_size == vec3::one);
|
||||
t.expects(b.contains(vec3{ 1, 2, 1 }) && !b.contains(vec3{ 1, 2.5, 1 }));
|
||||
t.expects(b.contains(bounds::around(b.center, vec3{ 0.5, 0.5, 0.5 })) && !b.contains(bounds::around(b.center, vec3{ 2, 0.5, 0.5 })));
|
||||
# Touching at a corner, they meet.
|
||||
t.expects(b.overlaps(bounds{ vec3{ 1, 2, 1 }, vec3{ 3, 3, 3 } }) && !b.overlaps(bounds{ vec3{ 1.5, 0, 0 }, vec3{ 3, 1, 1 } }));
|
||||
t.expects(b.closest(vec3{ 5, 1, -3 }) == vec3{ 1, 1, -1 } && b.closest(vec3{ 0, 1, 0 }) == vec3{ 0, 1, 0 });
|
||||
val points: vec3[3] = [vec3{ 1, 5, -2 }, vec3{ -3, 0, 4 }, vec3{ 2, 1, 0 }];
|
||||
t.expects(bounds::of(points[:]) == bounds{ vec3{ -3, 0, -2 }, vec3{ 2, 5, 4 } } && bounds::of(points[0:0]) == bounds::blank);
|
||||
t.expects(bounds::blank.empty && !b.empty && bounds::blank.including(vec3::one) == bounds{ vec3::one, vec3::one });
|
||||
t.expects(b.merged(bounds::around(vec3{ 4, 0, 0 }, vec3::one)) == bounds{ vec3{ -1, -1, -1 }, vec3{ 5, 2, 1 } });
|
||||
# A quarter turn about z takes the box's reach along y to x, and along x to y.
|
||||
val quarter := quat::axis_angle(vec3::back, math::pi as f32 / 2);
|
||||
val turned := bounds::around(vec3::zero, vec3{ 1, 2, 3 }).transformed(mat4::compose(vec3{ 10, 0, 0 }, quarter, vec3::one));
|
||||
t.expects(near(turned.min, vec3{ 8, -1, -3 }) && near(turned.max, vec3{ 12, 1, 3 }));
|
||||
t.expects(bounds::blank.transformed(mat4::identity).empty);
|
||||
|
||||
val s := sphere{ vec3::zero, 2 };
|
||||
t.expects(s.contains(vec3{ 0, 2, 0 }) && !s.contains(vec3{ 1.5, 1.5, 0 }));
|
||||
# Touching, they meet.
|
||||
t.expects(s.overlaps(sphere{ vec3{ 3, 0, 0 }, 1 }) && !s.overlaps(sphere{ vec3{ 3, 0, 0.5 }, 0.9 }));
|
||||
t.expects(s.overlaps(b) && b.overlaps(s) && !s.overlaps(bounds::around(vec3{ 3, 3, 0 }, vec3::one)));
|
||||
t.expects(bounds::of(s) == bounds::around(vec3::zero, vec3{ 2, 2, 2 }));
|
||||
t.expects(sphere::of(b).center == b.center && near(sphere::of(b).radius, math::sqrt(3f)));
|
||||
}
|
||||
|
||||
@test def casts_rays(t: &mut tester): void {
|
||||
val r := ray::toward(vec3{ 0, 0, 10 }, vec3::zero);
|
||||
t.expects(r.direction == vec3::forward && r.at(4) == vec3{ 0, 0, 6 });
|
||||
t.expects(r.closest(vec3{ 3, 0, 5 }) == vec3{ 0, 0, 5 } && r.closest(vec3{ 0, 0, 20 }) == r.origin);
|
||||
# A wall ahead is met from in front, and one behind, and a floor it runs beside, aren't.
|
||||
t.expects(hit_at(r.hit(plane::through(vec3{ 0, 0, 1 }, vec3::back))) == 9);
|
||||
t.expects(hit_at(r.hit(plane::through(vec3{ 0, 0, 1 }, vec3::forward))) == 9);
|
||||
t.expects(hit_at(r.hit(plane::through(vec3{ 0, 0, 11 }, vec3::back))) == -1 && hit_at(r.hit(plane::through(vec3::zero, vec3::up))) == -1);
|
||||
# A sphere ahead is met at its surface, one begun within at 0, and one beside or behind not at all.
|
||||
t.expects(hit_at(r.hit(sphere{ vec3::zero, 2 })) == 8 && hit_at(ray{ vec3::zero, vec3::up }.hit(sphere{ vec3::zero, 2 })) == 0);
|
||||
t.expects(hit_at(r.hit(sphere{ vec3{ 3, 0, 0 }, 2 })) == -1 && hit_at(r.hit(sphere{ vec3{ 0, 0, 12 }, 1 })) == -1);
|
||||
# A box ahead is met at its face, one begun within at 0, and one beside or behind not at all.
|
||||
val b := bounds::around(vec3::zero, vec3::one);
|
||||
t.expects(hit_at(r.hit(b)) == 9 && hit_at(ray{ vec3::zero, vec3{ 1, 2, 3 } }.hit(b)) == 0);
|
||||
t.expects(hit_at(r.hit(bounds::around(vec3{ 3, 0, 0 }, vec3::one))) == -1 && hit_at(r.hit(bounds::around(vec3{ 0, 0, 12 }, vec3::one))) == -1);
|
||||
# Slanting in, it meets the box where it crosses x = -1.
|
||||
t.expects(near(hit_at(ray::toward(vec3{ -5, -5, 0 }, vec3::zero).hit(b)), 4 * math::sqrt(2f)));
|
||||
}
|
||||
|
||||
@test def culls_by_a_cameras_frustum(t: &mut tester): void {
|
||||
# Looking from z = 10 at the origin, a quarter turn wide, from 1 to 100 away.
|
||||
val f := frustum::of(mat4::perspective(math::pi as f32 / 2, 1, 1, 100) * mat4::look_at(vec3{ 0, 0, 10 }, vec3::zero));
|
||||
# Its planes face in, so the place it looks at is in front of each.
|
||||
for p in f.planes {
|
||||
t.expects(p.distance_to(vec3::zero) > 0 && near(p.normal.length, 1));
|
||||
}
|
||||
# The far plane is the difference of two rows nearly alike, so as near as an `f32` of 90 gets.
|
||||
t.expects(near(f.planes[4].distance_to(vec3{ 0, 0, 9 }), 0) && math::abs(f.planes[5].distance_to(vec3{ 0, 0, -90 })) < 1e-3);
|
||||
t.expects(f.contains(vec3::zero) && f.contains(vec3{ 9, 0, 0 }));
|
||||
t.expects(!f.contains(vec3{ 0, 0, 9.5 }) && !f.contains(vec3{ 0, 0, -95 }) && !f.contains(vec3{ 11, 0, 0 }) && !f.contains(vec3{ 0, -11, 0 }));
|
||||
# A box or sphere reaching in from beside is seen, and one wholly beside or behind isn't.
|
||||
t.expects(f.overlaps(bounds::around(vec3{ 11, 0, 0 }, vec3{ 2, 1, 1 })) && !f.overlaps(bounds::around(vec3{ 14, 0, 0 }, vec3{ 2, 1, 1 })));
|
||||
t.expects(f.overlaps(bounds::around(vec3{ 0, 0, 9.5 }, vec3::one)) && !f.overlaps(bounds::around(vec3{ 0, 0, 12 }, vec3::one)));
|
||||
t.expects(f.overlaps(sphere{ vec3{ 0, 0, 9.5 }, 1 }) && !f.overlaps(sphere{ vec3{ 0, 0, 11 }, 1 }));
|
||||
t.expects(f.overlaps(sphere{ vec3{ 0, 11, 0 }, 1.5 }) && !f.overlaps(sphere{ vec3{ 0, 13, 0 }, 1.5 }));
|
||||
# Made of its own planes, it's the same.
|
||||
val again := frustum::of_planes(f.planes);
|
||||
for i in 0..6u {
|
||||
t.expects(again.planes[i] == f.planes[i]);
|
||||
}
|
||||
# A projection alone gives the frustum in the camera's space, an orthographic one a box.
|
||||
val o := frustum::of(mat4::orthographic(-10, 10, -10, 10, 0, 50));
|
||||
t.expects(o.contains(vec3{ 9.5, -9.5, -49 }) && !o.contains(vec3{ 10.5, 0, -1 }) && !o.contains(vec3{ 0, 0, 1 }) && !o.contains(vec3{ 0, 0, -51 }));
|
||||
}
|
||||
|
||||
# The bits of each number, folded together, as FNV-1a folds bytes, wrapping
|
||||
# as it does.
|
||||
def fold(hash: u64, x: f32): u64 {
|
||||
var held := x;
|
||||
val bits := ((&mut held) as *mut u32)[0] as u64;
|
||||
return math::wrapping_mul(hash ^ bits, 0x100000001B3);
|
||||
}
|
||||
|
||||
def fold(hash: u64, v: vec3): u64 {
|
||||
return fold(fold(fold(hash, v.x), v.y), v.z);
|
||||
}
|
||||
|
||||
def fold(hash: u64, yes: bool): u64 {
|
||||
return math::wrapping_mul(hash ^ (yes ? 1u : 2u), 0x100000001B3);
|
||||
}
|
||||
|
||||
@test def gives_the_same_bits_as_lanes_or_not(t: &mut tester): void {
|
||||
var h: u64 = 0xCBF29CE484222325;
|
||||
val f := frustum::of(mat4::perspective(1.1, 1.5, 0.5, 60) * mat4::look_at(vec3{ 3, 4, 12 }, vec3{ 0, 1, 0 }));
|
||||
val turn := quat::axis_angle(vec3{ 0.2, 1, -0.4 }, 0.37);
|
||||
var p := vec3{ 0.3, -1.7, 2.9 };
|
||||
for i in 0..64u {
|
||||
p = turn * (p * 1.07 + vec3{ 0.11, -0.05, 0.2 });
|
||||
val b := bounds::around(p, (turn * vec3{ 0.5, 1.25, 0.75 }).abs());
|
||||
val s := sphere{ p * 0.5, 1.5 };
|
||||
val r := ray::toward(vec3{ 20, -3, 7 }, p);
|
||||
val wall := plane::of_points(p, p + vec3::right, p + turn * vec3::up);
|
||||
val moved := b.transformed(mat4::compose(p, turn, vec3{ 1, 2, 0.5 }));
|
||||
h = fold(fold(fold(h, moved.min), moved.max), b.closest(s.center));
|
||||
h = fold(fold(fold(h, hit_at(r.hit(b))), hit_at(r.hit(s))), hit_at(r.hit(wall)));
|
||||
h = fold(fold(fold(h, r.closest(s.center)), wall.distance_to(s.center)), wall.closest(vec3::zero));
|
||||
h = fold(fold(fold(h, f.contains(p)), f.overlaps(b)), f.overlaps(s));
|
||||
h = fold(fold(fold(h, b.overlaps(s)), s.overlaps(moved)), b.overlaps(moved));
|
||||
h = fold(fold(h, wall.side_of(b) == side::across), wall.side_of(s) == side::front);
|
||||
}
|
||||
val corners: vec3[3] = [p, p * 2, -p];
|
||||
h = fold(fold(h, sphere::of(bounds::of(corners[:])).radius), f.planes[3].normal);
|
||||
t.expects(h == expected_bits);
|
||||
}
|
||||
|
||||
# What `gives_the_same_bits_as_lanes_or_not` folds, as the machine works it a
|
||||
# number at a time.
|
||||
const expected_bits: u64 = 16338969092856802616;
|
||||
@@ -0,0 +1,101 @@
|
||||
#! Contains the tests of `physics2d`'s joints that slide and drive: a block on
|
||||
#! a prismatic joint slid by its motor to its limit, a cart riding wheels on
|
||||
#! their springs, driven by their motors, a body driven by a motor joint to a
|
||||
#! place and an angle, and one dragged by a mouse joint.
|
||||
|
||||
import std::testing;
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
import malunal::physics2d;
|
||||
|
||||
def within(a: f32, b: f32, by: f32): bool {
|
||||
return math::abs(a - b) <= by;
|
||||
}
|
||||
|
||||
def seconds(s: &mut space, count: f32): void {
|
||||
val n := (count * 60) as u32;
|
||||
for i in 0..n {
|
||||
s.step(1f / 60f);
|
||||
}
|
||||
}
|
||||
|
||||
@test def slides_along_prismatic_joints(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(vec2{ 0, 2 }) });
|
||||
_ = s.attach(block, collider{ shape = shape::box(vec2{ 0.5, 0.25 }) });
|
||||
val rail := s.slide(ground, block, vec2{ 0, 2 }, vec2{ 1, 0 });
|
||||
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);
|
||||
seconds(&mut s, 3);
|
||||
# Held to its rail against gravity, unturned, it stops at its limit.
|
||||
val at := s.pose_of(block);
|
||||
t.expects(within(at.at.x, 3, 0.02) && within(at.at.y, 2, 0.01) && within(at.turn.angle, 0, 0.01));
|
||||
t.expects(within(s.translation_of(rail), 3, 0.02));
|
||||
}
|
||||
|
||||
@test def rides_wheels(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
s.sleeping = false;
|
||||
val ground := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, -1 }) });
|
||||
_ = s.attach(ground, collider{ shape = shape::box(vec2{ 100, 1 }), friction = 0.9 });
|
||||
val cart := s.add(body{ at = pose::of(vec2{ 0, 1 }) });
|
||||
_ = s.attach(cart, collider{ shape = shape::box(vec2{ 1, 0.2 }) });
|
||||
var axles: joint_id[2] = [joint_id{}, joint_id{}];
|
||||
for i in 0..2u {
|
||||
val x := i == 0 ? -0.8f : 0.8f;
|
||||
val wheel := s.add(body{ at = pose::of(vec2{ x, 0.5 }) });
|
||||
_ = s.attach(wheel, collider{ shape = shape::circle(0.3), friction = 0.9 });
|
||||
axles[i] = s.wheel(cart, wheel, vec2{ x, 0.5 }, vec2{ 0, 1 });
|
||||
}
|
||||
seconds(&mut s, 1);
|
||||
# Resting on its springs, the cart's held above its wheels, level.
|
||||
val resting := s.pose_of(cart);
|
||||
t.expects(resting.at.y > 0.6 && resting.at.y < 1.1 && within(resting.turn.angle, 0, 0.02) && within(resting.at.x, 0, 0.05));
|
||||
# Their motors turning them clockwise, it drives right.
|
||||
for i in 0..2u {
|
||||
var made := s.joint_of(axles[i]);
|
||||
made.motor = true;
|
||||
made.motor_speed = -10;
|
||||
made.max_motor_force = 50;
|
||||
s.change(axles[i], made);
|
||||
}
|
||||
seconds(&mut s, 2);
|
||||
t.expects(s.pose_of(cart).at.x > 3 && within(s.pose_of(cart).turn.angle, 0, 0.1));
|
||||
}
|
||||
|
||||
@test def drives_bodies_by_motor_joints(t: &mut tester): void {
|
||||
var s := space::new(vec2::zero);
|
||||
val base := s.add(body{ kind = body_kind::static });
|
||||
val puck := s.add(body{ at = pose::of(vec2{ 1, 0 }) });
|
||||
_ = s.attach(puck, collider{ shape = shape::box(vec2{ 0.3, 0.3 }) });
|
||||
val driven := s.drive(base, puck, 50, 20);
|
||||
# Where it's driven, it stays; driven elsewhere, it goes, and turns.
|
||||
seconds(&mut s, 0.5);
|
||||
t.expects(within(s.pose_of(puck).at.x, 1, 0.01) && within(s.pose_of(puck).at.y, 0, 0.01));
|
||||
s.drive_to(driven, vec2{ 3, 2 }, 1);
|
||||
seconds(&mut s, 4);
|
||||
val at := s.pose_of(puck);
|
||||
t.expects(within(at.at.x, 3, 0.05) && within(at.at.y, 2, 0.05) && within(at.turn.angle, 1, 0.05));
|
||||
}
|
||||
|
||||
@test def drags_bodies_by_mouse_joints(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{ 20, 1 }) });
|
||||
val crate := s.add(body{ at = pose::of(vec2{ 0, 0.5 }) });
|
||||
_ = s.attach(crate, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) });
|
||||
# Grabbed by its top and pulled up and over, it hangs below the target.
|
||||
val hand := s.grab(ground, crate, vec2{ 0, 1 }, 1000);
|
||||
s.drive_to(hand, vec2{ 4, 3 });
|
||||
seconds(&mut s, 3);
|
||||
val at := s.pose_of(crate);
|
||||
t.expects(within(at.apply(vec2{ 0, 0.5 }).x, 4, 0.1) && within(at.apply(vec2{ 0, 0.5 }).y, 3, 0.1));
|
||||
t.expects(at.at.y < 3);
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
#! Contains the tests of `linear`: vectors of two, three, and four; rotations;
|
||||
#! matrices of three and four; projections; values read as lanes where they lie
|
||||
#! unaligned; and the same bits whether numbers are worked as lanes or one at a
|
||||
#! time, which `malunal.linear.tests` and `malunal.linear.lanes.tests` each
|
||||
#! check.
|
||||
|
||||
import std::testing;
|
||||
import std::json;
|
||||
import std::math;
|
||||
import std::serial;
|
||||
import malunal::linear;
|
||||
|
||||
# Whether two numbers are within a millionth or so of one another.
|
||||
def near(a: f32, b: f32): bool {
|
||||
return math::abs(a - b) < 1e-5;
|
||||
}
|
||||
|
||||
def near(a: vec3, b: vec3): bool {
|
||||
return near(a.x, b.x) && near(a.y, b.y) && near(a.z, b.z);
|
||||
}
|
||||
|
||||
def near(a: &mat4, b: &mat4): bool {
|
||||
for i in 0..16u {
|
||||
return false if !near(a.m[i], b.m[i]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
def near(a: &mat3, b: &mat3): bool {
|
||||
return near(a.x_axis, b.x_axis) && near(a.y_axis, b.y_axis) && near(a.z_axis, b.z_axis);
|
||||
}
|
||||
|
||||
@test def lays_out_its_numbers_as_lanes(t: &mut tester): void {
|
||||
t.expects(size_of::<vec2>() == 8 && size_of::<vec3>() == 16 && size_of::<vec4>() == 16 && size_of::<quat>() == 16);
|
||||
t.expects(size_of::<mat3>() == 48 && size_of::<mat4>() == 64);
|
||||
# Each is of `f32`s, so a field of one is wherever the struct holding it puts it.
|
||||
t.expects(align_of::<vec3>() == 4 && align_of::<mat4>() == 4);
|
||||
val v := vec3{ 1, 2, 3 };
|
||||
t.expects(v.x == 1 && v.y == 2 && v.z == 3 && vec3{ y = 5 } == vec3{ 0, 5, 0 });
|
||||
}
|
||||
|
||||
@test def adds_scales_and_measures_vectors(t: &mut tester): void {
|
||||
val a := vec3{ 1, 2, 3 };
|
||||
val b := vec3{ 4, -5, 6 };
|
||||
t.expects(a + b == vec3{ 5, -3, 9 } && a - b == vec3{ -3, 7, -3 } && -a == vec3{ -1, -2, -3 });
|
||||
t.expects(a * 2 == vec3{ 2, 4, 6 } && a * b == vec3{ 4, -10, 18 } && b / 2 == vec3{ 2, -2.5, 3 });
|
||||
t.expects(b / a == vec3{ 4, -2.5, 2 });
|
||||
t.expects(a.dot(b) == 12 && a.length_squared == 14 && near(a.length, math::sqrt(14f)));
|
||||
t.expects(near(a.normalized().length, 1) && vec3::zero.normalized() == vec3::zero);
|
||||
t.expects(near(vec3{ 0, 3, 4 }.distance(vec3::zero), 5));
|
||||
t.expects(a.lerp(b, 0.5) == vec3{ 2.5, -1.5, 4.5 });
|
||||
t.expects(a.min(b) == vec3{ 1, -5, 3 } && a.max(b) == vec3{ 4, 2, 6 } && b.abs() == vec3{ 4, 5, 6 });
|
||||
t.expects(vec3::right.cross(vec3::up) == vec3::back && vec3::up.cross(vec3::right) == vec3::forward);
|
||||
|
||||
val c := vec2{ 3, 4 };
|
||||
t.expects(c + vec2::one == vec2{ 4, 5 } && c * 2 == vec2{ 6, 8 } && c.length == 5 && c.dot(vec2{ 1, 1 }) == 7);
|
||||
t.expects(vec2{ 1, 0 }.cross(vec2{ 0, 1 }) == 1 && vec2{ 1, 0 }.perpendicular() == vec2{ 0, 1 });
|
||||
t.expects(near(c.normalized().length, 1) && c.lerp(vec2::zero, 0.5) == vec2{ 1.5, 2 });
|
||||
|
||||
val d := vec4{ 1, 2, 3, 4 };
|
||||
t.expects(d + vec4::one == vec4{ 2, 3, 4, 5 } && d * 0.5 == vec4{ 0.5, 1, 1.5, 2 } && d.dot(d) == 30);
|
||||
t.expects(d.xyz == a && -d == vec4{ -1, -2, -3, -4 } && near(d.normalized().length, 1));
|
||||
t.expects(d.min(vec4{ 2, 2, 2, 2 }) == vec4{ 1, 2, 2, 2 } && (d - vec4{ 5, 5, 5, 5 }).abs() == vec4{ 4, 3, 2, 1 });
|
||||
}
|
||||
|
||||
@test def turns_and_moves_vectors(t: &mut tester): void {
|
||||
val quarter: f32 = math::pi as f32 / 2;
|
||||
# A quarter turn about y takes x to -z, and about z takes x to y.
|
||||
t.expects(near(quat::axis_angle(vec3::up, quarter) * vec3::right, vec3{ 0, 0, -1 }));
|
||||
t.expects(near(quat::axis_angle(vec3::back, quarter) * vec3::right, vec3::up));
|
||||
# Turning by a rotation and then back changes nothing.
|
||||
val q := quat::euler(0.3, -1.1, 0.7);
|
||||
t.expects(near(q.length, 1) && near(q.inverse() * (q * vec3{ 1, 2, 3 }), vec3{ 1, 2, 3 }));
|
||||
# Turning by one rotation after another is turning by their product.
|
||||
val r := quat::axis_angle(vec3{ 1, 1, 0 }, 0.4);
|
||||
t.expects(near((q * r) * vec3{ 1, 2, 3 }, q * (r * vec3{ 1, 2, 3 })));
|
||||
# A matrix composed of a transform does what the transform does.
|
||||
val m := mat4::compose(vec3{ 5, 0, -2 }, q, vec3{ 2, 2, 2 });
|
||||
t.expects(near(m.transform_point(vec3{ 1, 0, 0 }), vec3{ 5, 0, -2 } + q * vec3{ 2, 0, 0 }));
|
||||
t.expects(near(m.transform_vector(vec3{ 1, 0, 0 }), q * vec3{ 2, 0, 0 }));
|
||||
t.expects(near(&(m * m.inverse()), &mat4::identity));
|
||||
val p := m * vec4{ 1, 0, 0, 1 };
|
||||
t.expects(near(p.xyz, m.transform_point(vec3{ 1, 0, 0 })) && near(p.w, 1));
|
||||
# Halfway between no turn and a third of one is a sixth.
|
||||
val half := quat::identity.slerp(quat::axis_angle(vec3::up, 4 * quarter / 3), 0.5);
|
||||
t.expects(near(half * vec3::right, vec3{ 0.5, 0, -math::sqrt(3f) / 2 }));
|
||||
# Looking at a place turns -z toward it.
|
||||
t.expects(near(quat::looking(vec3{ 1, 0, 0 }) * vec3::forward, vec3::right));
|
||||
# The shortest turn between two directions takes one to the other, the
|
||||
# same one not at all, and opposite ones by half a turn.
|
||||
t.expects(near(quat::between(vec3::up, vec3{ 0.6, 0.8, 0 }) * vec3::up, vec3{ 0.6, 0.8, 0 }));
|
||||
t.expects(near(quat::between(vec3::right, vec3::right).w, 1));
|
||||
t.expects(near(quat::between(vec3::up, vec3{ 0, -1, 0 }) * vec3::up, vec3{ 0, -1, 0 }));
|
||||
# Turning y to a way across it leaves what's across both where it was.
|
||||
val tilted := quat::between(vec3::up, vec3{ 0, 0.6, 0.8 });
|
||||
t.expects(near(tilted * vec3::right, vec3::right) && near(tilted * vec3::back, vec3{ 0, -0.8, 0.6 }));
|
||||
}
|
||||
|
||||
@test def projects_into_clip_space(t: &mut tester): void {
|
||||
# The near plane is depth 0, the far one 1, and y points up.
|
||||
val p := mat4::perspective(math::pi as f32 / 2, 2, 1, 100);
|
||||
val near_point := p * vec4{ 0, 1, -1, 1 };
|
||||
t.expects(near(near_point.z / near_point.w, 0) && near(near_point.y / near_point.w, 1));
|
||||
val far_point := p * vec4{ 0, 0, -100, 1 };
|
||||
t.expects(near(far_point.z / far_point.w, 1));
|
||||
val o := mat4::orthographic(0, 200, 100, 0, -1, 1);
|
||||
val corner := o * vec4{ 200, 100, 0, 1 };
|
||||
t.expects(near(corner.x, 1) && near(corner.y, -1) && near(corner.z, 0.5));
|
||||
# A camera looking at the origin from +z sees it straight ahead.
|
||||
val view := mat4::look_at(vec3{ 0, 0, 10 }, vec3::zero);
|
||||
t.expects(near(view.transform_point(vec3::zero), vec3{ 0, 0, -10 }));
|
||||
# A projection is undone, and a scale of zero can't be.
|
||||
val clip := p * view;
|
||||
t.expects(near(&(clip * clip.inverse()), &mat4::identity) && near(&(clip.inverse() * clip), &mat4::identity));
|
||||
t.expects(mat4::scaling(vec3::zero).inverse() == mat4{});
|
||||
}
|
||||
|
||||
@test def works_matrices_of_three(t: &mut tester): void {
|
||||
val q := quat::euler(0.3, -1.1, 0.7);
|
||||
val r := mat3::rotation(q);
|
||||
# A rotation's matrix turns as the rotation does, and its transpose back.
|
||||
t.expects(near(r * vec3{ 1, 2, 3 }, q * vec3{ 1, 2, 3 }) && near(&(r * r.transposed()), &mat3::identity));
|
||||
t.expects(near(r.determinant, 1) && near(&r.inverse(), &r.transposed()));
|
||||
# The rotation of a rotation's matrix is the rotation.
|
||||
val back := quat::of_basis(r.x_axis, r.y_axis, r.z_axis);
|
||||
t.expects(near(back * vec3{ 1, 2, 3 }, q * vec3{ 1, 2, 3 }));
|
||||
val s := mat3::scaling(vec3{ 2, 4, 8 });
|
||||
t.expects(s.determinant == 64 && near(&(s * s.inverse()), &mat3::identity) && mat3::scaling(vec3::zero).inverse() == mat3{});
|
||||
t.expects(near(&(r * s), &mat4::compose(vec3::zero, q, vec3{ 2, 4, 8 }).linear_part()));
|
||||
t.expects(s + s == s * 2 && (s - s) == mat3{});
|
||||
}
|
||||
|
||||
# A value lying where a struct puts it, four bytes past a 16-byte boundary.
|
||||
struct placed {
|
||||
var tag: u32;
|
||||
var at: vec3;
|
||||
var turn: quat;
|
||||
var by: mat4;
|
||||
}
|
||||
|
||||
@test def reads_values_where_they_lie(t: &mut tester): void {
|
||||
var p := placed{ tag = 7, at = vec3{ 1, 2, 3 }, turn = quat::axis_angle(vec3::back, math::pi as f32 / 2), by = mat4::translation(vec3{ 1, 1, 1 }) };
|
||||
p.at = p.at * 2 + vec3::one;
|
||||
t.expects(p.tag == 7 && p.at == vec3{ 3, 5, 7 });
|
||||
t.expects(near(p.turn * vec3::right, vec3::up) && near(p.by.transform_point(p.at), vec3{ 4, 6, 8 }));
|
||||
t.expects(near(&(p.by * p.by), &mat4::translation(vec3{ 2, 2, 2 })));
|
||||
# Its lanes, and a vector of lanes.
|
||||
val l := p.at.lanes();
|
||||
t.expects(l[0] == 3 && l[1] == 5 && l[2] == 7 && vec3::of_lanes(l) == p.at);
|
||||
t.expects(vec4::of_lanes(f32x4::of([1.0, 2.0, 3.0, 4.0])) == vec4{ 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
@serial
|
||||
struct posed {
|
||||
@serial var at: vec3;
|
||||
@serial var turn: quat;
|
||||
@serial var by: mat3;
|
||||
@serial var size: vec2;
|
||||
@serial var tint: vec4;
|
||||
}
|
||||
|
||||
@test def writes_and_reads_its_types(t: &mut tester): void {
|
||||
# Each is written by its numbers' names, as the shortest text reading back as
|
||||
# each `f32`, but a `mat4`, as its 16 numbers; `vec3`'s fourth isn't written.
|
||||
val written := posed{
|
||||
at = vec3{ 1, 0.1, -2.5 }, turn = quat::axis_angle(vec3::up, 1), by = mat3::scaling(vec3{ 2, 3, 4 }),
|
||||
size = vec2{ 0.5, 8 }, tint = vec4{ 1, 0.25, 0, 1 },
|
||||
};
|
||||
val text := json::to_text(&written) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(text.span.find("\"at\":{\"x\":1,\"y\":0.1,\"z\":-2.5}") is some(_));
|
||||
t.expects(text.span.find("\"size\":{\"x\":0.5,\"y\":8}") is some(_) && text.span.find("_w") is none);
|
||||
val back := json::from_text::<posed>(text.span) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(back.at == written.at && back.turn == written.turn && back.by == written.by);
|
||||
t.expects(back.size == written.size && back.tint == written.tint);
|
||||
|
||||
val moved := mat4::compose(vec3{ 1, 2, 3 }, written.turn, vec3{ 2, 2, 2 });
|
||||
val matrix := json::to_text(&moved) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(matrix.span.starts_with("[") && json::from_text::<mat4>(matrix.span).value == moved);
|
||||
t.expects(json::from_text::<mat4>("[1, 0, 0, 1]").error.kind == json::error_kind::wrong_kind);
|
||||
|
||||
# What's left out reads as each number's default, where asked: a rotation's
|
||||
# `w` of 1, and 0 for the rest.
|
||||
val lenient := serial::options{ defaults = true };
|
||||
t.expects(json::from_text::<quat>("{}", lenient).value == quat::identity);
|
||||
t.expects(json::from_text::<vec3>("{\"y\": 2}", lenient).value == vec3{ 0, 2, 0 });
|
||||
}
|
||||
|
||||
# The bits of each number, folded together, as FNV-1a folds bytes, wrapping
|
||||
# as it does.
|
||||
def fold(hash: u64, x: f32): u64 {
|
||||
var held := x;
|
||||
val bits := ((&mut held) as *mut u32)[0] as u64;
|
||||
return math::wrapping_mul(hash ^ bits, 0x100000001B3);
|
||||
}
|
||||
|
||||
def fold(hash: u64, v: vec3): u64 {
|
||||
return fold(fold(fold(hash, v.x), v.y), v.z);
|
||||
}
|
||||
|
||||
def fold(hash: u64, q: quat): u64 {
|
||||
return fold(fold(fold(fold(hash, q.x), q.y), q.z), q.w);
|
||||
}
|
||||
|
||||
@test def gives_the_same_bits_as_lanes_or_not(t: &mut tester): void {
|
||||
var h: u64 = 0xCBF29CE484222325;
|
||||
var v := vec3{ 0.1, -2.3, 4.5 };
|
||||
var q := quat::euler(0.2, 0.9, -0.4);
|
||||
var m := mat4::compose(vec3{ 1, 2, 3 }, q, vec3{ 1.5, 0.5, 2 });
|
||||
val turn := quat::axis_angle(vec3{ 0.3, 1, -0.2 }, 0.05);
|
||||
for i in 0..64u {
|
||||
v = (v + vec3{ 0.25, 0.5, -0.125 }) * 0.9;
|
||||
v = q * v;
|
||||
v = m.transform_point(v) - m.transform_vector(v).normalized();
|
||||
q = (q * turn).normalized();
|
||||
m = m * mat4::rotation(turn);
|
||||
h = fold(fold(fold(h, v), q), v.dot(vec3{ 1, 2, 3 }));
|
||||
h = fold(h, (m * vec4{ v.x, v.y, v.z, 1 }).w);
|
||||
h = fold(h, (mat3::rotation(q) * v).lerp(v.min(vec3::one).abs(), 0.25));
|
||||
}
|
||||
h = fold(fold(h, q.slerp(quat::identity, 0.3)), m.inverse().m[7]);
|
||||
t.expects(h == expected_bits);
|
||||
}
|
||||
|
||||
# What `gives_the_same_bits_as_lanes_or_not` folds, as the machine works it a
|
||||
# number at a time.
|
||||
const expected_bits: u64 = 2409136494821621507;
|
||||
@@ -0,0 +1,11 @@
|
||||
#! Contains the `main` that runs the tests of `linear`, `geometry`,
|
||||
#! `photometry`, and `physics2d`, each in a copy of the program of its own.
|
||||
#! `blossom test` runs them, and `-- --only TEXT` those whose name holds the
|
||||
#! text.
|
||||
|
||||
import system;
|
||||
import std::testing;
|
||||
|
||||
def main(args: span<strview>): void {
|
||||
exit(tester::run(args));
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
#! 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);
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
#! 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);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#! Contains the tests of `photometry`: lumens and candela of lights every way and
|
||||
#! within cones, lux and nits of surfaces, exposure values and what they
|
||||
#! multiply light by, and the colors of temperatures.
|
||||
|
||||
import std::testing;
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
import malunal::photometry;
|
||||
|
||||
# Whether two numbers are within so much of one another, as a fraction of the
|
||||
# greater: a hundred-thousandth, at first.
|
||||
def close(a: f32, b: f32, within: f32 = 1e-5): bool {
|
||||
return math::abs(a - b) <= within * math::max(math::abs(a), math::abs(b));
|
||||
}
|
||||
|
||||
@test def converts_lumens_and_candela(t: &mut tester): void {
|
||||
val pi := math::pi as f32;
|
||||
# A sphere's solid angle, a hemisphere's, and a cone of 60 degrees each way.
|
||||
t.expects(close(solid_angle_of(pi), 4 * pi) && close(solid_angle_of(pi / 2), 2 * pi) && close(solid_angle_of(pi / 3), pi));
|
||||
# A bare bulb's 800 lumens are about 63.7 candela every way; gathered into
|
||||
# the cone of 60 degrees, about 255.
|
||||
t.expects(close(candela_of_lumens(lumens::bulb_60w), 63.66198) && close(candela_of_lumens(800, pi / 3), 254.6479));
|
||||
t.expects(close(lumens_of_candela(candela::candle), lumens::candle) && close(lumens_of_candela(candela_of_lumens(300, 0.2), 0.2), 300));
|
||||
# An LED of 8 watts gives what a 60-watt incandescent bulb did.
|
||||
t.expects(close(lumens_of_watts(8, efficacy::led), lumens::bulb_60w) && close(watts_of_lumens(lumens::bulb_60w, efficacy::incandescent), 57.14286));
|
||||
}
|
||||
|
||||
@test def converts_lux_and_nits(t: &mut tester): void {
|
||||
# A light of 100 candela gives a surface 2 meters away 25 lux, and gives 4
|
||||
# lux 5 meters away.
|
||||
t.expects(close(lux_of_candela(100, 2), 25) && close(distance_of_lux(100, 4), 5));
|
||||
# A white surface lit by π lux gives back a nit; a grey one half as much.
|
||||
t.expects(close(nits_of_lux(math::pi as f32), 1) && close(nits_of_lux(100, 0.5), 15.915494));
|
||||
t.expects(close(lux_of_nits(nits_of_lux(lux::office, 0.18), 0.18), lux::office));
|
||||
# A clear sky of 7,000 nits lights the ground with about 22,000 lux.
|
||||
t.expects(close(lux_of_nits(nits::clear_sky), 21991.15));
|
||||
}
|
||||
|
||||
@test def exposes_by_exposure_values(t: &mut tester): void {
|
||||
# f/16 at a hundredth of a second, as the sunny 16 rule has it, at ISO 100,
|
||||
# and a step less at ISO 200.
|
||||
t.expects(close(ev100_of_camera(16, 0.01), 14.643856) && close(ev100_of_camera(16, 0.01, 200), 13.643856));
|
||||
t.expects(close(ev100_of_lux(lux::direct_sunlight), 15.287712) && close(ev100_of_nits(12.5), 6.643856));
|
||||
t.expects(close(exposure_of_ev100(0), 1.0 / 1.2) && close(exposure_of_ev100(1), 1.0 / 2.4));
|
||||
t.expects(close(ev100_of_exposure(exposure_of_ev100(9.7)), 9.7) && close(ev100_of_exposure(1), -0.2630344));
|
||||
# Exposed as a meter where it's lit reads, a white surface records as two
|
||||
# thirds of the most, and an 18% grey card about an eighth: middle grey.
|
||||
for light in [lux::full_moon, lux::office, lux::direct_sunlight] {
|
||||
val exposure := exposure_of_ev100(ev100_of_lux(light));
|
||||
t.expects(close(nits_of_lux(light) * exposure, 0.6631456, 1e-4) && close(nits_of_lux(light, 0.18) * exposure, 0.1193662, 1e-4));
|
||||
}
|
||||
}
|
||||
|
||||
@test def colors_temperatures(t: &mut tester): void {
|
||||
# A warm bulb's is orange, daylight's about white, and a blue sky's blue.
|
||||
val warm := color_of_temperature(2700);
|
||||
t.expects(warm.x == 1 && close(warm.y, 0.4159, 1e-3) && close(warm.z, 0.0984, 1e-2));
|
||||
val day := color_of_temperature(6500);
|
||||
t.expects(day.x == 1 && day.y > 0.93 && day.z > 0.98);
|
||||
val sky := color_of_temperature(10000);
|
||||
t.expects(sky.z == 1 && sky.y > sky.x && close(sky.x, 0.6095, 1e-3));
|
||||
# Hotter is bluer all the way.
|
||||
var last := color_of_temperature(1000);
|
||||
for k in 1..40u {
|
||||
val next := color_of_temperature(1000 * (k + 1) as f32);
|
||||
t.expects(next.z / next.x >= last.z / last.x);
|
||||
last = next;
|
||||
}
|
||||
# Past either end, as either.
|
||||
t.expects(color_of_temperature(500) == color_of_temperature(1000) && color_of_temperature(90000) == color_of_temperature(40000));
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
#! 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::<vec2>::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.span) 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::<aabb>::new();
|
||||
var leaves := vector::<u32>::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::<u64>::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<manifold> {
|
||||
var found := vector::<manifold>::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 })).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 }));
|
||||
}
|
||||
@@ -0,0 +1,640 @@
|
||||
#! Contains the tests of `physics3d`: poses; hulls of boxes, of places, and of
|
||||
#! triangles, their faces facing out; shapes, their boxes, and what they hold;
|
||||
#! manifolds between spheres, capsules, and hulls, a box resting on another at
|
||||
#! four points, on its edge at two, on its corner at one, and edge across edge
|
||||
#! at one; rays meeting each shape; a tree of boxes, sound as it's changed,
|
||||
#! finding what a brute search finds; and a mesh of triangles, met but for the
|
||||
#! edges its triangles share flat, and rays meeting it.
|
||||
|
||||
import std::testing;
|
||||
import std::containers;
|
||||
import std::math;
|
||||
import malunal::geometry;
|
||||
import malunal::linear;
|
||||
import malunal::physics3d;
|
||||
|
||||
# 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: vec3, b: vec3): bool {
|
||||
return near(a.x, b.x) && near(a.y, b.y) && near(a.z, b.z);
|
||||
}
|
||||
|
||||
def quarter_about(axis: vec3): quat {
|
||||
return quat::axis_angle(axis, math::pi as f32 / 2);
|
||||
}
|
||||
|
||||
@test def turns_and_places_by_poses(t: &mut tester): void {
|
||||
# x turned a quarter about y, counterclockwise as y points at the viewer, is
|
||||
# -z.
|
||||
val at := pose::of(vec3{ 3, 1, 0 }, quarter_about(vec3::up));
|
||||
t.expects(near(at.apply(vec3{ 1, 0, 0 }), vec3{ 3, 1, -1 }) && near(at.unapply(vec3{ 3, 1, -1 }), vec3{ 1, 0, 0 }));
|
||||
t.expects(near(at.rotate(vec3{ 1, 0, 0 }), vec3{ 0, 0, -1 }) && near(at.unrotate(vec3{ 0, 0, -1 }), vec3{ 1, 0, 0 }));
|
||||
# A pose within another, found again within it, and undone.
|
||||
val inner := pose::of(vec3{ -2, 0.5, 1 }, quat::axis_angle(vec3{ 1, 1, 0 }, 0.7));
|
||||
val both := at * inner;
|
||||
val p := vec3{ 1, 2, 3 };
|
||||
t.expects(near(both.apply(p), at.apply(inner.apply(p))));
|
||||
val found := at.unapply_pose(both);
|
||||
t.expects(near(found.at, inner.at) && near(found.apply(p), inner.apply(p)));
|
||||
t.expects(near(at.inverse().apply(at.apply(p)), p) && near((at * at.inverse()).apply(p), p));
|
||||
}
|
||||
|
||||
# Each face's corners on its plane, counterclockwise about its normal, and
|
||||
# every corner of the hull behind it.
|
||||
def faces_out(h: &hull): bool {
|
||||
for f in 0..h.faces {
|
||||
val n := h.normal(f);
|
||||
val offset := h.offset(f);
|
||||
return false if !near(n.length, 1);
|
||||
val size := h.face_size(f);
|
||||
for k in 0..size {
|
||||
val a := h.point(h.corner(f, k));
|
||||
val b := h.point(h.corner(f, (k + 1) % size));
|
||||
val c := h.point(h.corner(f, (k + 2) % size));
|
||||
return false if !near(n.dot(a), offset) || (b - a).cross(c - b).dot(n) < -1e-4;
|
||||
}
|
||||
for i in 0..h.count {
|
||||
return false if n.dot(h.point(i)) > offset + 1e-4;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@test def makes_hulls(t: &mut tester): void {
|
||||
# A box's faces, +x, -x, +y, -y, +z, and -z, each edge between two at right
|
||||
# angles.
|
||||
val box := hull::box(vec3{ 2, 1, 0.5 });
|
||||
t.expects(box.count == 8 && box.faces == 6 && box.edges == 12 && faces_out(&box) && !box.is_flat);
|
||||
t.expects(near(box.normal(0), vec3{ 1, 0, 0 }) && near(box.offset(0), 2) && near(box.normal(3), vec3{ 0, -1, 0 }) && near(box.offset(4), 0.5));
|
||||
var square := true;
|
||||
for e in 0..box.edges {
|
||||
val sides := box.edge_faces(e);
|
||||
square = square && sides[0] != sides[1] && near(box.normal(sides[0]).dot(box.normal(sides[1])), 0);
|
||||
}
|
||||
t.expects(square && box.point(box.support(vec3{ 1, 1, 1 })) == vec3{ 2, 1, 0.5 });
|
||||
# Turned and moved, its faces still face out.
|
||||
val turned := hull::box(vec3{ 2, 1, 0.5 }, vec3{ 5, 0, 0 }, quat::axis_angle(vec3{ 1, 2, 3 }, 0.9));
|
||||
t.expects(faces_out(&turned) && near(turned.center, vec3{ 5, 0, 0 }));
|
||||
|
||||
# Around a cube's corners, with places within it, on its faces, and along
|
||||
# its edges: its 8 corners, 6 square faces, and 12 edges.
|
||||
val places: vec3[12] = [
|
||||
vec3{ 0.3, -0.2, 0.5 }, vec3{ -1, -1, -1 }, vec3{ 1, -1, -1 }, vec3{ -1, 1, -1 }, vec3{ 1, 1, -1 }, vec3{ 0, 0, 0 },
|
||||
vec3{ -1, -1, 1 }, vec3{ 1, -1, 1 }, vec3{ -1, 1, 1 }, vec3{ 1, 1, 1 }, vec3{ 1, 0, 0 }, vec3{ 1, 1, 0 },
|
||||
];
|
||||
val cube := hull::of(places[:]).expect("A cube is made.");
|
||||
t.expects(cube.count == 8 && cube.faces == 6 && cube.edges == 12 && faces_out(&cube));
|
||||
var quads := true;
|
||||
for f in 0..cube.faces {
|
||||
quads = quads && cube.face_size(f) == 4;
|
||||
}
|
||||
t.expects(quads && near(cube.center, vec3::zero));
|
||||
# A tetrahedron, and an icosahedron's 12 corners, of 20 triangles.
|
||||
val corners: vec3[4] = [vec3{ 0, 0, 0 }, vec3{ 1, 0, 0 }, vec3{ 0, 1, 0 }, vec3{ 0, 0, 1 }];
|
||||
val tetrahedron := hull::of(corners[:]).expect("A tetrahedron is made.");
|
||||
t.expects(tetrahedron.count == 4 && tetrahedron.faces == 4 && tetrahedron.edges == 6 && faces_out(&tetrahedron));
|
||||
val phi := (1 + math::sqrt(5f)) / 2;
|
||||
var ico := vector::<vec3>::new();
|
||||
for i in 0..4u {
|
||||
val a: f32 = (i & 1) != 0 ? 1 : -1;
|
||||
val b: f32 = (i & 2) != 0 ? phi : -phi;
|
||||
ico.append(vec3{ 0, a, b });
|
||||
ico.append(vec3{ a, b, 0 });
|
||||
ico.append(vec3{ b, 0, a });
|
||||
}
|
||||
val twenty := hull::of(ico.span).expect("An icosahedron is made.");
|
||||
t.expects(twenty.count == 12 && twenty.faces == 20 && twenty.edges == 30 && faces_out(&twenty));
|
||||
|
||||
# Flat places hold no volume, and 40 corners are too many.
|
||||
val flat: vec3[5] = [vec3{ 0, 0, 0 }, vec3{ 1, 0, 0 }, vec3{ 1, 1, 0 }, vec3{ 0, 1, 0 }, vec3{ 0.5, 0.5, 0 }];
|
||||
t.expects(hull::of(flat[:]) is none && hull::of(corners[0:3]) is none);
|
||||
var ball := vector::<vec3>::new();
|
||||
for i in 0..40u {
|
||||
val y := 1 - 2 * (i as f32 + 0.5) / 40;
|
||||
val r := math::sqrt(1 - y * y);
|
||||
val theta := i as f32 * 2.39996;
|
||||
ball.append(vec3{ r * math::cos(theta), y, r * math::sin(theta) });
|
||||
}
|
||||
t.expects(hull::of(ball.span) is none);
|
||||
|
||||
# A triangle is flat: a face each way, and each edge between them.
|
||||
val triangle := hull::triangle(vec3{ 0, 0, 0 }, vec3{ 1, 0, 0 }, vec3{ 0, 0, -1 });
|
||||
t.expects(triangle.is_flat && triangle.count == 3 && triangle.faces == 2 && triangle.edges == 3);
|
||||
t.expects(near(triangle.normal(0), vec3{ 0, 1, 0 }) && near(triangle.normal(1), vec3{ 0, -1, 0 }));
|
||||
}
|
||||
|
||||
@test def makes_shapes(t: &mut tester): void {
|
||||
t.expects(shape::capsule(vec3{ 1, 1, 1 }, vec3{ 1, 1, 1 }, 0.5).kind == shape_kind::sphere);
|
||||
val pill := shape::pill(1, 3);
|
||||
t.expects(pill.kind == shape_kind::capsule && near(pill.a, vec3{ 0, -1, 0 }) && near(pill.b, vec3{ 0, 1, 0 }) && near(pill.radius, 0.5));
|
||||
# The box holding a shape where a pose places it.
|
||||
val box := shape::box(vec3{ 2, 1, 0.5 });
|
||||
val turned := box.bounds(pose::of(vec3{ 10, 0, 0 }, quarter_about(vec3::back)));
|
||||
t.expects(near(turned.min, vec3{ 9, -2, -0.5 }) && near(turned.max, vec3{ 11, 2, 0.5 }));
|
||||
val held := pill.bounds(pose::of(vec3{ 0, 5, 0 }));
|
||||
t.expects(near(held.min, vec3{ -0.5, 3.5, -0.5 }) && near(held.max, vec3{ 0.5, 6.5, 0.5 }));
|
||||
# What they hold.
|
||||
t.expects(pill.contains(pose::identity, vec3{ 0.4, 1.2, 0 }) && !pill.contains(pose::identity, vec3{ 0.45, 1.4, 0 }));
|
||||
t.expects(box.contains(pose::identity, vec3{ 1.9, 0.9, 0.4 }) && !box.contains(pose::identity, vec3{ 2.1, 0, 0 }));
|
||||
t.expects(box.contains(pose::of(vec3{ 10, 0, 0 }, quarter_about(vec3::back)), vec3{ 10.9, 1.9, 0 }));
|
||||
t.expects(shape::hull_of(corners_of_cube()[:]) is some(s) && s.kind == shape_kind::hull);
|
||||
}
|
||||
|
||||
def corners_of_cube(): vec3[8] {
|
||||
return [
|
||||
vec3{ -1, -1, -1 }, vec3{ 1, -1, -1 }, vec3{ -1, 1, -1 }, vec3{ 1, 1, -1 },
|
||||
vec3{ -1, -1, 1 }, vec3{ 1, -1, 1 }, vec3{ -1, 1, 1 }, vec3{ 1, 1, 1 },
|
||||
];
|
||||
}
|
||||
|
||||
@test def collides_spheres_and_capsules(t: &mut tester): void {
|
||||
val ball := shape::sphere(0.5);
|
||||
val other := shape::sphere(1);
|
||||
val met := collide(&ball, pose::identity, &other, pose::of(vec3{ 1.2, 0, 0 })).expect("They meet.");
|
||||
t.expects(met.count == 1 && near(met.normal, vec3{ 1, 0, 0 }) && near(met.points[0].depth, 0.3) && near(met.points[0].point, vec3{ 0.35, 0, 0 }));
|
||||
t.expects(collide(&ball, pose::identity, &other, pose::of(vec3{ 1.6, 0, 0 })) is none);
|
||||
t.expects(collide(&ball, pose::identity, &other, pose::of(vec3{ 1.5, 0, 0 })) is some(m) && near(m.depth, 0));
|
||||
# Within a margin, apart, at a depth below 0.
|
||||
t.expects(collide(&ball, pose::identity, &other, pose::of(vec3{ 1.6, 0, 0 }), 0.2) is some(m) && near(m.depth, -0.1));
|
||||
|
||||
# Capsules lying along one another meet at two points; crossing, at one.
|
||||
val lying := shape::capsule(vec3{ -0.5, 0, 0 }, vec3{ 0.5, 0, 0 }, 0.5);
|
||||
val beside := collide(&lying, pose::identity, &lying, pose::of(vec3{ 0.2, 0.8, 0 })).expect("They lie together.");
|
||||
t.expects(beside.count == 2 && near(beside.normal, vec3{ 0, 1, 0 }) && near(beside.points[0].depth, 0.2) && near(beside.points[1].depth, 0.2));
|
||||
t.expects(near(beside.points[0].point, vec3{ -0.3, 0.4, 0 }) && near(beside.points[1].point, vec3{ 0.5, 0.4, 0 }));
|
||||
val crossing := collide(&lying, pose::identity, &lying, pose::of(vec3{ 0, 0.8, 0 }, quarter_about(vec3::up))).expect("They cross.");
|
||||
t.expects(crossing.count == 1 && near(crossing.normal, vec3{ 0, 1, 0 }) && near(crossing.depth, 0.2) && near(crossing.points[0].point, vec3{ 0, 0.4, 0 }));
|
||||
# A ball by a capsule's side.
|
||||
val pill := shape::capsule(vec3{ -1, 0, 0 }, vec3{ 1, 0, 0 }, 0.25);
|
||||
val side := collide(&pill, pose::identity, &ball, pose::of(vec3{ 0.3, 0, 0.7 })).expect("It meets the capsule's side.");
|
||||
t.expects(near(side.normal, vec3{ 0, 0, 1 }) && near(side.depth, 0.05));
|
||||
}
|
||||
|
||||
@test def collides_spheres_and_capsules_with_hulls(t: &mut tester): void {
|
||||
val slab := shape::box(vec3{ 1, 0.5, 1 });
|
||||
val ball := shape::sphere(0.5);
|
||||
# On the box's face, by its edge, by its corner, and within it.
|
||||
val above := collide(&slab, pose::identity, &ball, pose::of(vec3{ 0, 0.9, 0 })).expect("It rests on the box.");
|
||||
t.expects(near(above.normal, vec3{ 0, 1, 0 }) && near(above.depth, 0.1) && near(above.points[0].point, vec3{ 0, 0.45, 0 }));
|
||||
val edge := collide(&slab, pose::identity, &ball, pose::of(vec3{ 1.3, 0.8, 0 })).expect("It meets the edge.");
|
||||
val diagonal := math::sqrt(0.5f);
|
||||
t.expects(near(edge.normal, vec3{ diagonal, diagonal, 0 }) && near(edge.depth, 0.5 - math::sqrt(0.18f)));
|
||||
val corner := collide(&slab, pose::identity, &ball, pose::of(vec3{ 1.2, 0.7, 1.2 })).expect("It meets the corner.");
|
||||
val third := math::sqrt(1f / 3f);
|
||||
t.expects(near(corner.normal, vec3{ third, third, third }) && near(corner.depth, 0.5 - math::sqrt(0.12f)));
|
||||
t.expects(collide(&slab, pose::identity, &ball, pose::of(vec3{ 1.3, 0.8, 1.3 })) is none);
|
||||
val small := shape::sphere(0.1);
|
||||
val within := collide(&slab, pose::identity, &small, pose::of(vec3{ 0.8, 0, 0 })).expect("It's within the box.");
|
||||
t.expects(near(within.normal, vec3{ 1, 0, 0 }) && near(within.depth, 0.3));
|
||||
val back := collide(&small, pose::of(vec3{ 0.8, 0, 0 }), &slab, pose::identity).expect("It's within the box.");
|
||||
t.expects(near(back.normal, vec3{ -1, 0, 0 }) && near(back.depth, 0.3));
|
||||
# The box turned, the ball over its face.
|
||||
val tilted := collide(&slab, pose::of(vec3::zero, quarter_about(vec3::back)), &ball, pose::of(vec3{ -0.9, 0, 0 })).expect("It meets the turned box.");
|
||||
t.expects(near(tilted.normal, vec3{ -1, 0, 0 }) && near(tilted.depth, 0.1));
|
||||
|
||||
# A capsule lying on the box meets it at two points; standing by its edge,
|
||||
# at one; and stood within it, by the face it's nearest.
|
||||
val lying := shape::capsule(vec3{ -0.5, 0, 0 }, vec3{ 0.5, 0, 0 }, 0.5);
|
||||
val flat := collide(&slab, pose::identity, &lying, pose::of(vec3{ 0, 0.9, 0 })).expect("The capsule lies on the box.");
|
||||
t.expects(flat.count == 2 && near(flat.normal, vec3{ 0, 1, 0 }) && near(flat.points[0].depth, 0.1) && near(flat.points[0].point.y, 0.45));
|
||||
val standing := shape::capsule(vec3{ 0, 0, 0 }, vec3{ 0, 1, 0 }, 0.5);
|
||||
val by := collide(&slab, pose::identity, &standing, pose::of(vec3{ 1.3, 0.8, 0 })).expect("The capsule's end meets the edge.");
|
||||
t.expects(by.count == 1 && near(by.normal, vec3{ diagonal, diagonal, 0 }) && near(by.depth, 0.5 - math::sqrt(0.18f)));
|
||||
t.expects(!overlaps(&slab, pose::identity, &standing, pose::of(vec3{ 1.4, 0.9, 0 })));
|
||||
val stood := collide(&slab, pose::identity, &standing, pose::of(vec3{ 0, 0.3, 0 })).expect("The capsule stands within the box.");
|
||||
t.expects(stood.count == 1 && near(stood.normal, vec3{ 0, 1, 0 }) && near(stood.depth, 0.7) && near(stood.points[0].point, vec3{ 0, 0.15, 0 }));
|
||||
# The other way around, the normal is turned around.
|
||||
val under := collide(&lying, pose::of(vec3{ 0, 0.9, 0 }), &slab, pose::identity).expect("The box is under the capsule.");
|
||||
t.expects(under.count == 2 && near(under.normal, vec3{ 0, -1, 0 }));
|
||||
}
|
||||
|
||||
@test def collides_hulls(t: &mut tester): void {
|
||||
val slab := shape::box(vec3{ 2, 0.5, 2 });
|
||||
val crate := shape::box(vec3{ 0.5, 0.5, 0.5 });
|
||||
# A crate resting flat meets the slab at its four corners, midway between
|
||||
# their surfaces.
|
||||
val resting := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.9, 0 })).expect("The crate rests on the slab.");
|
||||
t.expects(resting.count == 4 && near(resting.normal, vec3{ 0, 1, 0 }));
|
||||
var corners := true;
|
||||
for i in 0..4u {
|
||||
val p := resting.points[i];
|
||||
corners = corners && near(p.depth, 0.1) && near(p.point.y, 0.45) && near(math::abs(p.point.x), 0.5) && near(math::abs(p.point.z), 0.5);
|
||||
}
|
||||
t.expects(corners);
|
||||
t.expects(collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 1.1, 0 })) is none);
|
||||
t.expects(collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 1.1, 0 }), 0.2) is some(m) && m.count == 4 && near(m.depth, -0.1));
|
||||
val flipped := collide(&crate, pose::of(vec3{ 0, 0.9, 0 }), &slab, pose::identity).expect("The slab is under the crate.");
|
||||
t.expects(flipped.count == 4 && near(flipped.normal, vec3{ 0, -1, 0 }) && near(flipped.depth, 0.1));
|
||||
# Turned about y, still at four.
|
||||
val turned := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.9, 0 }, quat::axis_angle(vec3::up, 0.7))).expect("The turned crate rests on the slab.");
|
||||
t.expects(turned.count == 4 && near(turned.normal, vec3{ 0, 1, 0 }) && near(turned.depth, 0.1));
|
||||
|
||||
# On its edge, at the edge's two ends; on its corner, at the corner.
|
||||
val half_diagonal := math::sqrt(0.5f);
|
||||
val on_edge := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.4 + half_diagonal, 0 }, quat::axis_angle(vec3::back, math::pi as f32 / 4))).expect("Its edge meets the slab.");
|
||||
t.expects(on_edge.count == 2 && near(on_edge.normal, vec3{ 0, 1, 0 }) && near(on_edge.points[0].depth, 0.1) && near(on_edge.points[1].depth, 0.1));
|
||||
t.expects(near(math::abs(on_edge.points[0].point.z), 0.5) && near(on_edge.points[0].point.x, 0));
|
||||
val pointing := quat::axis_angle(vec3::right, math::atan(math::sqrt(0.5f))) * quat::axis_angle(vec3::back, math::pi as f32 / 4);
|
||||
val half_cube := math::sqrt(0.75f);
|
||||
val on_corner := collide(&slab, pose::identity, &crate, pose::of(vec3{ 0, 0.4 + half_cube, 0 }, pointing)).expect("Its corner meets the slab.");
|
||||
t.expects(on_corner.count == 1 && near(on_corner.normal, vec3{ 0, 1, 0 }) && near(on_corner.depth, 0.1) && near(on_corner.points[0].point, vec3{ 0, 0.45, 0 }));
|
||||
|
||||
# Edge across edge, at one point between them, the normal along neither's
|
||||
# face.
|
||||
val ridge := pose::of(vec3::zero, quat::axis_angle(vec3::right, math::pi as f32 / 4));
|
||||
val keel := pose::of(vec3{ 0, 2 * half_diagonal - 0.1, 0 }, quat::axis_angle(vec3::back, math::pi as f32 / 4));
|
||||
val crossed := collide(&crate, ridge, &crate, keel).expect("The edges cross.");
|
||||
t.expects(crossed.count == 1 && near(crossed.normal, vec3{ 0, 1, 0 }) && near(crossed.depth, 0.1) && near(crossed.points[0].point, vec3{ 0, half_diagonal - 0.05, 0 }));
|
||||
t.expects(!overlaps(&crate, ridge, &crate, pose::of(vec3{ 0, 2 * half_diagonal + 0.05, 0 }, keel.turn)));
|
||||
# A hull of places meets as a box does.
|
||||
val cube := shape::hull_of(corners_of_cube()[:]).expect("A cube is made.");
|
||||
val stacked := collide(&slab, pose::identity, &cube, pose::of(vec3{ 0, 1.4, 0 })).expect("The cube rests on the slab.");
|
||||
t.expects(stacked.count == 4 && near(stacked.normal, vec3{ 0, 1, 0 }) && near(stacked.depth, 0.1));
|
||||
}
|
||||
|
||||
@test def casts_rays_at_shapes(t: &mut tester): void {
|
||||
val up := vec3{ 0, 1, 0 };
|
||||
val ball := shape::sphere(1);
|
||||
val struck := ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3::zero, vec3{ 1, 0, 0 }, 10).expect("It meets the ball.");
|
||||
t.expects(near(struck.distance, 2) && near(struck.point, vec3{ 2, 0, 0 }) && near(struck.normal, vec3{ -1, 0, 0 }));
|
||||
t.expects(ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3::zero, up, 10) is none);
|
||||
t.expects(ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3::zero, vec3{ 1, 0, 0 }, 1.5) is none);
|
||||
val inside := ball.ray_cast(pose::of(vec3{ 3, 0, 0 }), vec3{ 3, 0, 0 }, up, 10).expect("It begins within the ball.");
|
||||
t.expects(inside.distance == 0 && near(inside.normal, vec3{ 0, -1, 0 }));
|
||||
|
||||
# A box, turned a quarter, its long side up and down.
|
||||
val box := shape::box(vec3{ 1, 0.5, 0.5 });
|
||||
val roof := box.ray_cast(pose::of(vec3{ 0, 5, 0 }, quarter_about(vec3::back)), vec3::zero, up, 10).expect("It meets the box.");
|
||||
t.expects(near(roof.distance, 4) && near(roof.normal, vec3{ 0, -1, 0 }) && near(roof.point, vec3{ 0, 4, 0 }));
|
||||
t.expects(box.ray_cast(pose::of(vec3{ 0, 5, 0 }), vec3::zero, up, 4) is none);
|
||||
t.expects(box.ray_cast(pose::of(vec3{ 0, 5, 0 }), vec3{ 2, 0, 0 }, up, 10) is none);
|
||||
|
||||
# A capsule, by its side, along and across it, and by its round end.
|
||||
val pill := shape::capsule(vec3{ -1, 3, 0 }, vec3{ 1, 3, 0 }, 0.5);
|
||||
val side := pill.ray_cast(pose::identity, vec3::zero, up, 10).expect("It meets the capsule's side.");
|
||||
t.expects(near(side.distance, 2.5) && near(side.normal, vec3{ 0, -1, 0 }));
|
||||
val across := pill.ray_cast(pose::identity, vec3{ 0.2, 3, -5 }, vec3{ 0, 0, 1 }, 10).expect("It meets the capsule's side.");
|
||||
t.expects(near(across.distance, 4.5) && near(across.normal, vec3{ 0, 0, -1 }));
|
||||
val end := pill.ray_cast(pose::identity, vec3{ 3, 3, 0 }, vec3{ -1, 0, 0 }, 10).expect("It meets the capsule's end.");
|
||||
t.expects(near(end.distance, 1.5) && near(end.normal, vec3{ 1, 0, 0 }));
|
||||
t.expects(pill.ray_cast(pose::identity, vec3{ 0, 4, 0 }, vec3{ 1, 0, 0 }, 10) is none);
|
||||
}
|
||||
|
||||
# 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): bounds {
|
||||
val at := vec3{ seed.next() * 200 - 100, seed.next() * 200 - 100, seed.next() * 200 - 100 };
|
||||
return bounds::around(at, vec3{ 0.1 + seed.next() * 1.4, 0.1 + seed.next() * 1.4, 0.1 + seed.next() * 1.4 });
|
||||
}
|
||||
|
||||
# How far along a ray it meets a box, if within reach.
|
||||
def hit_within(box: bounds, from: vec3, way: vec3, reach: f32): maybe<f32> {
|
||||
val r := ray{ from, way };
|
||||
if r.hit(box) is some(at) {
|
||||
return at if at <= reach;
|
||||
}
|
||||
return nothing{};
|
||||
}
|
||||
|
||||
@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::<bounds>::new();
|
||||
var leaves := vector::<u32>::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], bounds{ boxes[0].min + vec3{ 0.1, 0, 0 }, boxes[0].max + vec3{ 0.1, 0, 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 := bounds::around(vec3{ seed.next() * 200 - 100, seed.next() * 200 - 100, seed.next() * 200 - 100 }, vec3{ 20, 20, 20 });
|
||||
var found := vector::<u64>::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 := vec3{ -120, 3, -2 };
|
||||
val way := vec3{ 1, 0.05, 0.02 }.normalized();
|
||||
var nearest: f32 = 1000;
|
||||
tree.ray_cast(from, way, 1000, def(leaf: u32, data: u64, reach: f32): f32 {
|
||||
if hit_within(boxes[data], 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 && hit_within(boxes[i], from, way, 1000) is some(at) {
|
||||
expected = math::min(expected, at);
|
||||
}
|
||||
}
|
||||
t.expects(near(nearest, expected));
|
||||
for i in 0..500u {
|
||||
if i % 4 != 1 {
|
||||
tree.remove(leaves[i]);
|
||||
}
|
||||
}
|
||||
t.expects(tree.length == 0 && tree.is_sound() && tree.height == 0);
|
||||
}
|
||||
|
||||
# A rotation of some axis and angle.
|
||||
def some_turn(seed: &mut dice): quat {
|
||||
val axis := vec3{ seed.next() - 0.5, seed.next() - 0.5, seed.next() - 0.5 };
|
||||
return quat::axis_angle(axis.length_squared > 1e-4 ? axis : vec3::up, seed.next() * 6.3);
|
||||
}
|
||||
|
||||
# A hull's corners and face normals, and its edges' directions, in space.
|
||||
struct placed_hull {
|
||||
var points: vector<vec3>;
|
||||
var normals: vector<vec3>;
|
||||
var edges: vector<vec3>;
|
||||
}
|
||||
|
||||
def placed_of(h: &hull, at: pose): placed_hull {
|
||||
var made := placed_hull{ points = vector::<vec3>::new(), normals = vector::<vec3>::new(), edges = vector::<vec3>::new() };
|
||||
for i in 0..h.count {
|
||||
made.points.append(at.apply(h.point(i)));
|
||||
}
|
||||
for f in 0..h.faces {
|
||||
made.normals.append(at.rotate(h.normal(f)));
|
||||
}
|
||||
for e in 0..h.edges {
|
||||
val ends := h.edge(e);
|
||||
made.edges.append(at.rotate(h.point(ends[1]) - h.point(ends[0])));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# How far apart two hulls are along an axis, below 0 where they overlap.
|
||||
def apart_along(a: &placed_hull, b: &placed_hull, axis: vec3): f32 {
|
||||
var a_low := 1e30f;
|
||||
var a_high := -1e30f;
|
||||
var b_low := 1e30f;
|
||||
var b_high := -1e30f;
|
||||
for i in 0..a.points.length {
|
||||
a_low = math::min(a_low, axis.dot(a.points[i]));
|
||||
a_high = math::max(a_high, axis.dot(a.points[i]));
|
||||
}
|
||||
for i in 0..b.points.length {
|
||||
b_low = math::min(b_low, axis.dot(b.points[i]));
|
||||
b_high = math::max(b_high, axis.dot(b.points[i]));
|
||||
}
|
||||
return math::max(b_low - a_high, a_low - b_high);
|
||||
}
|
||||
|
||||
# How far apart two hulls are, by every axis that may part them: each face's
|
||||
# normal, and the cross of every pair of edges, none passed over.
|
||||
def apart_by_every_axis(a: &placed_hull, b: &placed_hull): f32 {
|
||||
var most := -1e30f;
|
||||
for i in 0..a.normals.length {
|
||||
most = math::max(most, apart_along(a, b, a.normals[i]));
|
||||
}
|
||||
for i in 0..b.normals.length {
|
||||
most = math::max(most, apart_along(a, b, b.normals[i]));
|
||||
}
|
||||
for i in 0..a.edges.length {
|
||||
for j in 0..b.edges.length {
|
||||
val axis := a.edges[i].cross(b.edges[j]);
|
||||
continue if axis.length_squared <= 1e-8 * a.edges[i].length_squared * b.edges[j].length_squared;
|
||||
most = math::max(most, apart_along(a, b, axis.normalized()));
|
||||
}
|
||||
}
|
||||
return most;
|
||||
}
|
||||
|
||||
# Places within a box of so many each way from the origin.
|
||||
def some_places(seed: &mut dice, count: u32, reach: f32): vector<vec3> {
|
||||
var made := vector::<vec3>::new();
|
||||
for i in 0..count {
|
||||
made.append(vec3{ (seed.next() * 2 - 1) * reach, (seed.next() * 2 - 1) * reach, (seed.next() * 2 - 1) * reach });
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
# How far a place is from a box about the origin, turned and placed by a pose:
|
||||
# below 0, how deep within it.
|
||||
def from_box(half: vec3, at: pose, p: vec3): f32 {
|
||||
val local := at.unapply(p);
|
||||
val out := local.abs() - half;
|
||||
val outside := out.max(vec3::zero).length;
|
||||
return outside if outside > 0;
|
||||
return math::max(out.x, math::max(out.y, out.z));
|
||||
}
|
||||
|
||||
@test def agrees_with_a_full_search(t: &mut tester): void {
|
||||
var seed := dice{ state = 11 };
|
||||
# Boxes and hulls of places, turned every way, as near as to meet about
|
||||
# half the time: met where no axis parts them, as deep as the axis parting
|
||||
# them least, but where a face is taken over another or an edge as good,
|
||||
# a few percent deeper.
|
||||
var tested: u32 = 0;
|
||||
var met: u32 = 0;
|
||||
var agreed: u32 = 0;
|
||||
var deep_enough: u32 = 0;
|
||||
for i in 0..400u {
|
||||
var a := shape::box(vec3{ 0.2 + seed.next(), 0.2 + seed.next(), 0.2 + seed.next() });
|
||||
var b := shape::box(vec3{ 0.2 + seed.next(), 0.2 + seed.next(), 0.2 + seed.next() });
|
||||
if i % 2 == 1 {
|
||||
a = shape::hull_of(some_places(&mut seed, 12, 1).span).expect("A hull is made.");
|
||||
b = shape::hull_of(some_places(&mut seed, 12, 1).span).expect("A hull is made.");
|
||||
}
|
||||
val at_a := pose::of(vec3::zero, some_turn(&mut seed));
|
||||
val at_b := pose::of(vec3{ seed.next() * 3 - 1.5, seed.next() * 3 - 1.5, seed.next() * 3 - 1.5 }, some_turn(&mut seed));
|
||||
val apart := apart_by_every_axis(&placed_of(a.as_hull(), at_a), &placed_of(b.as_hull(), at_b));
|
||||
continue if math::abs(apart) < 1e-4;
|
||||
tested++;
|
||||
val found := collide(&a, at_a, &b, at_b);
|
||||
if (found is some(_)) == (apart < 0) {
|
||||
agreed++;
|
||||
}
|
||||
if found is some(m) {
|
||||
met++;
|
||||
if math::abs(m.depth + apart) <= 2e-3 + 0.075 * math::abs(apart) && m.count >= 1 && near(m.normal.length, 1) {
|
||||
deep_enough++;
|
||||
}
|
||||
}
|
||||
}
|
||||
t.expects(tested > 350 && agreed == tested && met > 100 && deep_enough == met);
|
||||
|
||||
# Spheres and capsules against boxes, against their nearest places: a
|
||||
# capsule's of a thousand along its segment.
|
||||
var spheres_agreed: u32 = 0;
|
||||
var capsules_agreed: u32 = 0;
|
||||
for i in 0..200u {
|
||||
val half := vec3{ 0.2 + seed.next(), 0.2 + seed.next(), 0.2 + seed.next() };
|
||||
val box := shape::box(half);
|
||||
val at_box := pose::of(vec3::zero, some_turn(&mut seed));
|
||||
val r := 0.1 + seed.next() * 0.6;
|
||||
val center := vec3{ seed.next() * 3 - 1.5, seed.next() * 3 - 1.5, seed.next() * 3 - 1.5 };
|
||||
val ball := shape::sphere(r);
|
||||
val gap := from_box(half, at_box, center) - r;
|
||||
if math::abs(gap) < 1e-4 {
|
||||
spheres_agreed++;
|
||||
} else if collide(&box, at_box, &ball, pose::of(center)) is some(m) {
|
||||
if gap < 0 && near(m.depth, -gap) && near(m.normal.length, 1) {
|
||||
spheres_agreed++;
|
||||
}
|
||||
} else if gap > 0 {
|
||||
spheres_agreed++;
|
||||
}
|
||||
val pill := shape::pill(r * 2, r * 2 + 0.2 + seed.next());
|
||||
val at_pill := pose::of(center, some_turn(&mut seed));
|
||||
var nearest := 1e30f;
|
||||
for k in 0..=1000u {
|
||||
val along := pill.a.lerp(pill.b, k as f32 / 1000);
|
||||
nearest = math::min(nearest, from_box(half, at_box, at_pill.apply(along)));
|
||||
}
|
||||
# Where its segment passes within the box, only whether they meet.
|
||||
val pill_gap := nearest - r;
|
||||
if math::abs(pill_gap) < 2e-3 {
|
||||
capsules_agreed++;
|
||||
} else if collide(&box, at_box, &pill, at_pill) is some(m) {
|
||||
if pill_gap < 0 && (nearest <= 0 || math::abs(m.depth + pill_gap) <= 2e-3) {
|
||||
capsules_agreed++;
|
||||
}
|
||||
} else if pill_gap > 0 {
|
||||
capsules_agreed++;
|
||||
}
|
||||
}
|
||||
t.expects(spheres_agreed == 200 && capsules_agreed == 200);
|
||||
}
|
||||
|
||||
# A floor of 8 by 8 squares, each of two triangles, from -4 to 4 along x and
|
||||
# z, and a wall up its +x side, 2 high, facing in.
|
||||
def room(): triangle_mesh {
|
||||
var points := vector::<vec3>::new();
|
||||
var indices := vector::<u32>::new();
|
||||
for i in 0..9u {
|
||||
for j in 0..9u {
|
||||
points.append(vec3{ i as f32 - 4, 0, j as f32 - 4 });
|
||||
}
|
||||
}
|
||||
for i in 0..8u {
|
||||
for j in 0..8u {
|
||||
val p00 := (i * 9 + j) as u32;
|
||||
val p01 := p00 + 1;
|
||||
val p10 := p00 + 9;
|
||||
val p11 := p10 + 1;
|
||||
for each in [p00, p01, p10, p10, p01, p11] {
|
||||
indices.append(each);
|
||||
}
|
||||
}
|
||||
}
|
||||
val w := points.length as u32;
|
||||
points.append(vec3{ 4, 0, -4 });
|
||||
points.append(vec3{ 4, 0, 4 });
|
||||
points.append(vec3{ 4, 2, -4 });
|
||||
points.append(vec3{ 4, 2, 4 });
|
||||
for each in [w, w + 1, w + 2, w + 2, w + 1, w + 3] {
|
||||
indices.append(each);
|
||||
}
|
||||
# A triangle of no area, and one of a place past the rest, left out.
|
||||
val skipped: u32[6] = [0, 1, 2, 0, 1, 500];
|
||||
for i in 0..6u {
|
||||
indices.append(skipped[i]);
|
||||
}
|
||||
return triangle_mesh::new(points.span, indices.span);
|
||||
}
|
||||
|
||||
# The manifolds a shape meets a mesh with.
|
||||
def met(m: &triangle_mesh, at: pose, s: &shape, at_s: pose): vector<manifold> {
|
||||
var found := vector::<manifold>::new();
|
||||
m.collide(at, s, at_s, 0, def(triangle: u32, each: manifold): bool {
|
||||
found.append(each);
|
||||
return true;
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
# Whether each manifold pushes along a direction, as deep as `depth`.
|
||||
def all_along(found: &vector<manifold>, normal: vec3, depth: f32): bool {
|
||||
for i in 0..found.length {
|
||||
return false if !near(found[i].normal, normal) || !near(found[i].depth, depth);
|
||||
}
|
||||
return !found.empty;
|
||||
}
|
||||
|
||||
@test def meets_a_mesh_of_triangles(t: &mut tester): void {
|
||||
val m := room();
|
||||
t.expects(m.length == 130 && near(m.normal(0), vec3{ 0, 1, 0 }) && near(m.normal(128), vec3{ -1, 0, 0 }));
|
||||
# The floor's edges between its own triangles are flat, so they don't push;
|
||||
# those along its sides, of one triangle alone, do.
|
||||
t.expects(!m.is_active(9, 1) && !m.is_active(9, 2) && m.is_active(0, 0) && m.is_active(128, 0));
|
||||
|
||||
# A crate on the floor, over the corner of four squares: pushed up by each
|
||||
# triangle it's over.
|
||||
val crate := shape::box(vec3{ 0.5, 0.5, 0.5 });
|
||||
val resting := met(&m, pose::identity, &crate, pose::of(vec3{ 0.1, 0.45, -0.2 }));
|
||||
t.expects(resting.length >= 4 && all_along(&resting, vec3{ 0, 1, 0 }, 0.05));
|
||||
# A ball sunk into it beside a seam: the triangles past the seam meet it by
|
||||
# their edges, flat, so they push it up as their faces do.
|
||||
val ball := shape::sphere(0.5);
|
||||
val sunk := met(&m, pose::identity, &ball, pose::of(vec3{ 0.1, 0.3, 0.35 }));
|
||||
t.expects(sunk.length >= 3 && all_along(&sunk, vec3{ 0, 1, 0 }, 0.2));
|
||||
# Against the wall, pushed back from it alone; in the corner, by both.
|
||||
t.expects(all_along(&met(&m, pose::identity, &ball, pose::of(vec3{ 3.7, 1, 0 })), vec3{ -1, 0, 0 }, 0.2));
|
||||
val cornered := met(&m, pose::identity, &ball, pose::of(vec3{ 3.7, 0.4, 0.5 }));
|
||||
var up: u64 = 0;
|
||||
var back: u64 = 0;
|
||||
for i in 0..cornered.length {
|
||||
if near(cornered[i].normal, vec3{ 0, 1, 0 }) && near(cornered[i].depth, 0.1) {
|
||||
up++;
|
||||
} else if near(cornered[i].normal, vec3{ -1, 0, 0 }) && near(cornered[i].depth, 0.2) {
|
||||
back++;
|
||||
}
|
||||
}
|
||||
t.expects(up >= 1 && back >= 1 && up + back == cornered.length);
|
||||
t.expects(m.overlaps(pose::identity, &ball, pose::of(vec3{ 0, 0.4, 0 })) && !m.overlaps(pose::identity, &ball, pose::of(vec3{ 0, 0.6, 0 })));
|
||||
# The mesh placed elsewhere is met there.
|
||||
val moved := pose::of(vec3{ 20, 0, 0 });
|
||||
t.expects(met(&m, moved, &ball, pose::of(vec3{ 20, 0.3, 0 })).length >= 1 && met(&m, moved, &ball, pose::of(vec3{ 0, 0.3, 0 })).empty);
|
||||
|
||||
# Rays down to the floor, up at it from below, and into the wall.
|
||||
val down := m.ray_cast(pose::identity, vec3{ 0.3, 5, 0.2 }, vec3{ 0, -1, 0 }, 10).expect("It meets the floor.");
|
||||
t.expects(near(down.distance, 5) && near(down.normal, vec3{ 0, 1, 0 }) && near(down.point, vec3{ 0.3, 0, 0.2 }));
|
||||
val from_below := m.ray_cast(pose::identity, vec3{ 0.3, -1, 0.2 }, vec3{ 0, 1, 0 }, 10).expect("It meets the floor from below.");
|
||||
t.expects(near(from_below.distance, 1) && near(from_below.normal, vec3{ 0, -1, 0 }));
|
||||
val wall := m.ray_cast(pose::identity, vec3{ 0, 1, 0 }, vec3{ 1, 0, 0 }, 10).expect("It meets the wall.");
|
||||
t.expects(near(wall.distance, 4) && near(wall.normal, vec3{ -1, 0, 0 }) && wall.triangle >= 128);
|
||||
t.expects(m.ray_cast(pose::identity, vec3{ 0, 1, 0 }, vec3{ 1, 0, 0 }, 3) is none);
|
||||
t.expects(m.ray_cast(moved, vec3{ 20.3, 5, 0.2 }, vec3{ 0, -1, 0 }, 10) is some(h) && near(h.point, vec3{ 20.3, 0, 0.2 }));
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,365 @@
|
||||
#! Contains the tests of `physics3d`'s space: shapes' masses and inertia; 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; a mesh's floor
|
||||
#! slid across without catching at its seams; and rays.
|
||||
|
||||
import std::testing;
|
||||
import std::containers;
|
||||
import std::math;
|
||||
import malunal::linear;
|
||||
import malunal::physics3d;
|
||||
|
||||
def close(a: f32, b: f32, within: f32): bool {
|
||||
return math::abs(a - b) <= within;
|
||||
}
|
||||
|
||||
def close(a: vec3, b: vec3, within: f32): bool {
|
||||
return (a - b).length <= 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(vec3{ 0, -1, 0 }) });
|
||||
_ = s.attach(ground, collider{ shape = shape::box(vec3{ 50, 1, 50 }) });
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
def cube(s: &mut space, at: vec3, half: f32): body_id {
|
||||
val made := s.add(body{ at = pose::of(at) });
|
||||
_ = s.attach(made, collider{ shape = shape::box(vec3{ half, half, half }) });
|
||||
return made;
|
||||
}
|
||||
|
||||
@test def finds_masses_and_inertia(t: &mut tester): void {
|
||||
# A box of 2 by 1 by 1: its tensor about each axis m(b² + c²)/12.
|
||||
val box := shape::box(vec3{ 1, 0.5, 0.5 }).mass(1);
|
||||
t.expects(close(box.mass, 2, 1e-4) && close(box.center, vec3::zero, 1e-5));
|
||||
t.expects(close(box.inertia.x_axis.x, 2f * 2f / 12f, 1e-4) && close(box.inertia.y_axis.y, 2f * 5f / 12f, 1e-4) && close(box.inertia.z_axis.z, 2f * 5f / 12f, 1e-4));
|
||||
t.expects(close(box.inertia.x_axis.y, 0, 1e-5) && close(box.inertia.x_axis.z, 0, 1e-5));
|
||||
# A ball: 2mr²/5 about every axis.
|
||||
val ball := shape::sphere(1, vec3{ 0, 2, 0 }).mass(1);
|
||||
val m := 4f / 3f * math::pi as f32;
|
||||
t.expects(close(ball.mass, m, 1e-4) && close(ball.center.y, 2, 1e-5) && close(ball.inertia.y_axis.y, 0.4 * m, 1e-4));
|
||||
# A hull of a box's corners weighs as the box does.
|
||||
var corners := vector::<vec3>::new();
|
||||
for k in 0..8u {
|
||||
corners.append(vec3{ (k & 1) != 0 ? 1f : -1f, (k & 2) != 0 ? 0.5f : -0.5f, (k & 4) != 0 ? 0.5f : -0.5f });
|
||||
}
|
||||
val hulled := shape::hull_of(corners.span).expect("A box's hull is made.").mass(1);
|
||||
t.expects(close(hulled.mass, 2, 1e-4) && close(hulled.inertia.x_axis.x, box.inertia.x_axis.x, 1e-4) && close(hulled.inertia.y_axis.y, box.inertia.y_axis.y, 1e-4));
|
||||
# A capsule along x: about its own axis as a cylinder and a ball, the least.
|
||||
val pill := shape::capsule(vec3{ -1, 0, 0 }, vec3{ 1, 0, 0 }, 0.5).mass(1);
|
||||
t.expects(close(pill.mass, math::pi as f32 * 0.25 * 2 + 4f / 3f * math::pi as f32 * 0.125, 1e-4));
|
||||
t.expects(pill.inertia.x_axis.x < pill.inertia.y_axis.y && close(pill.inertia.y_axis.y, pill.inertia.z_axis.z, 1e-4));
|
||||
# Two together, about their joint center.
|
||||
val both := box.add(&shape::box(vec3{ 1, 0.5, 0.5 }, vec3{ 0, 3, 0 }).mass(1));
|
||||
t.expects(close(both.mass, 4, 1e-4) && close(both.center.y, 1.5, 1e-4) && close(both.inertia.x_axis.x, 2 * box.inertia.x_axis.x + 4 * 1.5 * 1.5, 1e-3));
|
||||
}
|
||||
|
||||
@test def drops_a_box_onto_the_ground(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
_ = grounded(&mut s);
|
||||
val crate := cube(&mut s, vec3{ 0, 4, 0 }, 0.5);
|
||||
t.expects(close(s.mass_of(crate).mass, 1, 1e-4));
|
||||
var began: u32 = 0;
|
||||
for i in 0..120u {
|
||||
s.step(1f / 60f);
|
||||
for e in s.events() {
|
||||
if e.began {
|
||||
began++;
|
||||
}
|
||||
}
|
||||
}
|
||||
val at := s.pose_of(crate);
|
||||
t.expects(close(at.at, vec3{ 0, 0.5, 0 }, 0.01) && close(at.turn.dot(quat::identity), 1, 1e-4));
|
||||
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..8u {
|
||||
top = cube(&mut s, vec3{ 0, 0.5 + i as f32, 0 }, 0.5);
|
||||
}
|
||||
steps(&mut s, 4);
|
||||
t.expects(close(s.pose_of(top).at, vec3{ 0, 7.5, 0 }, 0.05) && 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 := cube(&mut s, vec3{ 0, 0.5, 0 }, 0.5);
|
||||
val upper := cube(&mut s, vec3{ 0, 1.5, 0 }, 0.5);
|
||||
val apart := cube(&mut s, vec3{ 10, 0.5, 0 }, 0.5);
|
||||
steps(&mut s, 2);
|
||||
t.expects(!s.is_awake(lower) && !s.is_awake(upper) && !s.is_awake(apart));
|
||||
s.apply_impulse(upper, vec3{ 0.1, 0, 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));
|
||||
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(vec3::zero);
|
||||
val world := s.add(body{ kind = body_kind::static });
|
||||
_ = s.attach(world, collider{ shape = shape::sphere(10) });
|
||||
val rock := s.add(body{ at = pose::of(vec3{ 0, 0, 14 }), gravity = vec3{ 0, 0, -10 } });
|
||||
_ = s.attach(rock, collider{ shape = shape::sphere(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(close(s.center_of(rock), vec3{ 0, 0, 10.5 }, 0.05) && close(s.gravity_of(rock), vec3{ 0, 0, -10 }, 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, vec3{ 0, 0, -10.001 });
|
||||
t.expects(!s.is_awake(rock));
|
||||
s.set_gravity(rock, vec3{ 0, 0, 10 });
|
||||
steps(&mut s, 1);
|
||||
t.expects(s.center_of(rock).z > 13);
|
||||
}
|
||||
|
||||
@test def bounces_and_slides(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
s.sleeping = false;
|
||||
_ = grounded(&mut s);
|
||||
val ball := s.add(body{ at = pose::of(vec3{ 0, 2.5, 0 }) });
|
||||
_ = s.attach(ball, collider{ shape = shape::sphere(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 tilt := quat::axis_angle(vec3::back, math::pi as f32 / 6);
|
||||
val slope := hill.add(body{ kind = body_kind::static, at = pose::of(vec3::zero, tilt) });
|
||||
_ = hill.attach(slope, collider{ shape = shape::box(vec3{ 20, 0.5, 20 }) });
|
||||
val up := tilt * vec3::up;
|
||||
val sticky := hill.add(body{ at = pose::of(up, tilt) });
|
||||
_ = hill.attach(sticky, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), friction = 1 });
|
||||
val slick := hill.add(body{ at = pose::of(up + vec3{ 0, 0, 5 }, tilt) });
|
||||
_ = hill.attach(slick, collider{ shape = shape::box(vec3{ 0.5, 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);
|
||||
val ghost := s.add(body{ at = pose::of(vec3{ -5, 1, 0 }) });
|
||||
_ = s.attach(ghost, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), layers = 2, mask = 2 });
|
||||
val zone := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 5, 4, 0 }) });
|
||||
val field := s.attach(zone, collider{ shape = shape::box(vec3{ 1, 1, 1 }), sensor = true });
|
||||
val stone := s.add(body{ at = pose::of(vec3{ 5, 7, 0 }) });
|
||||
val heavy := s.attach(stone, collider{ shape = shape::sphere(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 gem
|
||||
# notes a character walking, but never what stands still beside it.
|
||||
val gem := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 0, 0, 0 }) });
|
||||
val field := s.attach(gem, collider{ shape = shape::sphere(0.5), sensor = true, data = 7 });
|
||||
val post := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 0.5, 0, 0 }) });
|
||||
val pole := s.attach(post, collider{ shape = shape::box(vec3{ 0.25, 0.25, 0.25 }), data = 8 });
|
||||
val walker := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec3{ -3, 0, 0 }), velocity = vec3{ 3, 0, 0 } });
|
||||
val feet := s.attach(walker, collider{ shape = shape::sphere(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::sphere(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(vec3{ -2, 0, 0 }, vec3{ 1, 0, 0 }, 10) is some(&hit) && hit.collider == pole);
|
||||
t.expects(s.ray_cast(vec3{ -2, 0, 0 }, vec3{ 1, 0, 0 }, 10, query_filter{ sensors = true }) is some(&hit) && hit.collider == field);
|
||||
t.expects(s.ray_cast(vec3{ -2, 0, 0 }, vec3{ 1, 0, 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(vec3{ 0, 2, 0 }) });
|
||||
_ = s.attach(lift, collider{ shape = shape::box(vec3{ 2, 0.25, 2 }) });
|
||||
val crate := cube(&mut s, vec3{ 0, 2.75, 0 }, 0.5);
|
||||
steps(&mut s, 1);
|
||||
s.set_velocity(lift, vec3{ 1, 0, 0 });
|
||||
steps(&mut s, 1);
|
||||
t.expects(close(s.pose_of(lift).at, vec3{ 1, 2, 0 }, 0.02));
|
||||
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 on a ball joint keeps its bob as far from it.
|
||||
val bob := s.add(body{ at = pose::of(vec3{ 3, 5, 0 }) });
|
||||
_ = s.attach(bob, collider{ shape = shape::sphere(0.25) });
|
||||
_ = s.ball(ground, bob, vec3{ 0, 5, 0 });
|
||||
var farthest: f32 = 0;
|
||||
var nearest: f32 = 10;
|
||||
for i in 0..120u {
|
||||
s.step(1f / 60f);
|
||||
val reach := (s.pose_of(bob).at - vec3{ 0, 5, 0 }).length;
|
||||
farthest = math::max(farthest, reach);
|
||||
nearest = math::min(nearest, reach);
|
||||
}
|
||||
t.expects(close(farthest, 3, 0.03) && close(nearest, 3, 0.03));
|
||||
# A wheel on a hinge, turned by a motor at its speed about its axle alone.
|
||||
val wheel := s.add(body{ at = pose::of(vec3{ 10, 3, 0 }), gravity_scale = 0, angular_damping = 0 });
|
||||
_ = s.attach(wheel, collider{ shape = shape::sphere(0.5) });
|
||||
val axle := s.hinge(ground, wheel, vec3{ 10, 3, 0 }, vec3::back);
|
||||
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), vec3{ 0, 0, 2 }, 0.02) && close(s.pose_of(wheel).at, vec3{ 10, 3, 0 }, 1e-3));
|
||||
# A door, hinged up its side, limited to a quarter turn, swung by a push.
|
||||
val door := s.add(body{ at = pose::of(vec3{ 21, 4, 0 }), gravity_scale = 0 });
|
||||
_ = s.attach(door, collider{ shape = shape::box(vec3{ 1, 1, 0.1 }) });
|
||||
val hinge := s.hinge(ground, door, vec3{ 20, 4, 0 }, vec3::up);
|
||||
var limited := s.joint_of(hinge);
|
||||
limited.limit = true;
|
||||
limited.lower = -math::pi as f32 / 4;
|
||||
limited.upper = math::pi as f32 / 4;
|
||||
s.change(hinge, limited);
|
||||
s.apply_impulse(door, vec3{ 0, 0, -3 }, vec3{ 22, 4, 0 });
|
||||
steps(&mut s, 2);
|
||||
t.expects(close(s.angle_of(hinge), math::pi as f32 / 4, 0.03) && close(s.pose_of(door).turn.y * s.pose_of(door).turn.y + s.pose_of(door).turn.w * s.pose_of(door).turn.w, 1, 1e-3));
|
||||
# A rod keeps two bodies apart; a fixed joint keeps them as one.
|
||||
val left := s.add(body{ at = pose::of(vec3{ -10, 6, 0 }) });
|
||||
_ = s.attach(left, collider{ shape = shape::sphere(0.25) });
|
||||
val right := s.add(body{ at = pose::of(vec3{ -8, 6, 0 }) });
|
||||
_ = s.attach(right, collider{ shape = shape::sphere(0.25) });
|
||||
_ = s.tie(left, right, vec3{ -10, 6, 0 }, vec3{ -8, 6, 0 });
|
||||
val stuck := cube(&mut s, vec3{ -10, 7, 0 }, 0.25);
|
||||
_ = s.fix(left, stuck, vec3{ -10, 6.5, 0 });
|
||||
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, vec3{ 0, 1, 0 }, 0.03) && close(math::abs(s.pose_of(left).turn.dot(s.pose_of(stuck).turn)), 1, 1e-3));
|
||||
}
|
||||
|
||||
@test def slides_across_a_mesh_without_catching(t: &mut tester): void {
|
||||
var s := space::new();
|
||||
s.sleeping = false;
|
||||
# A floor of 20 by 20 squares, two triangles each, its top at y = 0.
|
||||
var points := vector::<vec3>::new();
|
||||
var indices := vector::<u32>::new();
|
||||
val n: u32 = 20;
|
||||
for z in 0..n + 1 {
|
||||
for x in 0..n + 1 {
|
||||
points.append(vec3{ x as f32 - 10, 0, z as f32 - 10 });
|
||||
}
|
||||
}
|
||||
for z in 0..n {
|
||||
for x in 0..n {
|
||||
val at := z * (n + 1) + x;
|
||||
indices.append(at);
|
||||
indices.append(at + n + 1);
|
||||
indices.append(at + 1);
|
||||
indices.append(at + 1);
|
||||
indices.append(at + n + 1);
|
||||
indices.append(at + n + 2);
|
||||
}
|
||||
}
|
||||
val floor := s.add(body{ kind = body_kind::static });
|
||||
val ground := s.attach_mesh(floor, triangle_mesh::new(points.span, indices.span), collider{ friction = 0 });
|
||||
val crate := s.add(body{ at = pose::of(vec3{ -8, 0.5, 0.3 }), velocity = vec3{ 4, 0, 0 } });
|
||||
_ = s.attach(crate, collider{ shape = shape::box(vec3{ 0.5, 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.
|
||||
t.expects(close(s.pose_of(crate).at.x, 4, 0.1) && close(lowest, 0.5, 0.01) && close(slowest, 4, 0.05));
|
||||
t.expects(close(s.pose_of(crate).at.y, 0.5, 0.01) && math::abs(s.spin_of(crate).length) < 0.01);
|
||||
# A ray meets the mesh's triangle under it.
|
||||
val hit := s.ray_cast(vec3{ 2.5, 5, 2.5 }, vec3::down, 10, 0xFFFFFFFF).expect("It meets the floor.");
|
||||
t.expects(hit.collider == ground && close(hit.distance, 5, 1e-4) && hit.triangle != 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
@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(vec3{ 3, 0.5, 0 }) });
|
||||
val side := s.attach(crate, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), layers = 2 });
|
||||
val hit := s.ray_cast(vec3{ 0, 0.5, 0 }, vec3::right, 10).expect("It meets the crate.");
|
||||
t.expects(hit.collider == side && hit.body == crate && close(hit.distance, 2.5, 1e-4) && close(hit.normal, vec3::left, 1e-4));
|
||||
val down := s.ray_cast(vec3{ 3, 5, 0 }, vec3::down, 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(vec3{ 0, 5, 0 }, vec3::up, 10) is none);
|
||||
}
|
||||
Reference in new issue
Block a user