#! Contains the tests of `physics2d`'s joints that slide and drive: a block on #! a prismatic joint slid by its motor to its limit, a cart riding wheels on #! their springs, driven by their motors, a body driven by a motor joint to a #! place and an angle, and one dragged by a mouse joint. import std::testing; import std::math; import malunal::linear; import malunal::physics2d; def within(a: f32, b: f32, by: f32): bool { return math::abs(a - b) <= by; } def seconds(s: &mut space, count: f32): void { val n := (count * 60) as u32; for i in 0..n { s.step(1f / 60f); } } @test def slides_along_prismatic_joints(t: &mut tester): void { var s := space::new(); val ground := s.add(body{ kind = body_kind::static }); val block := s.add(body{ at = pose::of(vec2{ 0, 2 }) }); _ = s.attach(block, collider{ shape = shape::box(vec2{ 0.5, 0.25 }) }); val rail := s.slide(ground, block, vec2{ 0, 2 }, vec2{ 1, 0 }); var made := s.joint_of(rail); made.motor = true; made.motor_speed = 2; made.max_motor_force = 100; made.limit = true; made.lower = -1; made.upper = 3; s.change(rail, made); seconds(&mut s, 3); # Held to its rail against gravity, unturned, it stops at its limit. val at := s.pose_of(block); t.expects(within(at.at.x, 3, 0.02) && within(at.at.y, 2, 0.01) && within(at.turn.angle, 0, 0.01)); t.expects(within(s.translation_of(rail), 3, 0.02)); } @test def rides_wheels(t: &mut tester): void { var s := space::new(); s.sleeping = false; val ground := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, -1 }) }); _ = s.attach(ground, collider{ shape = shape::box(vec2{ 100, 1 }), friction = 0.9 }); val cart := s.add(body{ at = pose::of(vec2{ 0, 1 }) }); _ = s.attach(cart, collider{ shape = shape::box(vec2{ 1, 0.2 }) }); var axles: joint_id[2] = [joint_id{}, joint_id{}]; for i in 0..2u { val x := i == 0 ? -0.8f : 0.8f; val wheel := s.add(body{ at = pose::of(vec2{ x, 0.5 }) }); _ = s.attach(wheel, collider{ shape = shape::circle(0.3), friction = 0.9 }); axles[i] = s.wheel(cart, wheel, vec2{ x, 0.5 }, vec2{ 0, 1 }); } seconds(&mut s, 1); # Resting on its springs, the cart's held above its wheels, level. val resting := s.pose_of(cart); t.expects(resting.at.y > 0.6 && resting.at.y < 1.1 && within(resting.turn.angle, 0, 0.02) && within(resting.at.x, 0, 0.05)); # Their motors turning them clockwise, it drives right. for i in 0..2u { var made := s.joint_of(axles[i]); made.motor = true; made.motor_speed = -10; made.max_motor_force = 50; s.change(axles[i], made); } seconds(&mut s, 2); t.expects(s.pose_of(cart).at.x > 3 && within(s.pose_of(cart).turn.angle, 0, 0.1)); } @test def drives_bodies_by_motor_joints(t: &mut tester): void { var s := space::new(vec2::zero); val base := s.add(body{ kind = body_kind::static }); val puck := s.add(body{ at = pose::of(vec2{ 1, 0 }) }); _ = s.attach(puck, collider{ shape = shape::box(vec2{ 0.3, 0.3 }) }); val driven := s.drive(base, puck, 50, 20); # Where it's driven, it stays; driven elsewhere, it goes, and turns. seconds(&mut s, 0.5); t.expects(within(s.pose_of(puck).at.x, 1, 0.01) && within(s.pose_of(puck).at.y, 0, 0.01)); s.drive_to(driven, vec2{ 3, 2 }, 1); seconds(&mut s, 4); val at := s.pose_of(puck); t.expects(within(at.at.x, 3, 0.05) && within(at.at.y, 2, 0.05) && within(at.turn.angle, 1, 0.05)); } @test def drags_bodies_by_mouse_joints(t: &mut tester): void { var s := space::new(); val ground := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, -1 }) }); _ = s.attach(ground, collider{ shape = shape::box(vec2{ 20, 1 }) }); val crate := s.add(body{ at = pose::of(vec2{ 0, 0.5 }) }); _ = s.attach(crate, collider{ shape = shape::box(vec2{ 0.5, 0.5 }) }); # Grabbed by its top and pulled up and over, it hangs below the target. val hand := s.grab(ground, crate, vec2{ 0, 1 }, 1000); s.drive_to(hand, vec2{ 4, 3 }); seconds(&mut s, 3); val at := s.pose_of(crate); t.expects(within(at.apply(vec2{ 0, 0.5 }).x, 4, 0.1) && within(at.apply(vec2{ 0, 0.5 }).y, 3, 0.1)); t.expects(at.at.y < 3); }