Skip to main content

euv_core/game/physics/
struct.rs

1use super::*;
2
3/// Global 2D rigid-body physics configuration.
4#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
5pub struct PhysicsConfig {
6    /// Global gravitational acceleration `(gx, gy)` in world units per second squared.
7    #[get(type(copy))]
8    pub(crate) gravity: Point,
9    /// Spatial-hash grid cell size for O(N) broad-phase collision pairing.
10    #[get(type(copy))]
11    pub(crate) cell_size: f32,
12    /// Baumgarte positional separation factor in `[0.0, 1.0]` preventing interpenetration.
13    #[get(type(copy))]
14    pub(crate) position_correction: f32,
15    /// Optional world arena containment rectangle bouncing dynamic bodies at borders.
16    #[get(skip)]
17    pub(crate) arena_bounds: Option<Rect>,
18}
19
20/// Contact event reported when two colliders overlap or collide during a physics step.
21#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
22pub struct CollisionEvent {
23    /// First colliding entity handle.
24    #[get(type(copy))]
25    pub(crate) entity_a: EntityId,
26    /// Second colliding entity handle.
27    #[get(type(copy))]
28    pub(crate) entity_b: EntityId,
29    /// Contact normal pointing from `entity_a` toward `entity_b`.
30    #[get(type(copy))]
31    pub(crate) normal: Point,
32    /// Penetration depth in world units.
33    #[get(type(copy))]
34    pub(crate) penetration: f32,
35    /// Whether either collider was marked as a trigger volume.
36    #[get(type(copy))]
37    pub(crate) is_trigger: bool,
38}
39
40/// Fluent query descriptor for 2D physics raycasting against `PhysicsWorld2D`.
41#[derive(Clone, Copy, Data, Debug, PartialEq)]
42pub struct RaycastQuery2D {
43    /// Ray origin in world units.
44    #[get(type(copy))]
45    pub(crate) origin: Point,
46    /// Ray direction vector (normalized automatically during cast).
47    #[get(type(copy))]
48    pub(crate) direction: Point,
49    /// Maximum ray travel distance in world units.
50    #[get(type(copy))]
51    pub(crate) max_distance: f32,
52    /// Collision layer filter bitmask (defaults to `u32::MAX`).
53    #[get(type(copy))]
54    pub(crate) layer_mask: u32,
55}
56
57/// Result of a 2D physics raycast intersection query.
58#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
59pub struct RaycastHit {
60    /// Hit entity handle.
61    #[get(type(copy))]
62    pub(crate) entity: EntityId,
63    /// World-space impact point.
64    #[get(type(copy))]
65    pub(crate) point: Point,
66    /// Surface normal at the hit point.
67    #[get(type(copy))]
68    pub(crate) normal: Point,
69    /// Distance along the ray from its origin.
70    #[get(type(copy))]
71    pub(crate) distance: f32,
72}
73
74/// Telemetry counters produced by a single `PhysicsWorld2D::step` invocation.
75#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
76pub struct PhysicsStepStats {
77    /// Number of bodies integrated during the step.
78    #[get(type(copy))]
79    pub(crate) bodies_integrated: usize,
80    /// Number of candidate broad-phase pairs inspected.
81    #[get(type(copy))]
82    pub(crate) broad_phase_pairs: usize,
83    /// Number of narrow-phase collisions or triggers detected.
84    #[get(type(copy))]
85    pub(crate) collisions_detected: usize,
86}
87
88/// Cached per-collider integer cell bounds and layer filters reused across broad-phase passes.
89#[derive(Clone, Copy, Debug, Default)]
90pub(crate) struct BodyGridBounds {
91    /// Arena index of the entity inside `GameWorld::entities`.
92    pub(crate) entity_idx: u32,
93    /// Inclusive minimum spatial-grid X cell.
94    pub(crate) min_cx: i32,
95    /// Inclusive maximum spatial-grid X cell.
96    pub(crate) max_cx: i32,
97    /// Inclusive minimum spatial-grid Y cell.
98    pub(crate) min_cy: i32,
99    /// Inclusive maximum spatial-grid Y cell.
100    pub(crate) max_cy: i32,
101    /// Collision layer bitmask of the collider.
102    pub(crate) layer: u32,
103    /// Collision target filter bitmask of the collider.
104    pub(crate) mask: u32,
105}
106
107/// Sparse open-addressed spatial-hash cell bucket used when world bounds exceed the dense flat grid limit.
108#[derive(Clone, Copy, Debug)]
109pub(crate) struct SparseGridCell {
110    /// Packed `(cx, cy)` integer grid key (`u64::MAX` when empty).
111    pub(crate) key: u64,
112    /// Cell X coordinate.
113    pub(crate) cx: i32,
114    /// Cell Y coordinate.
115    pub(crate) cy: i32,
116    /// Head index into `PhysicsWorld2D::sparse_nodes` (`u32::MAX` when empty).
117    pub(crate) head: u32,
118    /// Number of colliders stored in this cell's linked list.
119    pub(crate) count: u32,
120}
121
122/// Intrusive linked-list entry storing a `BodyGridBounds` slot index inside a sparse spatial-hash bucket.
123#[derive(Clone, Copy, Debug)]
124pub(crate) struct SparseGridNode {
125    /// Index into `PhysicsWorld2D::body_bounds`.
126    pub(crate) body_slot: u32,
127    /// Next entry index in `PhysicsWorld2D::sparse_nodes` (`u32::MAX` at tail).
128    pub(crate) next: u32,
129}
130
131/// Reusable zero-allocation flat spatial-grid broad-phase accelerator and 2D impulse physics solver.
132#[derive(Clone, Data, Debug)]
133pub struct PhysicsWorld2D {
134    /// Active simulation parameters.
135    #[get(type(copy))]
136    pub(crate) config: PhysicsConfig,
137    /// Reusable per-step collider cell bounds and layer masks.
138    #[get(skip)]
139    #[get_mut(skip)]
140    #[set(skip)]
141    pub(crate) body_bounds: Vec<BodyGridBounds>,
142    /// Dense counting-sort cell occupancy counts (`len == grid_w * grid_h`).
143    #[get(skip)]
144    #[get_mut(skip)]
145    #[set(skip)]
146    pub(crate) cell_counts: Vec<u32>,
147    /// Dense counting-sort cell start offsets (`len == grid_w * grid_h`).
148    #[get(skip)]
149    #[get_mut(skip)]
150    #[set(skip)]
151    pub(crate) cell_offsets: Vec<u32>,
152    /// Dense counting-sort per-cell write cursors (`len == grid_w * grid_h`).
153    #[get(skip)]
154    #[get_mut(skip)]
155    #[set(skip)]
156    pub(crate) cell_cursors: Vec<u32>,
157    /// Flat contiguous array of `body_bounds` indices grouped by cell.
158    #[get(skip)]
159    #[get_mut(skip)]
160    #[set(skip)]
161    pub(crate) cell_items: Vec<u32>,
162    /// Dense cell indices with at least 2 colliders (`(cell_idx, cx, cy)`).
163    #[get(skip)]
164    #[get_mut(skip)]
165    #[set(skip)]
166    pub(crate) active_cells: Vec<(usize, i32, i32)>,
167    /// Reusable sparse open-addressed cell table for ultra-wide worlds exceeding `FLAT_GRID_MAX_CELLS`.
168    #[get(skip)]
169    #[get_mut(skip)]
170    #[set(skip)]
171    pub(crate) sparse_table: Vec<SparseGridCell>,
172    /// Reusable sparse linked-list node storage.
173    #[get(skip)]
174    #[get_mut(skip)]
175    #[set(skip)]
176    pub(crate) sparse_nodes: Vec<SparseGridNode>,
177    /// Reusable list of occupied sparse table slots with `count >= 2`.
178    #[get(skip)]
179    #[get_mut(skip)]
180    #[set(skip)]
181    pub(crate) sparse_active: Vec<usize>,
182    /// Scratch bucket collecting body indices of one sparse cell in deterministic order.
183    #[get(skip)]
184    #[get_mut(skip)]
185    #[set(skip)]
186    pub(crate) sparse_bucket_scratch: Vec<u32>,
187    /// Persistent collision event output buffer reused across steps (zero allocation per fixed tick).
188    #[get(skip)]
189    #[get_mut(skip)]
190    #[set(skip)]
191    pub(crate) events_scratch: Vec<CollisionEvent>,
192    /// Indices of dense grid cells written during the previous step so only touched cells are cleared (no full-grid memset).
193    #[get(skip)]
194    #[get_mut(skip)]
195    #[set(skip)]
196    pub(crate) touched_dense_cells: Vec<usize>,
197    /// Indices of sparse hash table slots written during the previous step so only touched slots are cleared.
198    #[get(skip)]
199    #[get_mut(skip)]
200    #[set(skip)]
201    pub(crate) touched_sparse_slots: Vec<usize>,
202    /// Minimum X cell coordinate of the persistent dense spatial grid for DDA raycasting.
203    #[get(skip)]
204    #[get_mut(skip)]
205    #[set(skip)]
206    pub(crate) grid_min_cx: i32,
207    /// Minimum Y cell coordinate of the persistent dense spatial grid for DDA raycasting.
208    #[get(skip)]
209    #[get_mut(skip)]
210    #[set(skip)]
211    pub(crate) grid_min_cy: i32,
212    /// Width in cells of the persistent dense spatial grid for DDA raycasting.
213    #[get(skip)]
214    #[get_mut(skip)]
215    #[set(skip)]
216    pub(crate) grid_w: usize,
217    /// Height in cells of the persistent dense spatial grid for DDA raycasting.
218    #[get(skip)]
219    #[get_mut(skip)]
220    #[set(skip)]
221    pub(crate) grid_h: usize,
222    /// Cell edge size in world units of the persistent dense spatial grid.
223    #[get(skip)]
224    #[get_mut(skip)]
225    #[set(skip)]
226    pub(crate) grid_cell_size: f32,
227    /// Whether the dense spatial grid holds fresh broad-phase cell lists suitable for DDA raycasting.
228    #[get(skip)]
229    #[get_mut(skip)]
230    #[set(skip)]
231    pub(crate) grid_dense_valid: bool,
232    /// Most recent step telemetry.
233    #[get(type(copy))]
234    pub(crate) last_stats: PhysicsStepStats,
235}