#! Times `malunal::physics3d`'s space stepping two of Jolt's performance test #! scenes, built as Jolt's builds them, so the times are comparable: a pyramid #! of 1240 boxes, 15 tiers high, none sleeping, and 1764 boxes, spheres, #! capsules, and pyramids dropped onto a terrain of 20000 triangles, bouncing #! and coming to rest, each stepped 500 times at 60 steps a second. The #! pyramid prints where its top box ended: settled on the tiers below, its #! middle 29 above the floor. #! #! `blossom build`, then run `malunal.physics3d.bench`. import std::io; import std::math; import std::time; import std::containers; import malunal::linear; import malunal::physics3d; def pyramid(s: &mut space): body_id { 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 }) }); val height: i32 = 15; var top := body_id{}; for i in 0..height { val shift: f32 = (i & 1) != 0 ? 1f : 0f; var j := i / 2; while j < height - (i + 1) / 2 { var k := i / 2; while k < height - (i + 1) / 2 { val at := vec3{ (-height + 2 * j) as f32 + shift, 1 + 2.5 * i as f32, (-height + 2 * k) as f32 + shift }; top = s.add(body{ at = pose::of(at), can_sleep = false }); _ = s.attach(top, collider{ shape = shape::box(vec3{ 1, 1, 1 }) }); k++; } j++; } } return top; } def terrain(s: &mut space): void { val n: u32 = 100; val cell: f32 = 3; val tallest: f32 = 5; var points := vector::::new(); points.resize(((n + 1) * (n + 1)) as u64, vec3::zero); for x in 0..n + 1 { for z in 0..n + 1 { val height := math::sin(x as f32 * 50 / n as f32) * math::cos(z as f32 * 50 / n as f32); points[(z * (n + 1) + x) as u64] = vec3{ cell * x as f32, tallest * height, cell * z as f32 }; } } var indices := vector::::new(); for x in 0..n { for z in 0..n { val start := (n + 1) * z + x; indices.append(start); indices.append(start + n + 1); indices.append(start + 1); indices.append(start + 1); indices.append(start + n + 1); indices.append(start + n + 2); } } val center := n as f32 * cell / 2; val ground := s.add(body{ kind = body_kind::static, at = pose::of(vec3{ -center, tallest, -center }) }); _ = s.attach_mesh(ground, triangle_mesh::new(points.span, indices.span), collider{ friction = 0.5, restitution = 0.6 }); var tip: vec3[5] = [vec3{ 0, 1, 0 }, vec3{ 1, 0, 0 }, vec3{ -1, 0, 0 }, vec3{ 0, 0, 1 }, vec3{ 0, 0, -1 }]; var shapes: shape[4] = [ shape::box(vec3{ 0.5, 0.75, 1 }), shape::sphere(0.5), shape::capsule(vec3{ 0, -0.75, 0 }, vec3{ 0, 0.75, 0 }, 0.5), shape::hull_of(tip[:]).expect("A pyramid is made."), ]; var x: i32 = -10; while x <= 10 { for y in 0..4u { var z: i32 = -10; while z <= 10 { val b := s.add(body{ at = pose::of(vec3{ 7.5 * x as f32, 15 + 2 * y as f32, 7.5 * z as f32 }) }); _ = s.attach(b, collider{ shape = shapes[y], friction = 0.5, restitution = 0.6 }); z++; } } x++; } } def run(name: strview, s: &mut space, steps: u32): void { val at := instant::now(); var parts := step_profile{}; var awake_at_end: u64 = 0; for i in 0..steps { s.step(1f / 60f); parts.pairs += s.profile.pairs; parts.contacts += s.profile.contacts; parts.solve += s.profile.solve; parts.sleep += s.profile.sleep; } val ms := at.elapsed().in_nanoseconds as f64 / 1e6; println($"{name}: {ms} ms, {ms / steps as f64} ms a step, {1000 * steps as f64 / ms} steps a second; {s.body_count} bodies, {s.contact_count} contacts"); println($" finding pairs {parts.pairs / steps as f64} ms, contacts {parts.contacts / steps as f64} ms, solving {parts.solve / steps as f64} ms, sleeping {parts.sleep / steps as f64} ms a step"); } def main(): void { # On one thread, then on eight. for round in 0..2u { val threads: u32 = round == 0 ? 1 : 8; println($"on {threads} threads:"); var stacked := space::new(); stacked.sleeping = false; stacked.threads = threads; val top := pyramid(&mut stacked); val from := stacked.pose_of(top).at; run("pyramid", &mut stacked, 500); val to := stacked.pose_of(top).at; println($" top from ({from.x}, {from.y}, {from.z}) to ({to.x}, {to.y}, {to.z})"); var dropped := space::new(); dropped.threads = threads; terrain(&mut dropped); run("convex_vs_mesh", &mut dropped, 500); } }