#! Contains the tests of `physics3d`'s characters: walking a floor, stepping up #! a stair but not a wall, walking up a gentle ramp but not a steep one, #! sliding along a wall, riding a moving platform, pushed by what moves into #! it, and pushing a crate. import std::testing; import std::math; import malunal::linear; import malunal::physics3d; def about(a: f32, b: f32, within: f32): bool { return math::abs(a - b) <= within; } # A space with a floor, its top at y 0, and a character standing on it. def standing(s: &mut space, at: vec3): character { val floor := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ 0, -1, 0 }) }); _ = s.attach(floor, collider{ shape = shape::box(vec3{ 50, 1, 50 }) }); var c := character{ at = at }; s.move_character(&mut c, vec3::zero, 1f / 60f); return c; } # Walks a character at a velocity across the ground for a time, falling where # it's in the air, the space stepped beside it. def walk(s: &mut space, c: &mut character, along: vec3, seconds: f32): void { val n := (seconds * 60) as u32; for i in 0..n { var v := c.velocity; v.x = along.x; v.z = along.z; if !c.grounded { v.y -= 10f / 60f; } s.move_character(c, v, 1f / 60f); s.step(1f / 60f); } } def block(s: &mut space, center: vec3, half: vec3, turn: quat = quat::identity): body_id { val made := s.add(body{ kind = body_kind::static, at = pose::of(center, turn) }); _ = s.attach(made, collider{ shape = shape::box(half) }); return made; } @test def walks_floors(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec3{ 0, 1, 0 }); walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 0.5); # It fell to the floor, stands on it, and walks across it. t.expects(c.grounded && about(c.at.y, 0.92, 0.01)); walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 2); t.expects(c.grounded && about(c.at.x, 5, 0.1) && about(c.at.y, 0.92, 0.01)); } @test def steps_up_stairs_but_not_walls(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec3{ 0, 0.92, 0 }); _ = block(&mut s, vec3{ 3, 0.15, 0 }, vec3{ 1, 0.15, 2 }); _ = block(&mut s, vec3{ 8, 0.3, 0 }, vec3{ 1, 0.3, 2 }); walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 4); # Up the stair 30 centimeters high, onto it and off it, then stopped by the # block 60 centimeters high. t.expects(c.grounded && about(c.at.x, 7 - 0.3 - 0.02, 0.05) && about(c.at.y, 0.92, 0.02)); } @test def walks_up_gentle_ramps_but_not_steep_ones(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec3{ 0, 0.92, 0 }); # A ramp of 20 degrees rising toward +x, from x 1. val gentle := quat::axis_angle(vec3::back, 0.35); _ = block(&mut s, vec3{ 1, 0, 0 } + gentle * vec3{ 4, -0.5, 0 }, vec3{ 4, 0.5, 2 }, gentle); walk(&mut s, &mut c, vec3{ 2, 0, 0 }, 2); t.expects(c.grounded && c.at.y > 1.5); var steep_space := space::new(); var d := standing(&mut steep_space, vec3{ 0, 0.92, 0 }); val steep := quat::axis_angle(vec3::back, 1.1); _ = block(&mut steep_space, vec3{ 1, 0, 0 } + steep * vec3{ 4, -0.5, 0 }, vec3{ 4, 0.5, 2 }, steep); walk(&mut steep_space, &mut d, vec3{ 2, 0, 0 }, 2); t.expects(d.at.y < 1.3 && d.at.x < 1.5); } @test def slides_along_walls(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec3{ 0, 0.92, 0 }); _ = block(&mut s, vec3{ 2, 1, 0 }, vec3{ 0.5, 1, 20 }); # Walking into the wall at 45 degrees, it slides along it. walk(&mut s, &mut c, vec3{ 2, 0, 2 }, 2); t.expects(about(c.at.x, 1.5 - 0.3 - 0.02, 0.03) && c.at.z > 3); } @test def rides_platforms_and_is_pushed(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec3{ 0, 0.92, 0 }); val lift := s.add(body{ kind = body_kind::kinematic, at = pose::of(vec3{ 0, 0.25, 0 }) }); _ = s.attach(lift, collider{ shape = shape::box(vec3{ 1, 0.1, 1 }) }); c.at = vec3{ 0, 0.35 + 0.92, 0 }; s.move_character(&mut c, vec3::zero, 1f / 60f); s.set_velocity(lift, vec3{ 1, 0, 0 }); walk(&mut s, &mut c, vec3::zero, 2); # Carried along by the platform it stands on. t.expects(c.grounded && about(c.at.x, s.pose_of(lift).at.x, 0.1) && about(s.pose_of(lift).at.x, 2, 0.05)); # A wall moving into it pushes it along. var w := space::new(); var d := standing(&mut w, vec3{ 0, 0.92, 0 }); val pusher := w.add(body{ kind = body_kind::kinematic, at = pose::of(vec3{ -2, 1, 0 }) }); _ = w.attach(pusher, collider{ shape = shape::box(vec3{ 0.5, 1, 2 }) }); w.set_velocity(pusher, vec3{ 1, 0, 0 }); walk(&mut w, &mut d, vec3::zero, 3); t.expects(d.at.x > 0.5 && d.at.x > w.pose_of(pusher).at.x + 0.5 + 0.25); } @test def walks_around_a_world(t: &mut tester): void { # A world of 20 meters, up away from its middle wherever the character is. var s := space::new(vec3::zero); val world := s.add(body{ kind = body_kind::static }); _ = s.attach(world, collider{ shape = shape::sphere(20) }); var c := character{ at = vec3{ 0, 21, 0 } }; # Walking at 3 meters a second along the ground, around the world's middle # toward +x, falling toward it where it's in the air. Up turns as it walks, # so what was along the ground a step ago leans up from it: on the ground, # it keeps nothing of how fast it moved up. for i in 0..600u { c.up = c.at.normalized(); var v := c.up.cross(vec3::back).normalized() * 3; if !c.grounded { v = v + c.up * (c.velocity.dot(c.up) - 10f / 60f); } s.move_character(&mut c, v, 1f / 60f); s.step(1f / 60f); } # It stands on the ground, as far from the middle as its feet put it, 30 # meters around from where it began: its shape stood along its up all along. val around := math::pi as f32 / 2 - math::atan2(c.at.y, c.at.x); t.expects(c.grounded && about(c.at.length, 20.92, 0.01) && about(around, 30f / 20.92f, 0.05)); t.expects(c.ground_normal.dot(c.up) > 0.99 && about(c.at.z, 0, 0.01)); # Its pose stands its shape along its up. t.expects((c.pose().turn * vec3::up - c.up).length < 0.001); } @test def pushes_crates(t: &mut tester): void { var s := space::new(); var c := standing(&mut s, vec3{ 0, 0.92, 0 }); val crate := s.add(body{ at = pose::of(vec3{ 1.5, 0.5, 0 }) }); _ = s.attach(crate, collider{ shape = shape::box(vec3{ 0.5, 0.5, 0.5 }), density = 20 }); walk(&mut s, &mut c, vec3{ 1.5, 0, 0 }, 3); t.expects(s.pose_of(crate).at.x > 3 && c.at.x < s.pose_of(crate).at.x); }