box2d-rust 0.1.0

Pure Rust port of the Box2D v3 2D physics engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Body creation and mass update from body.c.
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT

use super::{body_flags, create_island_for_body, make_body_id, Body, BodySim, BodyState};
use crate::constants::huge;
use crate::core::NULL_INDEX;
use crate::id::BodyId;
use crate::math_functions::{
    is_valid_float, is_valid_position, is_valid_rotation, is_valid_vec2,
    ROT_IDENTITY as ROT_IDENTITY_,
};
use crate::solver_set::{SolverSet, AWAKE_SET, DISABLED_SET, STATIC_SET};
use crate::types::BodyType;
use crate::world::World;

/// Create a rigid body given a definition. (b2CreateBody)
pub fn create_body(world: &mut World, def: &crate::types::BodyDef) -> BodyId {
    debug_assert!(def.internal_value == crate::core::SECRET_COOKIE);
    debug_assert!(is_valid_position(def.position));
    debug_assert!(is_valid_rotation(def.rotation));
    debug_assert!(is_valid_vec2(def.linear_velocity));
    debug_assert!(is_valid_float(def.angular_velocity));
    debug_assert!(is_valid_float(def.linear_damping) && def.linear_damping >= 0.0);
    debug_assert!(is_valid_float(def.angular_damping) && def.angular_damping >= 0.0);
    debug_assert!(is_valid_float(def.sleep_threshold) && def.sleep_threshold >= 0.0);
    debug_assert!(is_valid_float(def.gravity_scale));

    debug_assert!(!world.locked);
    if world.locked {
        return BodyId::default();
    }

    let is_awake = (def.is_awake || !def.enable_sleep) && def.is_enabled;

    // determine the solver set
    let set_id;
    if !def.is_enabled {
        // any body type can be disabled
        set_id = DISABLED_SET;
    } else if def.type_ == BodyType::Static {
        set_id = STATIC_SET;
    } else if is_awake {
        set_id = AWAKE_SET;
    } else {
        // new set for a sleeping body in its own island
        set_id = world.solver_set_id_pool.alloc_id();
        if set_id == world.solver_sets.len() as i32 {
            // Create a zero initialized solver set. All sub-arrays are also
            // zero initialized.
            world.solver_sets.push(SolverSet::default());
        } else {
            debug_assert!(world.solver_sets[set_id as usize].set_index == NULL_INDEX);
        }

        world.solver_sets[set_id as usize].set_index = set_id;
    }

    debug_assert!(0 <= set_id && set_id < world.solver_sets.len() as i32);

    let body_id = world.body_id_pool.alloc_id();

    let mut lock_flags = 0u32;
    lock_flags |= if def.motion_locks.linear_x {
        body_flags::LOCK_LINEAR_X
    } else {
        0
    };
    lock_flags |= if def.motion_locks.linear_y {
        body_flags::LOCK_LINEAR_Y
    } else {
        0
    };
    lock_flags |= if def.motion_locks.angular_z {
        body_flags::LOCK_ANGULAR_Z
    } else {
        0
    };

    let set = &mut world.solver_sets[set_id as usize];
    let mut body_sim = BodySim {
        transform: crate::math_functions::WorldTransform {
            p: def.position,
            q: def.rotation,
        },
        center: def.position,
        rotation0: def.rotation,
        center0: def.position,
        min_extent: huge(),
        max_extent: 0.0,
        linear_damping: def.linear_damping,
        angular_damping: def.angular_damping,
        gravity_scale: def.gravity_scale,
        body_id,
        flags: lock_flags,
        ..Default::default()
    };
    body_sim.flags |= if def.is_bullet {
        body_flags::IS_BULLET
    } else {
        0
    };
    body_sim.flags |= if def.allow_fast_rotation {
        body_flags::ALLOW_FAST_ROTATION
    } else {
        0
    };
    body_sim.flags |= if def.type_ == BodyType::Dynamic {
        body_flags::DYNAMIC_FLAG
    } else {
        0
    };
    body_sim.flags |= if def.enable_sleep {
        body_flags::ENABLE_SLEEP
    } else {
        0
    };
    body_sim.flags |= if def.enable_contact_recycling {
        body_flags::BODY_ENABLE_CONTACT_RECYCLING
    } else {
        0
    };
    let sim_flags = body_sim.flags;
    set.body_sims.push(body_sim);
    let local_index = set.body_sims.len() as i32 - 1;

    if set_id == AWAKE_SET {
        set.body_states.push(BodyState {
            linear_velocity: def.linear_velocity,
            angular_velocity: def.angular_velocity,
            delta_rotation: ROT_IDENTITY_,
            flags: sim_flags,
            ..Default::default()
        });
    }

    if body_id == world.bodies.len() as i32 {
        world.bodies.push(Body::default());
    } else {
        debug_assert!(world.bodies[body_id as usize].id == NULL_INDEX);
    }

    let body = &mut world.bodies[body_id as usize];

    // C: strncpy into char[B2_NAME_LENGTH + 1]; the Rust name is truncated to
    // the same limit.
    body.name = def
        .name
        .chars()
        .take(crate::constants::NAME_LENGTH as usize)
        .collect();

    body.user_data = def.user_data;
    body.set_index = set_id;
    body.local_index = local_index;
    body.generation += 1;
    body.head_shape_id = NULL_INDEX;
    body.shape_count = 0;
    body.head_chain_id = NULL_INDEX;
    body.head_contact_key = NULL_INDEX;
    body.contact_count = 0;
    body.head_joint_key = NULL_INDEX;
    body.joint_count = 0;
    body.island_id = NULL_INDEX;
    body.island_index = NULL_INDEX;
    body.body_move_index = NULL_INDEX;
    body.id = body_id;
    body.mass = 0.0;
    body.inertia = 0.0;
    body.sleep_threshold = def.sleep_threshold;
    body.sleep_time = 0.0;
    body.type_ = def.type_;
    body.flags = sim_flags;

    // dynamic and kinematic bodies that are enabled need an island
    if set_id >= AWAKE_SET {
        create_island_for_body(world, set_id, body_id);
    }

    world.validate_solver_sets();

    make_body_id(world, body_id)
}

/// Update a body's mass, center of mass, rotational inertia, and extents from
/// its shapes. (b2UpdateBodyMassData)
pub fn update_body_mass_data(world: &mut World, body_id: i32) {
    use crate::collision::MassData;
    use crate::math_functions::{
        add, cross_sv, dot, max_float, min_float, mul_add, mul_sv, sub, sub_pos,
        transform_world_point, VEC2_ZERO,
    };
    use crate::shape::{compute_shape_extent, compute_shape_mass};

    let (set_index, local_index, body_type, head_shape_id, shape_count) = {
        let body = &mut world.bodies[body_id as usize];
        // Mass is no longer dirty
        body.flags &= !body_flags::DIRTY_MASS;
        // Compute mass data from shapes. Each shape has its own density.
        body.mass = 0.0;
        body.inertia = 0.0;
        (
            body.set_index,
            body.local_index,
            body.type_,
            body.head_shape_id,
            body.shape_count,
        )
    };

    {
        let sim = &mut world.solver_sets[set_index as usize].body_sims[local_index as usize];
        sim.inv_mass = 0.0;
        sim.inv_inertia = 0.0;
        sim.local_center = VEC2_ZERO;
        sim.min_extent = huge();
        sim.max_extent = 0.0;
    }

    // Static and kinematic sims have zero mass.
    if body_type != BodyType::Dynamic {
        {
            let sim = &mut world.solver_sets[set_index as usize].body_sims[local_index as usize];
            sim.center = sim.transform.p;
            sim.center0 = sim.center;
        }

        // Need extents for kinematic bodies for sleeping to work correctly.
        if body_type == BodyType::Kinematic {
            let mut shape_id = head_shape_id;
            while shape_id != NULL_INDEX {
                let extent = {
                    let s = &world.shapes[shape_id as usize];
                    let e = compute_shape_extent(s, VEC2_ZERO);
                    shape_id = s.next_shape_id;
                    e
                };
                let sim =
                    &mut world.solver_sets[set_index as usize].body_sims[local_index as usize];
                sim.min_extent = min_float(sim.min_extent, extent.min_extent);
                sim.max_extent = max_float(sim.max_extent, extent.max_extent);
            }
        }

        return;
    }

    // C uses arena scratch (b2StackAlloc); a Vec is the Rust equivalent.
    let mut masses: Vec<MassData> = Vec::with_capacity(shape_count as usize);

    // Accumulate mass over all shapes.
    let mut local_center = VEC2_ZERO;
    let mut shape_id = head_shape_id;
    while shape_id != NULL_INDEX {
        let (mass_data, next) = {
            let s = &world.shapes[shape_id as usize];
            let next = s.next_shape_id;
            if s.density == 0.0 {
                (MassData::default(), next)
            } else {
                (compute_shape_mass(s), next)
            }
        };
        shape_id = next;

        if mass_data.mass != 0.0 {
            world.bodies[body_id as usize].mass += mass_data.mass;
            local_center = mul_add(local_center, mass_data.mass, mass_data.center);
        }
        masses.push(mass_data);
    }

    // Compute center of mass.
    let body_mass = world.bodies[body_id as usize].mass;
    if body_mass > 0.0 {
        let inv_mass = 1.0 / body_mass;
        world.solver_sets[set_index as usize].body_sims[local_index as usize].inv_mass = inv_mass;
        local_center = mul_sv(inv_mass, local_center);
    }

    // Second loop to accumulate the rotational inertia about the center of mass
    for mass_data in &masses {
        if mass_data.mass == 0.0 {
            continue;
        }

        // Shift to center of mass. This is safe because it can only increase.
        let offset = sub(local_center, mass_data.center);
        let inertia = mass_data.rotational_inertia + mass_data.mass * dot(offset, offset);
        world.bodies[body_id as usize].inertia += inertia;
    }

    debug_assert!(world.bodies[body_id as usize].inertia >= 0.0);

    let inertia = world.bodies[body_id as usize].inertia;
    if inertia > 0.0 {
        world.solver_sets[set_index as usize].body_sims[local_index as usize].inv_inertia =
            1.0 / inertia;
    } else {
        world.bodies[body_id as usize].inertia = 0.0;
        world.solver_sets[set_index as usize].body_sims[local_index as usize].inv_inertia = 0.0;
    }

    // Move center of mass.
    let old_center = {
        let sim = &mut world.solver_sets[set_index as usize].body_sims[local_index as usize];
        let old = sim.center;
        sim.local_center = local_center;
        sim.center = transform_world_point(sim.transform, sim.local_center);
        sim.center0 = sim.center;
        old
    };

    // Update center of mass velocity
    if set_index == AWAKE_SET {
        let new_center =
            world.solver_sets[set_index as usize].body_sims[local_index as usize].center;
        let state = &mut world.solver_sets[AWAKE_SET as usize].body_states[local_index as usize];
        let delta_linear = cross_sv(state.angular_velocity, sub_pos(new_center, old_center));
        state.linear_velocity = add(state.linear_velocity, delta_linear);
    }

    // Compute body extents relative to center of mass
    let mut shape_id = head_shape_id;
    while shape_id != NULL_INDEX {
        let extent = {
            let s = &world.shapes[shape_id as usize];
            let e = compute_shape_extent(s, local_center);
            shape_id = s.next_shape_id;
            e
        };
        let sim = &mut world.solver_sets[set_index as usize].body_sims[local_index as usize];
        sim.min_extent = min_float(sim.min_extent, extent.min_extent);
        sim.max_extent = max_float(sim.max_extent, extent.max_extent);
    }
}

/// Destroy the attached contacts. (static b2DestroyBodyContacts)
pub(crate) fn destroy_body_contacts(world: &mut World, body_id: i32, wake_bodies: bool) {
    let mut edge_key = world.bodies[body_id as usize].head_contact_key;
    while edge_key != NULL_INDEX {
        let contact_id = edge_key >> 1;
        let edge_index = edge_key & 1;

        edge_key = world.contacts[contact_id as usize].edges[edge_index as usize].next_key;
        crate::contact::destroy_contact(world, contact_id, wake_bodies);
    }

    world.validate_solver_sets();
}

/// Destroy a rigid body and everything attached to it: joints, contacts,
/// shapes, and chains. (b2DestroyBody)
pub fn destroy_body(world: &mut World, body_id: BodyId) {
    use super::{remove_body_from_island, remove_body_sim};
    use crate::body::get_body_full_id;
    use crate::solver_set::FIRST_SLEEPING_SET;

    let body_index = get_body_full_id(world, body_id);

    // Wake bodies attached to this body, even if this body is static.
    let wake_bodies = true;

    // Destroy the attached joints
    let mut edge_key = world.bodies[body_index as usize].head_joint_key;
    while edge_key != NULL_INDEX {
        let joint_id = edge_key >> 1;
        let edge_index = edge_key & 1;

        edge_key = world.joints[joint_id as usize].edges[edge_index as usize].next_key;

        // Careful because this modifies the list being traversed
        crate::joint::destroy_joint_internal(world, joint_id, wake_bodies);
    }

    // Destroy all contacts attached to this body.
    destroy_body_contacts(world, body_index, wake_bodies);

    // Destroy the attached shapes and their broad-phase proxies.
    let mut shape_id = world.bodies[body_index as usize].head_shape_id;
    while shape_id != NULL_INDEX {
        if world.shapes[shape_id as usize].sensor_index != NULL_INDEX {
            crate::sensor::destroy_sensor(world, shape_id);
        }

        {
            let (shapes, broad_phase) = (&mut world.shapes, &mut world.broad_phase);
            crate::shape::destroy_shape_proxy(&mut shapes[shape_id as usize], broad_phase);
        }

        // Return shape to free list.
        world.shape_id_pool.free_id(shape_id);
        world.shapes[shape_id as usize].id = NULL_INDEX;

        shape_id = world.shapes[shape_id as usize].next_shape_id;
    }

    // Destroy the attached chains. The associated shapes have already been
    // destroyed above.
    let mut chain_id = world.bodies[body_index as usize].head_chain_id;
    while chain_id != NULL_INDEX {
        // Free the chain data. (b2FreeChainData)
        {
            let chain = &mut world.chain_shapes[chain_id as usize];
            chain.shape_indices = Vec::new();
            chain.materials = Vec::new();
        }

        // Return chain to free list.
        world.chain_id_pool.free_id(chain_id);
        world.chain_shapes[chain_id as usize].id = NULL_INDEX;

        chain_id = world.chain_shapes[chain_id as usize].next_chain_id;
    }

    remove_body_from_island(world, body_index);

    // Remove body sim from solver set that owns it
    let (set_index, local_index) = {
        let body = &world.bodies[body_index as usize];
        (body.set_index, body.local_index)
    };
    remove_body_sim(
        &mut world.solver_sets[set_index as usize].body_sims,
        &mut world.bodies,
        local_index,
    );

    // Remove body state from awake set
    if set_index == AWAKE_SET {
        world.solver_sets[set_index as usize]
            .body_states
            .swap_remove(local_index as usize);
    } else if set_index >= FIRST_SLEEPING_SET
        && world.solver_sets[set_index as usize].body_sims.is_empty()
    {
        // Remove solver set if it is empty
        crate::solver_set::destroy_solver_set(world, set_index);
    }

    // Free body and id (preserve body generation)
    let raw_id = world.bodies[body_index as usize].id;
    world.body_id_pool.free_id(raw_id);

    let body = &mut world.bodies[body_index as usize];
    body.set_index = NULL_INDEX;
    body.local_index = NULL_INDEX;
    body.id = NULL_INDEX;

    world.validate_solver_sets();
}