oxiphysics 0.1.1

Unified physics engine - Bullet/OpenFOAM/LAMMPS/CalculiX replacement
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Physics event publish/subscribe system.
//!
//! The `EventBus` collects physics events emitted during a simulation step
//! and dispatches them to registered listeners.  Events are queued during the
//! step and either flushed (dispatching to all listeners) or drained (moved
//! into a `Vec` for manual inspection).
//!
//! ## Event types
//!
//! | Variant                  | Fired when …                                    |
//! |--------------------------|-------------------------------------------------|
//! | `CollisionBegin`       | Two bodies start overlapping                    |
//! | `CollisionEnd`         | Two bodies separate                             |
//! | `BodySleep`            | A body transitions to the sleeping state        |
//! | `BodyWake`             | A sleeping body is woken                        |
//! | `ConstraintBreak`      | A breakable constraint reaches its force limit  |
//! | `BodyEnterRegion`      | A body enters a tagged sensor region            |
//! | `BodyLeaveRegion`      | A body leaves a tagged sensor region            |
//! | `StepCompleted`        | A simulation step has finished                  |
//! | `UserDefined`          | Application-defined custom event                |
//!
//!
//!
//!
//!
//!
//!
//!
//!
//!
//!
//! ## Example
//!
//! ```rust
//! use oxiphysics::event_bus::{EventBus, PhysicsEvent};
//!
//! let mut bus = EventBus::new();
//!
//! // Register a listener that counts collisions
//! let id = bus.subscribe(|ev| {
//!     if matches!(ev, PhysicsEvent::CollisionBegin { .. }) {
//!         // handle collision
//!     }
//! });
//!
//! // Emit some events (normally done by the pipeline)
//! bus.publish(PhysicsEvent::CollisionBegin {
//!     body_a: 0, body_b: 1,
//!     contact_normal: [0.0, 1.0, 0.0],
//!     penetration_depth: 0.01,
//! });
//! bus.publish(PhysicsEvent::StepCompleted { step: 1, sim_time: 1.0 / 60.0, dt: 1.0 / 60.0 });
//!
//! // Flush: deliver all queued events to listeners
//! bus.flush();
//!
//! // Or drain: take ownership without calling listeners
//! let events = bus.drain();
//! assert!(events.is_empty()); // already flushed above
//!
//! bus.unsubscribe(id);
//! ```

#![allow(missing_docs)]
#![allow(dead_code)]

// ============================================================================
// PhysicsEvent
// ============================================================================

/// Contact information bundled with a [`CollisionBegin`] event.
///
/// [`CollisionBegin`]: PhysicsEvent::CollisionBegin
#[derive(Debug, Clone, PartialEq)]
pub struct ContactInfo {
    /// Index of the first colliding body.
    pub body_a: usize,
    /// Index of the second colliding body.
    pub body_b: usize,
    /// World-space contact normal pointing from A toward B.
    pub contact_normal: [f64; 3],
    /// Penetration depth at the contact point \[m\].
    pub penetration_depth: f64,
    /// World-space contact point.
    pub contact_point: [f64; 3],
}

/// All events that the physics engine may emit during a simulation step.
#[derive(Debug, Clone, PartialEq)]
pub enum PhysicsEvent {
    /// Two bodies have begun overlapping.
    CollisionBegin {
        body_a: usize,
        body_b: usize,
        contact_normal: [f64; 3],
        penetration_depth: f64,
    },

    /// Two previously overlapping bodies have separated.
    CollisionEnd { body_a: usize, body_b: usize },

    /// A dynamic body has entered the sleeping state.
    BodySleep { body: usize },

    /// A sleeping body has been woken (by collision, force, or API call).
    BodyWake { body: usize },

    /// A breakable constraint has exceeded its maximum force and been removed.
    ConstraintBreak {
        /// User-assigned constraint identifier.
        constraint_id: usize,
        /// The accumulated impulse magnitude that triggered the break \[N·s\].
        accumulated_impulse: f64,
    },

    /// A body has entered a named sensor region.
    BodyEnterRegion {
        body: usize,
        /// User-assigned region identifier.
        region_id: usize,
    },

    /// A body has left a named sensor region.
    BodyLeaveRegion { body: usize, region_id: usize },

    /// A simulation step has completed.
    StepCompleted {
        /// Step counter (0-based).
        step: u64,
        /// Accumulated simulation time at end of this step.
        sim_time: f64,
        /// Time step duration used.
        dt: f64,
    },

    /// Application-defined event with arbitrary payload.
    UserDefined {
        /// Application-assigned event type ID.
        id: u32,
        /// Arbitrary binary payload.
        data: Vec<u8>,
    },
}

impl PhysicsEvent {
    /// Returns a short human-readable name for this event type.
    pub fn kind_name(&self) -> &'static str {
        match self {
            PhysicsEvent::CollisionBegin { .. } => "CollisionBegin",
            PhysicsEvent::CollisionEnd { .. } => "CollisionEnd",
            PhysicsEvent::BodySleep { .. } => "BodySleep",
            PhysicsEvent::BodyWake { .. } => "BodyWake",
            PhysicsEvent::ConstraintBreak { .. } => "ConstraintBreak",
            PhysicsEvent::BodyEnterRegion { .. } => "BodyEnterRegion",
            PhysicsEvent::BodyLeaveRegion { .. } => "BodyLeaveRegion",
            PhysicsEvent::StepCompleted { .. } => "StepCompleted",
            PhysicsEvent::UserDefined { .. } => "UserDefined",
        }
    }

    /// Returns `true` if this is a collision event (begin or end).
    pub fn is_collision(&self) -> bool {
        matches!(
            self,
            PhysicsEvent::CollisionBegin { .. } | PhysicsEvent::CollisionEnd { .. }
        )
    }
}

impl std::fmt::Display for PhysicsEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PhysicsEvent::CollisionBegin {
                body_a,
                body_b,
                penetration_depth,
                ..
            } => write!(
                f,
                "CollisionBegin({body_a}{body_b}, depth={penetration_depth:.4}m)"
            ),
            PhysicsEvent::CollisionEnd { body_a, body_b } => {
                write!(f, "CollisionEnd({body_a}{body_b})")
            }
            PhysicsEvent::BodySleep { body } => write!(f, "BodySleep(body={body})"),
            PhysicsEvent::BodyWake { body } => write!(f, "BodyWake(body={body})"),
            PhysicsEvent::ConstraintBreak {
                constraint_id,
                accumulated_impulse,
            } => write!(
                f,
                "ConstraintBreak(id={constraint_id}, impulse={accumulated_impulse:.2}N·s)"
            ),
            PhysicsEvent::BodyEnterRegion { body, region_id } => {
                write!(f, "BodyEnterRegion(body={body}, region={region_id})")
            }
            PhysicsEvent::BodyLeaveRegion { body, region_id } => {
                write!(f, "BodyLeaveRegion(body={body}, region={region_id})")
            }
            PhysicsEvent::StepCompleted { step, sim_time, dt } => write!(
                f,
                "StepCompleted(step={step}, t={sim_time:.4}s, dt={dt:.6}s)"
            ),
            PhysicsEvent::UserDefined { id, data } => {
                write!(f, "UserDefined(id={id}, {n}B)", n = data.len())
            }
        }
    }
}

// ============================================================================
// ListenerId
// ============================================================================

/// Opaque handle returned by [`EventBus::subscribe`] for later unsubscription.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ListenerId(u64);

// ============================================================================
// EventBus
// ============================================================================

/// A boxed listener closure stored in the event bus.
type ListenerFn = Box<dyn Fn(&PhysicsEvent) + Send + 'static>;

/// A lightweight publish/subscribe event bus for physics events.
///
/// Events are published to an internal queue with [`publish`][EventBus::publish].
/// They are delivered to listeners either:
/// - On [`flush`][EventBus::flush] — calls every listener with each event.
/// - On [`drain`][EventBus::drain] — moves the events into the caller's `Vec`
///   without invoking any listeners.
///
/// Listeners are closures stored as `Box<dyn Fn(&PhysicsEvent)>` and are called
/// in registration order.
pub struct EventBus {
    /// Pending events not yet flushed.
    queue: Vec<PhysicsEvent>,
    /// Registered listener closures with their IDs.
    listeners: Vec<(ListenerId, ListenerFn)>,
    /// Monotonically increasing ID counter.
    next_id: u64,
    /// Total number of events published since creation.
    published_count: u64,
    /// Total number of listener invocations since creation.
    dispatch_count: u64,
}

impl Default for EventBus {
    fn default() -> Self {
        Self::new()
    }
}

impl EventBus {
    /// Create a new, empty event bus.
    pub fn new() -> Self {
        Self {
            queue: Vec::new(),
            listeners: Vec::new(),
            next_id: 0,
            published_count: 0,
            dispatch_count: 0,
        }
    }

    /// Publish an event to the queue.
    ///
    /// The event is not dispatched immediately — call [`flush`][Self::flush]
    /// or [`drain`][Self::drain] to process it.
    pub fn publish(&mut self, event: PhysicsEvent) {
        self.published_count += 1;
        self.queue.push(event);
    }

    /// Publish multiple events at once.
    pub fn publish_all(&mut self, events: impl IntoIterator<Item = PhysicsEvent>) {
        for e in events {
            self.publish(e);
        }
    }

    /// Register a listener closure.
    ///
    /// Returns a [`ListenerId`] that can be passed to [`unsubscribe`][Self::unsubscribe]
    /// to remove the listener.
    pub fn subscribe(&mut self, f: impl Fn(&PhysicsEvent) + Send + 'static) -> ListenerId {
        let id = ListenerId(self.next_id);
        self.next_id += 1;
        self.listeners.push((id, Box::new(f)));
        id
    }

    /// Register a listener that only receives events matching a predicate.
    pub fn subscribe_filtered(
        &mut self,
        predicate: impl Fn(&PhysicsEvent) -> bool + Send + 'static,
        handler: impl Fn(&PhysicsEvent) + Send + 'static,
    ) -> ListenerId {
        self.subscribe(move |ev| {
            if predicate(ev) {
                handler(ev);
            }
        })
    }

    /// Unsubscribe a listener by its ID.
    ///
    /// Returns `true` if the listener was found and removed.
    pub fn unsubscribe(&mut self, id: ListenerId) -> bool {
        if let Some(pos) = self.listeners.iter().position(|(lid, _)| *lid == id) {
            let _ = self.listeners.remove(pos);
            true
        } else {
            false
        }
    }

    /// Dispatch all queued events to every registered listener, then clear the queue.
    ///
    /// Listeners are called in registration order for each event in queue order.
    pub fn flush(&mut self) {
        for event in &self.queue {
            for (_, listener) in &self.listeners {
                listener(event);
                self.dispatch_count += 1;
            }
        }
        self.queue.clear();
    }

    /// Move all queued events out of the bus without calling listeners.
    ///
    /// Returns the events in FIFO order.  The queue is cleared after this call.
    pub fn drain(&mut self) -> Vec<PhysicsEvent> {
        std::mem::take(&mut self.queue)
    }

    /// Peek at the current queue without consuming or dispatching it.
    pub fn pending(&self) -> &[PhysicsEvent] {
        &self.queue
    }

    /// Number of events currently in the queue.
    pub fn pending_count(&self) -> usize {
        self.queue.len()
    }

    /// Clear the queue without dispatching or returning events.
    pub fn clear(&mut self) {
        self.queue.clear();
    }

    /// Number of currently registered listeners.
    pub fn listener_count(&self) -> usize {
        self.listeners.len()
    }

    /// Total events published since the bus was created.
    pub fn total_published(&self) -> u64 {
        self.published_count
    }

    /// Total listener invocations since the bus was created.
    pub fn total_dispatched(&self) -> u64 {
        self.dispatch_count
    }

    /// Count of events in the queue that satisfy `predicate`.
    pub fn count_pending<F>(&self, predicate: F) -> usize
    where
        F: Fn(&PhysicsEvent) -> bool,
    {
        self.queue.iter().filter(|e| predicate(e)).count()
    }
}

impl std::fmt::Debug for EventBus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EventBus")
            .field("pending_count", &self.queue.len())
            .field("listener_count", &self.listeners.len())
            .field("published_count", &self.published_count)
            .field("dispatch_count", &self.dispatch_count)
            .finish()
    }
}

// ============================================================================
// Convenience: filter helpers
// ============================================================================

/// A helper that collects all [`CollisionBegin`] events into a `Vec`.
///
/// [`CollisionBegin`]: PhysicsEvent::CollisionBegin
pub fn collect_collision_begins(events: &[PhysicsEvent]) -> Vec<(usize, usize)> {
    events
        .iter()
        .filter_map(|e| {
            if let PhysicsEvent::CollisionBegin { body_a, body_b, .. } = e {
                Some((*body_a, *body_b))
            } else {
                None
            }
        })
        .collect()
}

/// A helper that extracts all bodies that entered or left any region in this batch.
pub fn region_crossing_events(events: &[PhysicsEvent]) -> Vec<(usize, usize, bool)> {
    // returns (body, region, is_enter)
    events
        .iter()
        .filter_map(|e| match e {
            PhysicsEvent::BodyEnterRegion { body, region_id } => Some((*body, *region_id, true)),
            PhysicsEvent::BodyLeaveRegion { body, region_id } => Some((*body, *region_id, false)),
            _ => None,
        })
        .collect()
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    #[test]
    fn publish_and_drain() {
        let mut bus = EventBus::new();
        bus.publish(PhysicsEvent::BodySleep { body: 0 });
        bus.publish(PhysicsEvent::BodyWake { body: 1 });
        assert_eq!(bus.pending_count(), 2);
        let events = bus.drain();
        assert_eq!(events.len(), 2);
        assert_eq!(bus.pending_count(), 0);
    }

    #[test]
    fn flush_calls_listeners() {
        let mut bus = EventBus::new();
        let counter = Arc::new(Mutex::new(0u32));
        let c = Arc::clone(&counter);
        bus.subscribe(move |_| {
            *c.lock().unwrap_or_else(|e| e.into_inner()) += 1;
        });

        bus.publish(PhysicsEvent::BodySleep { body: 0 });
        bus.publish(PhysicsEvent::BodySleep { body: 1 });
        bus.flush();

        assert_eq!(*counter.lock().unwrap_or_else(|e| e.into_inner()), 2);
        assert_eq!(bus.pending_count(), 0);
    }

    #[test]
    fn unsubscribe_stops_delivery() {
        let mut bus = EventBus::new();
        let counter = Arc::new(Mutex::new(0u32));
        let c = Arc::clone(&counter);
        let id = bus.subscribe(move |_| {
            *c.lock().unwrap_or_else(|e| e.into_inner()) += 1;
        });
        bus.publish(PhysicsEvent::BodySleep { body: 0 });
        bus.flush();
        assert_eq!(*counter.lock().unwrap_or_else(|e| e.into_inner()), 1);

        bus.unsubscribe(id);
        bus.publish(PhysicsEvent::BodySleep { body: 0 });
        bus.flush();
        assert_eq!(*counter.lock().unwrap_or_else(|e| e.into_inner()), 1); // unchanged
    }

    #[test]
    fn filtered_subscription() {
        let mut bus = EventBus::new();
        let hits = Arc::new(Mutex::new(Vec::new()));
        let h = Arc::clone(&hits);
        bus.subscribe_filtered(
            |ev| matches!(ev, PhysicsEvent::CollisionBegin { .. }),
            move |ev| {
                h.lock()
                    .unwrap_or_else(|e| e.into_inner())
                    .push(ev.kind_name());
            },
        );
        bus.publish(PhysicsEvent::CollisionBegin {
            body_a: 0,
            body_b: 1,
            contact_normal: [0.0, 1.0, 0.0],
            penetration_depth: 0.1,
        });
        bus.publish(PhysicsEvent::BodySleep { body: 0 });
        bus.flush();
        let hits = hits.lock().unwrap_or_else(|e| e.into_inner());
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0], "CollisionBegin");
    }

    #[test]
    fn multiple_listeners_order() {
        let mut bus = EventBus::new();
        let log = Arc::new(Mutex::new(Vec::new()));
        for i in 0..3u32 {
            let l = Arc::clone(&log);
            bus.subscribe(move |_| {
                l.lock().unwrap_or_else(|e| e.into_inner()).push(i);
            });
        }
        bus.publish(PhysicsEvent::BodyWake { body: 0 });
        bus.flush();
        assert_eq!(
            *log.lock().unwrap_or_else(|e| e.into_inner()),
            vec![0, 1, 2]
        );
    }

    #[test]
    fn clear_discards_queue() {
        let mut bus = EventBus::new();
        bus.publish(PhysicsEvent::BodySleep { body: 0 });
        bus.clear();
        assert_eq!(bus.pending_count(), 0);
    }

    #[test]
    fn event_display() {
        let ev = PhysicsEvent::CollisionBegin {
            body_a: 3,
            body_b: 7,
            contact_normal: [0.0, 1.0, 0.0],
            penetration_depth: 0.005,
        };
        let s = format!("{}", ev);
        assert!(s.contains("CollisionBegin"));
        assert!(s.contains('3'));
        assert!(s.contains('7'));
    }

    #[test]
    fn collect_collision_begins_helper() {
        let events = vec![
            PhysicsEvent::CollisionBegin {
                body_a: 0,
                body_b: 1,
                contact_normal: [0., 1., 0.],
                penetration_depth: 0.01,
            },
            PhysicsEvent::BodySleep { body: 2 },
            PhysicsEvent::CollisionBegin {
                body_a: 3,
                body_b: 4,
                contact_normal: [1., 0., 0.],
                penetration_depth: 0.02,
            },
        ];
        let pairs = collect_collision_begins(&events);
        assert_eq!(pairs, vec![(0, 1), (3, 4)]);
    }

    #[test]
    fn total_stats() {
        let mut bus = EventBus::new();
        bus.subscribe(|_| {});
        bus.publish(PhysicsEvent::BodySleep { body: 0 });
        bus.publish(PhysicsEvent::BodyWake { body: 0 });
        bus.flush();
        assert_eq!(bus.total_published(), 2);
        assert_eq!(bus.total_dispatched(), 2);
    }
}