1use crate::bitset::BitSet;
25use crate::body::Body;
26use crate::broad_phase::BroadPhase;
27use crate::constraint_graph::ConstraintGraph;
28use crate::contact::Contact;
29use crate::events::{
30 BodyMoveEvent, ContactBeginTouchEvent, ContactEndTouchEvent, ContactHitEvent, JointEvent,
31 SensorBeginTouchEvent, SensorEndTouchEvent,
32};
33use crate::id::ShapeId;
34use crate::id_pool::IdPool;
35use crate::island::Island;
36use crate::joint::Joint;
37use crate::math_functions::{Pos, Vec2};
38use crate::sensor::{Sensor, SensorHit, SensorTaskContext};
39use crate::shape::{ChainShape, Shape};
40use crate::solver_set::SolverSet;
41use crate::types::{Capacity, FrictionCallback, RestitutionCallback};
42
43pub type CustomFilterFcn = fn(ShapeId, ShapeId, u64) -> bool;
47
48pub type PreSolveFcn = fn(ShapeId, ShapeId, Pos, Vec2, u64) -> bool;
52
53#[derive(Debug, Clone, Copy, PartialEq, Default)]
55pub struct Profile {
56 pub step: f32,
57 pub pairs: f32,
58 pub collide: f32,
59 pub solve: f32,
60 pub solver_setup: f32,
61 pub constraints: f32,
62 pub prepare_constraints: f32,
63 pub integrate_velocities: f32,
64 pub warm_start: f32,
65 pub solve_impulses: f32,
66 pub integrate_positions: f32,
67 pub relax_impulses: f32,
68 pub apply_restitution: f32,
69 pub store_impulses: f32,
70 pub split_islands: f32,
71 pub transforms: f32,
72 pub sensor_hits: f32,
73 pub joint_events: f32,
74 pub hit_events: f32,
75 pub refit: f32,
76 pub bullets: f32,
77 pub sleep_islands: f32,
78 pub sensors: f32,
79}
80
81#[derive(Debug, Clone, Default)]
83pub struct TaskContext {
84 pub sensor_hits: Vec<SensorHit>,
86
87 pub contact_state_bit_set: BitSet,
90
91 pub hit_event_bit_set: BitSet,
93
94 pub has_hit_events: bool,
97
98 pub joint_state_bit_set: BitSet,
101
102 pub enlarged_sim_bit_set: BitSet,
105
106 pub awake_island_bit_set: BitSet,
108
109 pub split_sleep_time: f32,
111 pub split_island_id: i32,
112
113 pub recycled_contact_count: i32,
115}
116
117#[derive(Debug)]
120pub struct World {
121 pub broad_phase: BroadPhase,
122 pub constraint_graph: ConstraintGraph,
123
124 pub body_id_pool: IdPool,
127
128 pub bodies: Vec<Body>,
130
131 pub solver_set_id_pool: IdPool,
133
134 pub solver_sets: Vec<SolverSet>,
137
138 pub joint_id_pool: IdPool,
140
141 pub joints: Vec<Joint>,
144
145 pub contact_id_pool: IdPool,
147
148 pub contacts: Vec<Contact>,
151
152 pub island_id_pool: IdPool,
154
155 pub islands: Vec<Island>,
157
158 pub shape_id_pool: IdPool,
159 pub chain_id_pool: IdPool,
160
161 pub shapes: Vec<Shape>,
163 pub chain_shapes: Vec<ChainShape>,
164
165 pub sensors: Vec<Sensor>,
167
168 pub task_contexts: Vec<TaskContext>,
170 pub sensor_task_contexts: Vec<SensorTaskContext>,
171
172 pub body_move_events: Vec<BodyMoveEvent>,
173 pub sensor_begin_events: Vec<SensorBeginTouchEvent>,
174 pub contact_begin_events: Vec<ContactBeginTouchEvent>,
175
176 pub sensor_end_events: [Vec<SensorEndTouchEvent>; 2],
179 pub contact_end_events: [Vec<ContactEndTouchEvent>; 2],
180 pub end_event_array_index: i32,
181
182 pub contact_hit_events: Vec<ContactHitEvent>,
183 pub joint_events: Vec<JointEvent>,
184
185 pub debug_body_set: BitSet,
187 pub debug_joint_set: BitSet,
188 pub debug_contact_set: BitSet,
189 pub debug_island_set: BitSet,
190
191 pub step_index: u64,
193
194 pub split_island_id: i32,
196
197 pub gravity: Vec2,
198 pub hit_event_threshold: f32,
199 pub restitution_threshold: f32,
200 pub max_linear_speed: f32,
201 pub contact_speed: f32,
202 pub contact_hertz: f32,
203 pub contact_damping_ratio: f32,
204 pub contact_recycle_distance: f32,
205
206 pub friction_callback: Option<FrictionCallback>,
207 pub restitution_callback: Option<RestitutionCallback>,
208
209 pub generation: u16,
210
211 pub profile: Profile,
212
213 pub max_capacity: Capacity,
214
215 pub pre_solve_fcn: Option<PreSolveFcn>,
216 pub pre_solve_context: u64,
217
218 pub custom_filter_fcn: Option<CustomFilterFcn>,
219 pub custom_filter_context: u64,
220
221 pub worker_count: i32,
222
223 pub user_data: u64,
224
225 pub inv_h: f32,
227
228 pub inv_dt: f32,
230
231 pub world_id: u16,
232
233 pub enable_sleep: bool,
234 pub locked: bool,
235 pub enable_warm_starting: bool,
236 pub enable_contact_softening: bool,
237 pub enable_continuous: bool,
238 pub enable_speculative: bool,
239 pub in_use: bool,
240}
241
242pub fn default_friction_callback(
245 friction_a: f32,
246 _material_a: u64,
247 friction_b: f32,
248 _material_b: u64,
249) -> f32 {
250 (friction_a * friction_b).sqrt()
251}
252
253pub fn default_restitution_callback(
256 restitution_a: f32,
257 _material_a: u64,
258 restitution_b: f32,
259 _material_b: u64,
260) -> f32 {
261 crate::math_functions::max_float(restitution_a, restitution_b)
262}
263
264impl World {
265 pub fn new(def: &crate::types::WorldDef) -> World {
273 use crate::constants::contact_recycle_distance;
274 use crate::constraint_graph::ConstraintGraph;
275 use crate::math_functions::max_int;
276
277 debug_assert!(def.internal_value == crate::core::SECRET_COOKIE);
278
279 let body_capacity = max_int(
280 16,
281 def.capacity.static_body_count + def.capacity.dynamic_body_count,
282 ) as usize;
283 let shape_capacity = max_int(
284 16,
285 def.capacity.static_shape_count + def.capacity.dynamic_shape_count,
286 ) as usize;
287 let contact_capacity = max_int(16, def.capacity.contact_count) as usize;
288
289 let mut solver_set_id_pool = IdPool::new();
290 let mut solver_sets: Vec<SolverSet> = Vec::with_capacity(8);
291
292 let mut set = SolverSet {
295 set_index: solver_set_id_pool.alloc_id(),
296 ..Default::default()
297 };
298 set.body_sims
299 .reserve(max_int(16, def.capacity.static_body_count) as usize);
300 solver_sets.push(set);
301 debug_assert!(
302 solver_sets[crate::solver_set::STATIC_SET as usize].set_index
303 == crate::solver_set::STATIC_SET
304 );
305
306 solver_sets.push(SolverSet {
308 set_index: solver_set_id_pool.alloc_id(),
309 ..Default::default()
310 });
311 debug_assert!(
312 solver_sets[crate::solver_set::DISABLED_SET as usize].set_index
313 == crate::solver_set::DISABLED_SET
314 );
315
316 let mut awake = SolverSet {
318 set_index: solver_set_id_pool.alloc_id(),
319 ..Default::default()
320 };
321 awake
322 .body_sims
323 .reserve(max_int(16, def.capacity.dynamic_body_count) as usize);
324 awake
325 .body_states
326 .reserve(max_int(16, def.capacity.dynamic_body_count) as usize);
327 awake.contact_sims.reserve(contact_capacity);
328 solver_sets.push(awake);
329 debug_assert!(
330 solver_sets[crate::solver_set::AWAKE_SET as usize].set_index
331 == crate::solver_set::AWAKE_SET
332 );
333
334 World {
335 broad_phase: BroadPhase::new(&def.capacity),
336 constraint_graph: ConstraintGraph::new(&def.capacity),
337 body_id_pool: IdPool::new(),
338 bodies: Vec::with_capacity(body_capacity),
339 solver_set_id_pool,
340 solver_sets,
341 joint_id_pool: IdPool::new(),
342 joints: Vec::with_capacity(16),
343 contact_id_pool: IdPool::new(),
344 contacts: Vec::with_capacity(contact_capacity),
345 island_id_pool: IdPool::new(),
346 islands: Vec::with_capacity(max_int(16, def.capacity.dynamic_body_count) as usize),
347 shape_id_pool: IdPool::new(),
348 chain_id_pool: IdPool::new(),
349 shapes: Vec::with_capacity(shape_capacity),
350 chain_shapes: Vec::with_capacity(4),
351 sensors: Vec::with_capacity(4),
352 task_contexts: vec![TaskContext::default()],
354 sensor_task_contexts: vec![SensorTaskContext::default()],
355 body_move_events: Vec::with_capacity(4),
356 sensor_begin_events: Vec::with_capacity(4),
357 contact_begin_events: Vec::with_capacity(4),
358 sensor_end_events: [Vec::with_capacity(4), Vec::with_capacity(4)],
359 contact_end_events: [Vec::with_capacity(4), Vec::with_capacity(4)],
360 end_event_array_index: 0,
361 contact_hit_events: Vec::with_capacity(4),
362 joint_events: Vec::with_capacity(4),
363 debug_body_set: BitSet::new(256),
364 debug_joint_set: BitSet::new(256),
365 debug_contact_set: BitSet::new(256),
366 debug_island_set: BitSet::new(256),
367 step_index: 0,
368 split_island_id: crate::core::NULL_INDEX,
369 gravity: def.gravity,
370 hit_event_threshold: def.hit_event_threshold,
371 restitution_threshold: def.restitution_threshold,
372 max_linear_speed: def.maximum_linear_speed,
373 contact_speed: def.contact_speed,
374 contact_hertz: def.contact_hertz,
375 contact_damping_ratio: def.contact_damping_ratio,
376 contact_recycle_distance: contact_recycle_distance(),
377 friction_callback: Some(def.friction_callback.unwrap_or(default_friction_callback)),
378 restitution_callback: Some(
379 def.restitution_callback
380 .unwrap_or(default_restitution_callback),
381 ),
382 generation: 0,
383 profile: Profile::default(),
384 max_capacity: def.capacity,
385 pre_solve_fcn: None,
386 pre_solve_context: 0,
387 custom_filter_fcn: None,
388 custom_filter_context: 0,
389 worker_count: 1,
390 user_data: def.user_data,
391 inv_h: 0.0,
392 inv_dt: 0.0,
393 world_id: 0,
394 enable_sleep: def.enable_sleep,
395 locked: false,
396 enable_warm_starting: true,
397 enable_contact_softening: def.enable_contact_softening,
398 enable_continuous: def.enable_continuous,
399 enable_speculative: true,
400 in_use: true,
401 }
402 }
403
404 pub fn validate_solver_sets(&self) {
411 use crate::core::NULL_INDEX;
412
413 let mut active_body_count = 0;
414 for (set_index, set) in self.solver_sets.iter().enumerate() {
415 if set.set_index == NULL_INDEX {
416 debug_assert!(set.body_sims.is_empty());
418 debug_assert!(set.body_states.is_empty());
419 debug_assert!(set.island_sims.is_empty());
420 continue;
421 }
422
423 debug_assert!(set.set_index == set_index as i32);
424
425 if set_index == crate::solver_set::AWAKE_SET as usize {
426 debug_assert!(set.body_sims.len() == set.body_states.len());
427 } else {
428 debug_assert!(set.body_states.is_empty());
429 }
430
431 for (local_index, sim) in set.body_sims.iter().enumerate() {
432 let body = &self.bodies[sim.body_id as usize];
433 debug_assert!(body.set_index == set_index as i32);
434 debug_assert!(body.local_index == local_index as i32);
435 debug_assert!(body.id == sim.body_id);
436 let _ = (body, local_index);
437 }
438 active_body_count += set.body_sims.len() as i32;
439
440 for (local_index, island_sim) in set.island_sims.iter().enumerate() {
441 let island = &self.islands[island_sim.island_id as usize];
442 debug_assert!(island.set_index == set_index as i32);
443 debug_assert!(island.local_index == local_index as i32);
444 let _ = (island, local_index);
445 }
446 }
447
448 debug_assert!(active_body_count == self.body_id_pool.id_count());
449 let _ = active_body_count;
450 }
451}
452
453mod step;
454
455pub use step::*;