Initial Commit
This commit is contained in:
64 files changed
+13419
No files matched your search
@@ -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.view() == "{\"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.view()).value == box);
|
||||
val ball := sphere{ vec3{ 0, 1, 0 }, 0.75 };
|
||||
t.expects(json::from_text::<sphere>(json::to_text(&ball).value.view()).value == ball);
|
||||
val floor := plane::through(vec3{ 0, 2, 0 }, vec3::up);
|
||||
t.expects(json::from_text::<plane>(json::to_text(&floor).value.view()).value == floor);
|
||||
val look := ray::toward(vec3::zero, vec3{ 0, 0, -4 });
|
||||
t.expects(json::from_text::<ray>(json::to_text(&look).value.view()).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::empty);
|
||||
t.expects(bounds::empty.is_empty && !b.is_empty && bounds::empty.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::empty.transformed(mat4::identity).is_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,227 @@
|
||||
#! 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));
|
||||
}
|
||||
|
||||
@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.view().find("\"at\":{\"x\":1,\"y\":0.1,\"z\":-2.5}") is some(_));
|
||||
t.expects(text.view().find("\"size\":{\"x\":0.5,\"y\":8}") is some(_) && text.view().find("_w") is none);
|
||||
val back := json::from_text::<posed>(text.view()) 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.view().starts_with("[") && json::from_text::<mat4>(matrix.view()).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,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.view()) is none);
|
||||
# A capsule of one place is a circle; a pill stands up.
|
||||
t.expects(shape::capsule(vec2{ 1, 1 }, vec2{ 1, 1 }, 0.5).kind == shape_kind::circle);
|
||||
val pill := shape::pill(1, 3);
|
||||
t.expects(pill.kind == shape_kind::capsule && near(pill.point(0), vec2{ 0, -1 }) && near(pill.point(1), vec2{ 0, 1 }) && near(pill.radius, 0.5));
|
||||
# The box holding a shape where a pose places it.
|
||||
val turned := box.bounds(pose::of(vec2{ 10, 0 }, math::pi as f32 / 2));
|
||||
t.expects(near(turned.min, vec2{ 9, -2 }) && near(turned.max, vec2{ 11, 2 }));
|
||||
val held := pill.bounds(pose::of(vec2{ 0, 5 }));
|
||||
t.expects(near(held.min, vec2{ -0.5, 3.5 }) && near(held.max, vec2{ 0.5, 6.5 }));
|
||||
t.expects(pill.contains(pose::identity, vec2{ 0.4, 1.2 }) && !pill.contains(pose::identity, vec2{ 0.45, 1.4 }));
|
||||
}
|
||||
|
||||
@test def collides_circles_and_hulls(t: &mut tester): void {
|
||||
val ball := shape::circle(0.5);
|
||||
val other := shape::circle(1);
|
||||
val met := collide(&ball, pose::identity, &other, pose::of(vec2{ 1.2, 0 })).expect("They meet.");
|
||||
t.expects(met.count == 1 && near(met.normal, vec2{ 1, 0 }) && near(met.points[0].depth, 0.3) && near(met.points[0].point, vec2{ 0.35, 0 }));
|
||||
t.expects(collide(&ball, pose::identity, &other, pose::of(vec2{ 1.6, 0 })) is none);
|
||||
# Touching, they meet, at a depth of 0.
|
||||
t.expects(collide(&ball, pose::identity, &other, pose::of(vec2{ 1.5, 0 })) is some(m) && near(m.depth, 0));
|
||||
|
||||
# A ball over a box's face, by its corner, and within it.
|
||||
val slab := shape::box(vec2{ 1, 0.5 });
|
||||
val above := collide(&slab, pose::identity, &ball, pose::of(vec2{ 0, 0.9 })).expect("It rests on the box.");
|
||||
t.expects(near(above.normal, vec2{ 0, 1 }) && near(above.depth, 0.1) && near(above.points[0].point, vec2{ 0, 0.45 }));
|
||||
val corner := collide(&slab, pose::identity, &ball, pose::of(vec2{ 1.3, 0.8 })).expect("It meets the corner.");
|
||||
val diagonal := math::sqrt(0.5f);
|
||||
t.expects(near(corner.normal, vec2{ diagonal, diagonal }) && near(corner.depth, 0.5 - math::sqrt(0.18f)));
|
||||
val small := shape::circle(0.1);
|
||||
val within := collide(&slab, pose::identity, &small, pose::of(vec2{ 0.8, 0 })).expect("It's within the box.");
|
||||
t.expects(near(within.normal, vec2{ 1, 0 }) && near(within.depth, 0.3));
|
||||
# The other way around, the normal is turned around.
|
||||
val back := collide(&small, pose::of(vec2{ 0.8, 0 }), &slab, pose::identity).expect("It's within the box.");
|
||||
t.expects(near(back.normal, vec2{ -1, 0 }) && near(back.depth, 0.3));
|
||||
# A capsule and a ball meet as the ball nears its segment.
|
||||
val pill := shape::capsule(vec2{ -1, 0 }, vec2{ 1, 0 }, 0.25);
|
||||
val side := collide(&pill, pose::identity, &ball, pose::of(vec2{ 0.3, 0.7 })).expect("It meets the capsule's side.");
|
||||
t.expects(near(side.normal, vec2{ 0, 1 }) && near(side.depth, 0.05));
|
||||
}
|
||||
|
||||
@test def collides_hulls_at_one_point_or_two(t: &mut tester): void {
|
||||
# A box resting flat on another meets it at two points, midway between their
|
||||
# surfaces.
|
||||
val slab := shape::box(vec2{ 1, 0.5 });
|
||||
val crate := shape::box(vec2{ 0.5, 0.5 });
|
||||
val resting := collide(&slab, pose::identity, &crate, pose::of(vec2{ 0, 0.9 })).expect("The crate rests on the slab.");
|
||||
t.expects(resting.count == 2 && near(resting.normal, vec2{ 0, 1 }));
|
||||
t.expects(near(resting.points[0].depth, 0.1) && near(resting.points[1].depth, 0.1));
|
||||
t.expects(near(resting.points[0].point.y, 0.45) && near(math::abs(resting.points[0].point.x), 0.5) && near(resting.points[0].point.x, -resting.points[1].point.x));
|
||||
t.expects(collide(&slab, pose::identity, &crate, pose::of(vec2{ 0, 1.1 })) is none);
|
||||
val flipped := collide(&crate, pose::of(vec2{ 0, 0.9 }), &slab, pose::identity).expect("The slab is under the crate.");
|
||||
t.expects(flipped.count == 2 && near(flipped.normal, vec2{ 0, -1 }));
|
||||
# A crate turned onto its corner meets the slab at the corner alone.
|
||||
val tipped := collide(&slab, pose::identity, &crate, pose::of(vec2{ 0, 1.2 }, math::pi as f32 / 4)).expect("Its corner meets the slab.");
|
||||
t.expects(tipped.count == 1 && near(tipped.normal, vec2{ 0, 1 }) && near(tipped.depth, 0.5 * math::sqrt(2f) - 0.7));
|
||||
|
||||
# A capsule lying on the slab meets it at two points; one standing by its
|
||||
# corner, at one, along the line from the corner to its end.
|
||||
val lying := shape::capsule(vec2{ -0.5, 0 }, vec2{ 0.5, 0 }, 0.5);
|
||||
val flat := collide(&slab, pose::identity, &lying, pose::of(vec2{ 0, 0.9 })).expect("The capsule lies on the slab.");
|
||||
t.expects(flat.count == 2 && near(flat.normal, vec2{ 0, 1 }) && near(flat.points[0].depth, 0.1) && near(flat.points[0].point.y, 0.45));
|
||||
val standing := shape::capsule(vec2{ 0, 0 }, vec2{ 0, 1 }, 0.5);
|
||||
val by := collide(&slab, pose::identity, &standing, pose::of(vec2{ 1.3, 0.8 })).expect("The capsule's end meets the corner.");
|
||||
val diagonal := math::sqrt(0.5f);
|
||||
t.expects(by.count == 1 && near(by.normal, vec2{ diagonal, diagonal }) && near(by.depth, 0.5 - math::sqrt(0.18f)));
|
||||
t.expects(!overlaps(&slab, pose::identity, &standing, pose::of(vec2{ 1.4, 0.9 })));
|
||||
# Two capsules side by side, and crossing.
|
||||
val beside := collide(&lying, pose::identity, &lying, pose::of(vec2{ 0, 0.8 })).expect("They lie together.");
|
||||
t.expects(near(beside.normal, vec2{ 0, 1 }) && near(beside.depth, 0.2));
|
||||
t.expects(overlaps(&lying, pose::identity, &lying, pose::of(vec2{ 0, 0 }, 1.2)));
|
||||
# A segment, of no thickness, across a box.
|
||||
val wire := shape::segment(vec2{ -2, 0 }, vec2{ 2, 0 });
|
||||
t.expects(overlaps(&wire, pose::identity, &crate, pose::of(vec2{ 0, 0.3 })) && !overlaps(&wire, pose::identity, &crate, pose::of(vec2{ 0, 0.6 })));
|
||||
}
|
||||
|
||||
@test def casts_rays_at_shapes(t: &mut tester): void {
|
||||
val up := vec2{ 0, 1 };
|
||||
val ball := shape::circle(1);
|
||||
val struck := ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2::zero, vec2{ 1, 0 }, 10).expect("It meets the ball.");
|
||||
t.expects(near(struck.distance, 2) && near(struck.point, vec2{ 2, 0 }) && near(struck.normal, vec2{ -1, 0 }));
|
||||
t.expects(ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2::zero, up, 10) is none);
|
||||
t.expects(ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2::zero, vec2{ 1, 0 }, 1.5) is none);
|
||||
val inside := ball.ray_cast(pose::of(vec2{ 3, 0 }), vec2{ 3, 0 }, up, 10).expect("It begins within the ball.");
|
||||
t.expects(inside.distance == 0 && near(inside.normal, vec2{ 0, -1 }));
|
||||
|
||||
# A box, turned a quarter, its long side up and down.
|
||||
val box := shape::box(vec2{ 1, 0.5 });
|
||||
val roof := box.ray_cast(pose::of(vec2{ 0, 5 }, math::pi as f32 / 2), vec2::zero, up, 10).expect("It meets the box.");
|
||||
t.expects(near(roof.distance, 4) && near(roof.normal, vec2{ 0, -1 }) && near(roof.point, vec2{ 0, 4 }));
|
||||
t.expects(box.ray_cast(pose::of(vec2{ 0, 5 }), vec2::zero, up, 4) is none);
|
||||
t.expects(box.ray_cast(pose::of(vec2{ 0, 5 }), vec2{ 2, 0 }, up, 10) is none);
|
||||
|
||||
# A capsule, by its side and its round end.
|
||||
val pill := shape::capsule(vec2{ -1, 3 }, vec2{ 1, 3 }, 0.5);
|
||||
val side := pill.ray_cast(pose::identity, vec2::zero, up, 10).expect("It meets the capsule's side.");
|
||||
t.expects(near(side.distance, 2.5) && near(side.normal, vec2{ 0, -1 }));
|
||||
val end := pill.ray_cast(pose::identity, vec2{ 3, 3 }, vec2{ -1, 0 }, 10).expect("It meets the capsule's end.");
|
||||
t.expects(near(end.distance, 1.5) && near(end.normal, vec2{ 1, 0 }));
|
||||
# A segment, from either side.
|
||||
val wire := shape::segment(vec2{ -1, 2 }, vec2{ 1, 2 });
|
||||
val from_below := wire.ray_cast(pose::identity, vec2::zero, up, 10).expect("It meets the segment from below.");
|
||||
val from_above := wire.ray_cast(pose::identity, vec2{ 0, 5 }, vec2{ 0, -1 }, 10).expect("It meets the segment from above.");
|
||||
t.expects(near(from_below.distance, 2) && near(from_below.normal, vec2{ 0, -1 }) && near(from_above.distance, 3) && near(from_above.normal, up));
|
||||
}
|
||||
|
||||
# Numbers from 0 to 1, each of the last, as an LCG makes them.
|
||||
struct dice {
|
||||
var state: u64;
|
||||
}
|
||||
|
||||
def dice::next(&mut this): f32 {
|
||||
this.state = math::wrapping_add(math::wrapping_mul(this.state, 6364136223846793005), 1442695040888963407);
|
||||
return (this.state >> 40) as f32 / (1u << 24) as f32;
|
||||
}
|
||||
|
||||
# A box somewhere within 100 of the origin, as large as 3.
|
||||
def some_box(seed: &mut dice): aabb {
|
||||
val at := vec2{ seed.next() * 200 - 100, seed.next() * 200 - 100 };
|
||||
return aabb::around(at, vec2{ 0.1 + seed.next() * 1.4, 0.1 + seed.next() * 1.4 });
|
||||
}
|
||||
|
||||
@test def sorts_boxes_in_a_tree(t: &mut tester): void {
|
||||
var seed := dice{ state = 7 };
|
||||
var tree := aabb_tree::new(0.25);
|
||||
var boxes := vector::<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 })).is_empty);
|
||||
|
||||
# A ray down to the floor, through the cells above it.
|
||||
val down := vec2{ 1, -1 }.normalized();
|
||||
val floor := g.ray_cast(pose::identity, vec2{ 0.3, 3.5 }, down, 10).expect("It meets the floor.");
|
||||
t.expects(floor.x == 2 && floor.y == 0 && near(floor.hit.distance, 2.5 * math::sqrt(2f)) && near(floor.hit.normal, vec2{ 0, 1 }));
|
||||
t.expects(near(floor.hit.point, vec2{ 2.8, 1 }));
|
||||
# From outside the grid, into the wall.
|
||||
val wall := g.ray_cast(pose::identity, vec2{ -2, 2.5 }, vec2{ 1, 0 }, 10).expect("It meets the wall.");
|
||||
t.expects(wall.x == 5 && wall.y == 2 && near(wall.hit.distance, 7) && near(wall.hit.normal, vec2{ -1, 0 }));
|
||||
t.expects(g.ray_cast(pose::identity, vec2{ -2, 2.5 }, vec2{ 1, 0 }, 6.5) is none);
|
||||
t.expects(g.ray_cast(pose::identity, vec2{ 1, 2 }, vec2{ 0, 1 }, 10) is none);
|
||||
val within := g.ray_cast(pose::identity, vec2{ 5.5, 2.5 }, vec2{ -1, 0 }, 10).expect("It begins in the wall.");
|
||||
t.expects(within.hit.distance == 0 && within.x == 5 && within.y == 2);
|
||||
val there := g.ray_cast(moved, vec2{ 8, 2.5 }, vec2{ 1, 0 }, 10).expect("It meets the moved wall.");
|
||||
t.expects(near(there.hit.distance, 7) && near(there.hit.point, vec2{ 15, 2.5 }));
|
||||
}
|
||||
@@ -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.view()).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.view()) 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).view()).expect("A hull is made.");
|
||||
b = shape::hull_of(some_places(&mut seed, 12, 1).view()).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.view(), indices.view());
|
||||
}
|
||||
|
||||
# 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.is_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 })).is_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,288 @@
|
||||
#! 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);
|
||||
}
|
||||
|
||||
@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 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 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,294 @@
|
||||
#! 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.view()).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);
|
||||
}
|
||||
|
||||
@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 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 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.view(), indices.view()), 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