Skip to main content

demo_scenes/
demo_scenes.rs

1// Repro of the demo scenes: Bodies (mixed shower into container) and
2// Stacking (pyramid + heavy ball).
3use box2d_rust::body::{create_body, get_body_full_id, get_body_transform};
4use box2d_rust::geometry::make_box;
5use box2d_rust::math_functions as m;
6use box2d_rust::shape::{create_circle_shape, create_polygon_shape};
7use box2d_rust::types::{default_body_def, default_shape_def, default_world_def, BodyType};
8use box2d_rust::world::{world_step, World};
9
10fn add_static_box(world: &mut World, x: f32, y: f32, hx: f32, hy: f32) -> i32 {
11    let mut body_def = default_body_def();
12    body_def.position = m::to_pos(m::Vec2 { x, y });
13    let id = create_body(world, &body_def);
14    let def = default_shape_def();
15    let poly = make_box(hx, hy);
16    create_polygon_shape(world, id, &def, &poly);
17    get_body_full_id(world, id)
18}
19
20fn add_box(world: &mut World, x: f32, y: f32, hx: f32, hy: f32) -> i32 {
21    let mut body_def = default_body_def();
22    body_def.type_ = BodyType::Dynamic;
23    body_def.position = m::to_pos(m::Vec2 { x, y });
24    let id = create_body(world, &body_def);
25    let mut def = default_shape_def();
26    def.density = 1.0;
27    def.material.friction = 0.3;
28    let poly = make_box(hx, hy);
29    create_polygon_shape(world, id, &def, &poly);
30    get_body_full_id(world, id)
31}
32
33fn add_circle(world: &mut World, x: f32, y: f32, r: f32, density: f32) -> i32 {
34    let mut body_def = default_body_def();
35    body_def.type_ = BodyType::Dynamic;
36    body_def.position = m::to_pos(m::Vec2 { x, y });
37    let id = create_body(world, &body_def);
38    let mut def = default_shape_def();
39    def.density = density;
40    def.material.friction = 0.3;
41    def.material.restitution = 0.2;
42    let circle = box2d_rust::collision::Circle {
43        center: m::VEC2_ZERO,
44        radius: r,
45    };
46    create_circle_shape(world, id, &def, &circle);
47    get_body_full_id(world, id)
48}
49
50fn main() {
51    // === Bodies demo scene ===
52    let mut wd = default_world_def();
53    wd.gravity = m::Vec2 { x: 0.0, y: -10.0 };
54    let mut world = World::new(&wd);
55
56    add_static_box(&mut world, 0.0, -0.5, 13.0, 0.5);
57    add_static_box(&mut world, -12.2, 2.0, 0.3, 2.0);
58    add_static_box(&mut world, 12.2, 2.0, 0.3, 2.0);
59
60    let mut tracked = Vec::new();
61    for i in 0..24usize {
62        let x = -6.0 + (i % 8) as f32 * 1.7 + 0.13 * (i % 3) as f32;
63        let y = 5.0 + (i / 8) as f32 * 1.6;
64        if i % 2 == 0 {
65            let hx = 0.25 + 0.2 * ((i * 7) % 3) as f32 * 0.5;
66            tracked.push(add_box(&mut world, x, y, hx, hx));
67        } else {
68            let r = 0.22 + 0.16 * ((i * 5) % 3) as f32 * 0.5;
69            tracked.push(add_circle(&mut world, x, y, r, 1.0));
70        }
71    }
72
73    for step in 0..600 {
74        world_step(&mut world, 1.0 / 60.0, 4);
75        if step % 100 == 0 {
76            let t = get_body_transform(&world, tracked[0]);
77            println!(
78                "bodies step {step}: body0 = ({:.3}, {:.3}), contacts = {}",
79                t.p.x,
80                t.p.y,
81                world.contact_id_pool.id_count()
82            );
83        }
84    }
85    println!("BODIES SCENE OK");
86
87    // === Stacking demo scene ===
88    let mut world = World::new(&wd);
89    add_static_box(&mut world, 0.0, -0.5, 11.0, 0.5);
90    let h = 0.4f32;
91    let base = 9i32;
92    for row in 0..base {
93        let count = base - row;
94        let y = h + row as f32 * 2.0 * h;
95        for i in 0..count {
96            let x = (i as f32 - (count - 1) as f32 / 2.0) * 2.05 * h;
97            add_box(&mut world, x, y, h, h);
98        }
99    }
100    for _ in 0..600 {
101        world_step(&mut world, 1.0 / 60.0, 4);
102    }
103    println!(
104        "stacking settled: awake = {}",
105        world.solver_sets[box2d_rust::solver_set::AWAKE_SET as usize]
106            .body_sims
107            .len()
108    );
109    // Drop the heavy ball
110    add_circle(&mut world, 0.3, 9.0, 0.5, 4.0);
111    for _ in 0..600 {
112        world_step(&mut world, 1.0 / 60.0, 4);
113    }
114    println!("STACKING SCENE OK");
115}