#! Contains the tests of `physics2d`'s characters: walking ground, stepping up #! a stair but not a wall, walking up a gentle slope but not a steep one, #! riding a moving platform, jumping up through a one-way platform and landing #! on it, and walking a chain of segments. import std::testing; import std::containers; import std::math; import malunal::linear; import malunal::physics2d; def about(a: f32, b: f32, within: f32): bool { return math::abs(a - b) <= within; } def standing(s: &mut space, at: vec2): character { val floor := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, -1 }) }); _ = s.attach(floor, collider{ shape = shape::box(vec2{ 50, 1 }) }); var c := character{ at = at }; s.move_character(&mut c, vec2::zero, 1f / 60f); return c; } # Walks a character for a time, falling where it's in the air, jumping at # first where it's asked. def walk(s: &mut space, c: &mut character, along: f32, seconds: f32, jump: f32 = 0): void { val n := (seconds * 60) as u32; for i in 0..n { var v := c.velocity; v.x = along; if !c.grounded { v.y -= 10f / 60f; } else if i == 0 && jump > 0 { v.y = jump; } s.move_character(c, v, 1f / 60f); s.step(1f / 60f); } } def block(s: &mut space, center: vec2, half: vec2, angle: f32 = 0): void { val made := s.add(body{ kind = body_kind::static, at = pose::of(center, angle) }); _ = s.attach(made, collider{ shape = shape::box(half) }); } @test def walks_and_steps(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec2{ 0, 1 }); walk(&mut s, &mut c, 0, 0.5); t.expects(c.grounded && about(c.at.y, 0.92, 0.01)); block(&mut s, vec2{ 3, 0.125 }, vec2{ 1, 0.125 }); block(&mut s, vec2{ 8, 0.3 }, vec2{ 1, 0.3 }); walk(&mut s, &mut c, 2, 4); t.expects(c.grounded && about(c.at.x, 7 - 0.3 - 0.02, 0.05) && about(c.at.y, 0.92, 0.02)); } @test def walks_around_a_world(t: &mut tester): void { # A world of 20 meters, up away from its middle wherever the character is. var s := space::new(vec2::zero); val world := s.add(body{ kind = body_kind::static }); _ = s.attach(world, collider{ shape = shape::circle(20) }); var c := character{ at = vec2{ 0, 21 } }; # Walking at 3 meters a second along the ground, clockwise around the # world's middle, falling toward it where it's in the air, and on the # ground keeping nothing of how fast it moved up. for i in 0..600u { c.up = c.at.normalized(); var v := vec2{ c.up.y, -c.up.x } * 3; if !c.grounded { v = v + c.up * (c.velocity.dot(c.up) - 10f / 60f); } s.move_character(&mut c, v, 1f / 60f); s.step(1f / 60f); } # It stands on the ground, as far from the middle as its feet put it, 30 # meters around from where it began, its shape standing along its up. val around := math::pi as f32 / 2 - math::atan2(c.at.y, c.at.x); t.expects(c.grounded && about(c.at.length, 20.92, 0.01) && about(around, 30f / 20.92f, 0.05)); t.expects(c.ground_normal.dot(c.up) > 0.99 && (c.pose().turn.apply(vec2{ 0, 1 }) - c.up).length < 0.001); } @test def walks_gentle_slopes_but_not_steep_ones(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec2{ 0, 0.92 }); block(&mut s, vec2{ 1, 0 } + rot::of(0.35).apply(vec2{ 4, -0.5 }), vec2{ 4, 0.5 }, 0.35); walk(&mut s, &mut c, 2, 2); t.expects(c.grounded && c.at.y > 1.5); var steep := space::new(); var d := standing(&mut steep, vec2{ 0, 0.92 }); block(&mut steep, vec2{ 1, 0 } + rot::of(1.1).apply(vec2{ 4, -0.5 }), vec2{ 4, 0.5 }, 1.1); walk(&mut steep, &mut d, 2, 2); t.expects(d.at.y < 1.3 && d.at.x < 1.5); } @test def rides_moving_platforms(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec2{ 0, 0.92 }); val lift := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec2{ 0, 0.25 }) }); _ = s.attach(lift, collider{ shape = shape::box(vec2{ 1, 0.1 }) }); c.at = vec2{ 0, 0.35 + 0.92 }; s.move_character(&mut c, vec2::zero, 1f / 60f); s.set_velocity(lift, vec2{ 1, 0.5 }); walk(&mut s, &mut c, 0, 2); t.expects(c.grounded && about(c.at.x, s.pose_of(lift).at.x, 0.1) && about(c.at.y, s.pose_of(lift).at.y + 0.1 + 0.92, 0.03)); } @test def jumps_up_through_one_way_platforms(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec2{ 0, 0.92 }); val ledge := s.add(body{ kind = body_kind::static, at = pose::of(vec2{ 0, 2 }) }); _ = s.attach(ledge, collider{ shape = shape::box(vec2{ 2, 0.1 }), one_way = true }); walk(&mut s, &mut c, 0, 2, 9); t.expects(c.grounded && about(c.at.y, 2.1 + 0.92, 0.02)); } @test def walks_chains(t: &mut tester): void { var s := space::new(); # Ground of a chain, bumpy but gentle, right to left so it faces up. var places := vector::::new(); var i: i64 = 40; while i >= 0 { places.append(vec2{ i as f32 * 0.5 - 2, i % 2 == 0 ? 0f : 0.05f }); i--; } val ground := s.add(body{ kind = body_kind::static }); _ = s.attach_chain(ground, places.span, false, collider{}); var c := character{ at = vec2{ 0, 1 } }; walk(&mut s, &mut c, 0, 0.5); walk(&mut s, &mut c, 3, 3); t.expects(c.grounded && about(c.at.x, 9, 0.2) && c.at.y > 0.9 && c.at.y < 1); }