#! 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::::new(); for k in 0..8u { corners.append(vec3{ (k & 1) != 0 ? 1f : -1f, (k & 2) != 0 ? 0.5f : -0.5f, (k & 4) != 0 ? 0.5f : -0.5f }); } val hulled := shape::hull_of(corners.span).expect("A box's hull is made.").mass(1); t.expects(close(hulled.mass, 2, 1e-4) && close(hulled.inertia.x_axis.x, box.inertia.x_axis.x, 1e-4) && close(hulled.inertia.y_axis.y, box.inertia.y_axis.y, 1e-4)); # A capsule along x: about its own axis as a cylinder and a ball, the least. val pill := shape::capsule(vec3{ -1, 0, 0 }, vec3{ 1, 0, 0 }, 0.5).mass(1); t.expects(close(pill.mass, math::pi as f32 * 0.25 * 2 + 4f / 3f * math::pi as f32 * 0.125, 1e-4)); t.expects(pill.inertia.x_axis.x < pill.inertia.y_axis.y && close(pill.inertia.y_axis.y, pill.inertia.z_axis.z, 1e-4)); # Two together, about their joint center. val both := box.add(&shape::box(vec3{ 1, 0.5, 0.5 }, vec3{ 0, 3, 0 }).mass(1)); t.expects(close(both.mass, 4, 1e-4) && close(both.center.y, 1.5, 1e-4) && close(both.inertia.x_axis.x, 2 * box.inertia.x_axis.x + 4 * 1.5 * 1.5, 1e-3)); } @test def drops_a_box_onto_the_ground(t: &mut tester): void { var s := space::new(); _ = grounded(&mut s); val crate := cube(&mut s, vec3{ 0, 4, 0 }, 0.5); t.expects(close(s.mass_of(crate).mass, 1, 1e-4)); var began: u32 = 0; for i in 0..120u { s.step(1f / 60f); for e in s.events() { if e.began { began++; } } } val at := s.pose_of(crate); t.expects(close(at.at, vec3{ 0, 0.5, 0 }, 0.01) && close(at.turn.dot(quat::identity), 1, 1e-4)); t.expects(s.velocity_of(crate).length < 0.01 && began == 1 && s.contact_count == 1); # Touching the ground still, as the space visits it. var touching: u32 = 0; s.each_touching(def(x: collider_id, y: collider_id, sensor: bool): bool { touching++; return !sensor; }); t.expects(touching == 1); } @test def stands_a_tower_still(t: &mut tester): void { var s := space::new(); s.sleeping = false; _ = grounded(&mut s); var top := body_id{}; for i in 0..8u { top = cube(&mut s, vec3{ 0, 0.5 + i as f32, 0 }, 0.5); } steps(&mut s, 4); t.expects(close(s.pose_of(top).at, vec3{ 0, 7.5, 0 }, 0.05) && s.velocity_of(top).length < 0.01); } @test def sleeps_and_wakes(t: &mut tester): void { var s := space::new(); _ = grounded(&mut s); val lower := cube(&mut s, vec3{ 0, 0.5, 0 }, 0.5); val upper := cube(&mut s, vec3{ 0, 1.5, 0 }, 0.5); val apart := cube(&mut s, vec3{ 10, 0.5, 0 }, 0.5); steps(&mut s, 2); t.expects(!s.is_awake(lower) && !s.is_awake(upper) && !s.is_awake(apart)); s.apply_impulse(upper, vec3{ 0.1, 0, 0 }, s.center_of(upper)); t.expects(s.is_awake(lower) && s.is_awake(upper) && !s.is_awake(apart)); steps(&mut s, 2); t.expects(!s.is_awake(upper)); s.remove(lower); t.expects(s.is_awake(upper) && !s.has(lower)); steps(&mut s, 1); t.expects(close(s.pose_of(upper).at.y, 0.5, 0.02)); } @test def pulls_bodies_by_gravities_of_their_own(t: &mut tester): void { # A space of no gravity, but a world in its middle pulling a rock toward # its middle wherever the rock is. var s := space::new(vec3::zero); val world := s.add(body{ kind = body_kind::static }); _ = s.attach(world, collider{ shape = shape::sphere(10) }); val rock := s.add(body{ at = pose::of(vec3{ 0, 0, 14 }), gravity = vec3{ 0, 0, -10 } }); _ = s.attach(rock, collider{ shape = shape::sphere(0.5) }); for i in 0..240u { s.set_gravity(rock, s.center_of(rock).normalized() * -10); s.step(1f / 60f); } # It fell onto the world's side, and rests there. t.expects(close(s.center_of(rock), vec3{ 0, 0, 10.5 }, 0.05) && close(s.gravity_of(rock), vec3{ 0, 0, -10 }, 0.01)); steps(&mut s, 2); t.expects(!s.is_awake(rock)); # Its pull found again, as it was, leaves it asleep; turned away, it wakes # it, and the rock falls away. s.set_gravity(rock, vec3{ 0, 0, -10.001 }); t.expects(!s.is_awake(rock)); s.set_gravity(rock, vec3{ 0, 0, 10 }); steps(&mut s, 1); t.expects(s.center_of(rock).z > 13); } @test def bounces_and_slides(t: &mut tester): void { var s := space::new(); s.sleeping = false; _ = grounded(&mut s); val ball := s.add(body{ at = pose::of(vec3{ 0, 2.5, 0 }) }); _ = s.attach(ball, collider{ shape = shape::sphere(0.5), restitution = 1 }); var highest: f32 = 0; var fell := false; for i in 0..90u { s.step(1f / 60f); val y := s.pose_of(ball).at.y; if y < 0.6 { fell = true; } if fell { highest = math::max(highest, y); } } t.expects(fell && close(highest, 2.5, 0.15)); # On a slope of 30°, a box of friction 1 stays, and of none slides. var hill := space::new(); hill.sleeping = false; val tilt := quat::axis_angle(vec3::back, math::pi as f32 / 6); val slope := hill.add(body{ kind = body_kind::static, at = pose::of(vec3::zero, tilt) }); _ = hill.attach(slope, collider{ shape = shape::box(vec3{ 20, 0.5, 20 }) }); val up := tilt * vec3::up; val sticky := hill.add(body{ at = pose::of(up, tilt) }); _ = hill.attach(sticky, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), friction = 1 }); val slick := hill.add(body{ at = pose::of(up + vec3{ 0, 0, 5 }, tilt) }); _ = hill.attach(slick, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), friction = 0 }); steps(&mut hill, 1); t.expects(hill.velocity_of(sticky).length < 0.05 && close(hill.velocity_of(slick).length, 5, 0.25)); } @test def meets_by_layers_and_sensors(t: &mut tester): void { var s := space::new(); s.sleeping = false; _ = grounded(&mut s); val ghost := s.add(body{ at = pose::of(vec3{ -5, 1, 0 }) }); _ = s.attach(ghost, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), layers = 2, mask = 2 }); val zone := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 5, 4, 0 }) }); val field := s.attach(zone, collider{ shape = shape::box(vec3{ 1, 1, 1 }), sensor = true }); val stone := s.add(body{ at = pose::of(vec3{ 5, 7, 0 }) }); val heavy := s.attach(stone, collider{ shape = shape::sphere(0.25) }); var entered := false; var left := false; for i in 0..120u { s.step(1f / 60f); for e in s.events() { if e.sensor && ((e.a == field && e.b == heavy) || (e.a == heavy && e.b == field)) { entered = entered || e.began; left = left || !e.began; } } } t.expects(s.pose_of(ghost).at.y < -5 && entered && left && close(s.pose_of(stone).at.y, 0.25, 0.01)); } @test def senses_what_moves_and_filters_queries(t: &mut tester): void { var s := space::new(); s.sleeping = false; # A sensor standing still notes a kinematic body moving through it, as a gem # notes a character walking, but never what stands still beside it. val gem := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 0, 0, 0 }) }); val field := s.attach(gem, collider{ shape = shape::sphere(0.5), sensor = true, data = 7 }); val post := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 0.5, 0, 0 }) }); val pole := s.attach(post, collider{ shape = shape::box(vec3{ 0.25, 0.25, 0.25 }), data = 8 }); val walker := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec3{ -3, 0, 0 }), velocity = vec3{ 3, 0, 0 } }); val feet := s.attach(walker, collider{ shape = shape::sphere(0.25), data = 9 }); var walked_in := false; var walked_out := false; var post_met := false; for i in 0..120u { s.step(1f / 60f); for e in s.events() { val of_field := e.a == field || e.b == field; if of_field && (e.a == feet || e.b == feet) { walked_in = walked_in || e.began; walked_out = walked_out || !e.began; } post_met = post_met || (of_field && (e.a == pole || e.b == pole)); } } t.expects(walked_in && walked_out && !post_met); # Queries pass over sensors unless asked of them, and the asker's own. val probe := shape::sphere(0.1); var met: u32 = 0; s.overlap(&probe, pose::identity, 0xFFFFFFFF, def(c: collider_id, b: body_id): bool { met++; return true; }); t.expects(met == 0); s.overlap(&probe, pose::identity, query_filter{ sensors = true }, def(c: collider_id, b: body_id): bool { met++; return true; }); t.expects(met == 1); t.expects(s.ray_cast(vec3{ -2, 0, 0 }, vec3{ 1, 0, 0 }, 10) is some(&hit) && hit.collider == pole); t.expects(s.ray_cast(vec3{ -2, 0, 0 }, vec3{ 1, 0, 0 }, 10, query_filter{ sensors = true }) is some(&hit) && hit.collider == field); t.expects(s.ray_cast(vec3{ -2, 0, 0 }, vec3{ 1, 0, 0 }, 10, query_filter{ ignore = 8 }) is some(&hit) && hit.collider == feet); } @test def carries_what_rests_on_a_kinematic_body(t: &mut tester): void { var s := space::new(); _ = grounded(&mut s); val lift := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec3{ 0, 2, 0 }) }); _ = s.attach(lift, collider{ shape = shape::box(vec3{ 2, 0.25, 2 }) }); val crate := cube(&mut s, vec3{ 0, 2.75, 0 }, 0.5); steps(&mut s, 1); s.set_velocity(lift, vec3{ 1, 0, 0 }); steps(&mut s, 1); t.expects(close(s.pose_of(lift).at, vec3{ 1, 2, 0 }, 0.02)); t.expects(close(s.pose_of(crate).at.x, 1, 0.1) && close(s.velocity_of(crate).x, 1, 0.1)); } @test def holds_bodies_by_joints(t: &mut tester): void { var s := space::new(); s.sleeping = false; val ground := grounded(&mut s); # A pendulum on a ball joint keeps its bob as far from it. val bob := s.add(body{ at = pose::of(vec3{ 3, 5, 0 }) }); _ = s.attach(bob, collider{ shape = shape::sphere(0.25) }); _ = s.ball(ground, bob, vec3{ 0, 5, 0 }); var farthest: f32 = 0; var nearest: f32 = 10; for i in 0..120u { s.step(1f / 60f); val reach := (s.pose_of(bob).at - vec3{ 0, 5, 0 }).length; farthest = math::max(farthest, reach); nearest = math::min(nearest, reach); } t.expects(close(farthest, 3, 0.03) && close(nearest, 3, 0.03)); # A wheel on a hinge, turned by a motor at its speed about its axle alone. val wheel := s.add(body{ at = pose::of(vec3{ 10, 3, 0 }), gravity_scale = 0, angular_damping = 0 }); _ = s.attach(wheel, collider{ shape = shape::sphere(0.5) }); val axle := s.hinge(ground, wheel, vec3{ 10, 3, 0 }, vec3::back); var turning := s.joint_of(axle); turning.motor = true; turning.motor_speed = 2; turning.max_motor_force = 100; s.change(axle, turning); steps(&mut s, 1); t.expects(close(s.spin_of(wheel), vec3{ 0, 0, 2 }, 0.02) && close(s.pose_of(wheel).at, vec3{ 10, 3, 0 }, 1e-3)); # A door, hinged up its side, limited to a quarter turn, swung by a push. val door := s.add(body{ at = pose::of(vec3{ 21, 4, 0 }), gravity_scale = 0 }); _ = s.attach(door, collider{ shape = shape::box(vec3{ 1, 1, 0.1 }) }); val hinge := s.hinge(ground, door, vec3{ 20, 4, 0 }, vec3::up); var limited := s.joint_of(hinge); limited.limit = true; limited.lower = -math::pi as f32 / 4; limited.upper = math::pi as f32 / 4; s.change(hinge, limited); s.apply_impulse(door, vec3{ 0, 0, -3 }, vec3{ 22, 4, 0 }); steps(&mut s, 2); t.expects(close(s.angle_of(hinge), math::pi as f32 / 4, 0.03) && close(s.pose_of(door).turn.y * s.pose_of(door).turn.y + s.pose_of(door).turn.w * s.pose_of(door).turn.w, 1, 1e-3)); # A rod keeps two bodies apart; a fixed joint keeps them as one. val left := s.add(body{ at = pose::of(vec3{ -10, 6, 0 }) }); _ = s.attach(left, collider{ shape = shape::sphere(0.25) }); val right := s.add(body{ at = pose::of(vec3{ -8, 6, 0 }) }); _ = s.attach(right, collider{ shape = shape::sphere(0.25) }); _ = s.tie(left, right, vec3{ -10, 6, 0 }, vec3{ -8, 6, 0 }); val stuck := cube(&mut s, vec3{ -10, 7, 0 }, 0.25); _ = s.fix(left, stuck, vec3{ -10, 6.5, 0 }); steps(&mut s, 2); t.expects(close((s.pose_of(right).at - s.pose_of(left).at).length, 2, 0.02)); val held := s.pose_of(left).unapply(s.pose_of(stuck).at); t.expects(close(held, vec3{ 0, 1, 0 }, 0.03) && close(math::abs(s.pose_of(left).turn.dot(s.pose_of(stuck).turn)), 1, 1e-3)); } @test def slides_across_a_mesh_without_catching(t: &mut tester): void { var s := space::new(); s.sleeping = false; # A floor of 20 by 20 squares, two triangles each, its top at y = 0. var points := vector::::new(); var indices := vector::::new(); val n: u32 = 20; for z in 0..n + 1 { for x in 0..n + 1 { points.append(vec3{ x as f32 - 10, 0, z as f32 - 10 }); } } for z in 0..n { for x in 0..n { val at := z * (n + 1) + x; indices.append(at); indices.append(at + n + 1); indices.append(at + 1); indices.append(at + 1); indices.append(at + n + 1); indices.append(at + n + 2); } } val floor := s.add(body{ kind = body_kind::static }); val ground := s.attach_mesh(floor, triangle_mesh::new(points.span, indices.span), collider{ friction = 0 }); val crate := s.add(body{ at = pose::of(vec3{ -8, 0.5, 0.3 }), velocity = vec3{ 4, 0, 0 } }); _ = s.attach(crate, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), friction = 0 }); var lowest: f32 = 1; var slowest: f32 = 4; for i in 0..180u { s.step(1f / 60f); lowest = math::min(lowest, s.pose_of(crate).at.y); slowest = math::min(slowest, s.velocity_of(crate).x); } # Across 12 seams it neither sank, bounced, nor slowed. t.expects(close(s.pose_of(crate).at.x, 4, 0.1) && close(lowest, 0.5, 0.01) && close(slowest, 4, 0.05)); t.expects(close(s.pose_of(crate).at.y, 0.5, 0.01) && math::abs(s.spin_of(crate).length) < 0.01); # A ray meets the mesh's triangle under it. val hit := s.ray_cast(vec3{ 2.5, 5, 2.5 }, vec3::down, 10, 0xFFFFFFFF).expect("It meets the floor."); t.expects(hit.collider == ground && close(hit.distance, 5, 1e-4) && hit.triangle != 0xFFFFFFFF); } @test def casts_rays_in_space(t: &mut tester): void { var s := space::new(); val ground := grounded(&mut s); val crate := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 3, 0.5, 0 }) }); val side := s.attach(crate, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), layers = 2 }); val hit := s.ray_cast(vec3{ 0, 0.5, 0 }, vec3::right, 10).expect("It meets the crate."); t.expects(hit.collider == side && hit.body == crate && close(hit.distance, 2.5, 1e-4) && close(hit.normal, vec3::left, 1e-4)); val down := s.ray_cast(vec3{ 3, 5, 0 }, vec3::down, 10, 1).expect("It passes the crate to the ground."); t.expects(down.body == ground && close(down.distance, 5, 1e-4)); t.expects(s.ray_cast(vec3{ 0, 5, 0 }, vec3::up, 10) is none); }