Files
physics/tools/physics2d_bench.sakura
T
2026-11-11 11:53:37 -06:00

107 lines
3.8 KiB
Plaintext

#! Times `malunal::physics2d`'s space stepping two of Box2D's benchmark scenes,
#! built as Box2D's benchmark builds them, so the times are comparable: a
#! pyramid of 5050 boxes, 100 at its base, stepped 500 times, and 400 pyramids
#! of 55 boxes on 20 floors, stepped 200 times, each at 60 steps a second of 4
#! substeps, with sleeping off. Each prints how long it took, and where its top
#! box ended, how far it settled, which a stable stack keeps to millimeters.
#!
#! `blossom build`, then run `malunal.physics2d.bench`.
import std::io;
import std::math;
import std::time;
import std::containers;
import malunal::linear;
import malunal::physics2d;
def large_pyramid(s: &mut space): body_id {
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 }) });
val base: u32 = 100;
val a: f32 = 0.5;
var top := body_id{};
for i in 0..base {
val y := (2 * i as f32 + 1) * a;
for j in i..base {
val x := (i as f32 + 1) * a + 2 * (j - i) as f32 * a - a * base as f32;
top = s.add(body{ at = pose::of(vec2{ x, y }) });
_ = s.attach(top, collider{ shape = shape::box(vec2{ a, a }) });
}
}
return top;
}
def small_pyramid(s: &mut space, base: u32, extent: f32, center_x: f32, base_y: f32): body_id {
var top := body_id{};
for i in 0..base {
val y := (2 * i as f32 + 1) * extent + base_y;
for j in i..base {
val x := (i as f32 + 1) * extent + 2 * (j - i) as f32 * extent + center_x - 0.5;
top = s.add(body{ at = pose::of(vec2{ x, y }) });
_ = s.attach(top, collider{ shape = shape::box(vec2{ extent, extent }) });
}
}
return top;
}
def many_pyramids(s: &mut space): body_id {
val base: u32 = 10;
val extent: f32 = 0.5;
val rows: u32 = 20;
val columns: u32 = 20;
val ground := s.add(body{ kind = body_kind::static });
val ground_step := 2 * extent * (base as f32 + 1);
val width := 2 * extent * columns as f32 * (base as f32 + 1);
var y: f32 = 0;
for i in 0..rows {
_ = s.attach(ground, collider{ shape = shape::segment(vec2{ -0.5 * width, y }, vec2{ 0.5 * width, y }) });
y += ground_step;
}
val base_width := 2 * extent * base as f32;
var base_y: f32 = 0;
var top := body_id{};
for i in 0..rows {
for j in 0..columns {
val center_x := -0.5 * width + j as f32 * (base_width + 2 * extent) + 2 * extent;
top = small_pyramid(s, base, extent, center_x, base_y);
}
base_y += ground_step;
}
return top;
}
def run(name: strview, s: &mut space, top: body_id, steps: u32): void {
val from := s.pose_of(top).at;
val at := instant::now();
var parts := step_profile{};
for i in 0..steps {
s.step(1f / 60f);
parts.pairs += s.profile.pairs;
parts.contacts += s.profile.contacts;
parts.solve += s.profile.solve;
}
val ms := at.elapsed().in_nanoseconds as f64 / 1e6;
val to := s.pose_of(top).at;
println($"{name}: {ms} ms, {ms / steps as f64} ms a step; {s.body_count} bodies, {s.contact_count} contacts; top from ({from.x}, {from.y}) to ({to.x}, {to.y}), turned {s.pose_of(top).turn.angle}");
println($" finding pairs {parts.pairs / steps as f64} ms, contacts {parts.contacts / steps as f64} ms, solving {parts.solve / 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 large := space::new();
large.sleeping = false;
large.threads = threads;
val large_top := large_pyramid(&mut large);
run("large_pyramid", &mut large, large_top, 500);
var many := space::new();
many.sleeping = false;
many.threads = threads;
val many_top := many_pyramids(&mut many);
run("many_pyramids", &mut many, many_top, 200);
}
}