euv_engine/physics/struct.rs
1use super::*;
2
3/// Configuration parameters for the physics world simulation.
4#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
5pub struct PhysicsConfig {
6 /// The gravitational acceleration vector in pixels per second squared.
7 #[get(type(copy))]
8 pub(crate) gravity: Vector2D,
9 /// The linear velocity damping coefficient applied per second.
10 #[get(type(copy))]
11 pub(crate) linear_damping: f64,
12 /// The angular velocity damping coefficient applied per second.
13 #[get(type(copy))]
14 pub(crate) angular_damping: f64,
15}
16
17/// A 2D rigid body participating in the physics simulation.
18#[derive(Clone, Data, Debug, New, PartialEq)]
19pub struct RigidBody2D {
20 /// The unique identifier of this body, typically matching a game object ID.
21 #[get(type(copy))]
22 pub(crate) id: u64,
23 /// The world-space position of the body's center.
24 #[get(type(copy))]
25 #[get_mut(pub(crate))]
26 pub(crate) position: Vector2D,
27 /// The linear velocity in pixels per second.
28 #[get(type(copy))]
29 #[get_mut(pub(crate))]
30 #[new(skip)]
31 pub(crate) velocity: Vector2D,
32 /// The accumulated force to be applied during the next physics step.
33 #[get(pub(crate), type(copy))]
34 #[get_mut(pub(crate))]
35 #[set(pub(crate))]
36 #[new(skip)]
37 pub(crate) force_accumulator: Vector2D,
38 /// The rotation angle in radians.
39 #[get(type(copy))]
40 #[get_mut(pub(crate))]
41 #[new(skip)]
42 pub(crate) rotation: f64,
43 /// The angular velocity in radians per second.
44 #[get(type(copy))]
45 #[get_mut(pub(crate))]
46 #[new(skip)]
47 pub(crate) angular_velocity: f64,
48 /// The mass of the body in kilograms. Static bodies have a mass of 0.
49 #[get(type(copy))]
50 #[get_mut(pub(crate))]
51 pub(crate) mass: f64,
52 /// The precomputed inverse mass (1/mass). Static bodies have 0 inverse mass.
53 #[get(pub(crate), type(copy))]
54 #[get_mut(pub(crate))]
55 #[set(pub(crate))]
56 pub(crate) inverse_mass: f64,
57 /// The restitution (bounciness) coefficient in the range 0.0 to 1.0.
58 #[get(type(copy))]
59 pub(crate) restitution: f64,
60 /// The friction coefficient for surface contact.
61 #[get(type(copy))]
62 pub(crate) friction: f64,
63 /// How this body participates in the simulation.
64 #[get(type(copy))]
65 pub(crate) body_type: BodyType,
66 /// The collider shape attached to this body.
67 #[get(type(copy))]
68 #[new(skip)]
69 pub(crate) collider: Option<BodyCollider>,
70}
71
72/// The physics world managing all rigid bodies and simulation steps.
73#[derive(Clone, Data, Debug, New, PartialEq)]
74pub struct PhysicsWorld2D {
75 /// All rigid bodies in the world.
76 #[get(pub(crate))]
77 #[get_mut(pub(crate))]
78 #[set(pub(crate))]
79 #[new(skip)]
80 pub(crate) bodies: Vec<RigidBody2D>,
81 /// The simulation configuration.
82 #[get(type(copy))]
83 pub(crate) config: PhysicsConfig,
84 /// The persistent broad-phase grid, cleared and re-inserted each step so its
85 /// `HashMap` allocation is reused instead of rebuilt per step.
86 #[get_mut(pub(crate))]
87 #[set(pub(crate))]
88 #[new(skip)]
89 pub(crate) grid: SpatialHashGrid2D,
90 /// Scratch buffer reused by grid queries to avoid a per-query `Vec` allocation.
91 #[get_mut(pub(crate))]
92 #[new(skip)]
93 pub(crate) query_buffer: Vec<usize>,
94 /// Scratch `HashSet` reused by grid queries for candidate dedup.
95 #[get_mut(pub(crate))]
96 #[new(skip)]
97 pub(crate) query_seen: HashSet<usize>,
98 /// Reusable broad-phase candidate pair list, rebuilt once per step and
99 /// iterated by every solver iteration (the grid is unchanged between them).
100 #[get_mut(pub(crate))]
101 #[new(skip)]
102 pub(crate) pair_buffer: Vec<(usize, usize)>,
103}
104
105/// Configuration parameters for the 3D physics world simulation.
106#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
107pub struct PhysicsConfig3D {
108 /// The gravitational acceleration vector in meters per second squared.
109 #[get(type(copy))]
110 pub(crate) gravity: Vector3D,
111 /// The linear velocity damping coefficient applied per second.
112 #[get(type(copy))]
113 pub(crate) linear_damping: f64,
114 /// The angular velocity damping coefficient applied per second.
115 #[get(type(copy))]
116 pub(crate) angular_damping: f64,
117}
118
119/// A 3D rigid body participating in the physics simulation.
120#[derive(Clone, Data, Debug, New, PartialEq)]
121pub struct RigidBody3D {
122 /// The unique identifier of this body, typically matching a game object ID.
123 #[get(type(copy))]
124 pub(crate) id: u64,
125 /// The world-space position of the body's center.
126 #[get(type(copy))]
127 #[get_mut(pub(crate))]
128 pub(crate) position: Vector3D,
129 /// The linear velocity in meters per second.
130 #[get(type(copy))]
131 #[get_mut(pub(crate))]
132 #[new(skip)]
133 pub(crate) velocity: Vector3D,
134 /// The accumulated force to be applied during the next physics step.
135 #[get(pub(crate), type(copy))]
136 #[get_mut(pub(crate))]
137 #[set(pub(crate))]
138 #[new(skip)]
139 pub(crate) force_accumulator: Vector3D,
140 /// The rotation as a quaternion.
141 #[get(type(copy))]
142 #[get_mut(pub(crate))]
143 #[new(skip)]
144 pub(crate) rotation: Quaternion,
145 /// The angular velocity as a 3D vector (axis * speed).
146 #[get(type(copy))]
147 #[get_mut(pub(crate))]
148 #[new(skip)]
149 pub(crate) angular_velocity: Vector3D,
150 /// The accumulated torque to be applied during the next physics step.
151 #[get(pub(crate), type(copy))]
152 #[get_mut(pub(crate))]
153 #[set(pub(crate))]
154 #[new(skip)]
155 pub(crate) torque_accumulator: Vector3D,
156 /// The mass of the body in kilograms. Static bodies have a mass of 0.
157 #[get(type(copy))]
158 #[get_mut(pub(crate))]
159 pub(crate) mass: f64,
160 /// The precomputed inverse mass (1/mass). Static bodies have 0 inverse mass.
161 #[get(pub(crate), type(copy))]
162 #[get_mut(pub(crate))]
163 #[set(pub(crate))]
164 pub(crate) inverse_mass: f64,
165 /// The precomputed inverse moment of inertia (scalar, axis-aligned). Static
166 /// bodies have 0 inverse inertia. Used to convert accumulated torque into
167 /// angular velocity each step.
168 #[get(pub(crate), type(copy))]
169 #[get_mut(pub(crate))]
170 #[set(pub(crate))]
171 #[new(skip)]
172 pub(crate) inverse_inertia: f64,
173 /// The restitution (bounciness) coefficient in the range 0.0 to 1.0.
174 #[get(type(copy))]
175 pub(crate) restitution: f64,
176 /// The friction coefficient for surface contact.
177 #[get(type(copy))]
178 pub(crate) friction: f64,
179 /// How this body participates in the simulation.
180 #[get(type(copy))]
181 pub(crate) body_type: BodyType,
182 /// The 3D collider shape attached to this body.
183 #[get(type(copy))]
184 #[new(skip)]
185 pub(crate) collider: Option<BodyCollider3D>,
186}
187
188/// The 3D physics world managing all rigid bodies and simulation steps.
189#[derive(Clone, Data, Debug, New, PartialEq)]
190pub struct PhysicsWorld3D {
191 /// All rigid bodies in the world.
192 #[get(pub(crate))]
193 #[get_mut(pub(crate))]
194 #[set(pub(crate))]
195 #[new(skip)]
196 pub(crate) bodies: Vec<RigidBody3D>,
197 /// The simulation configuration.
198 #[get(type(copy))]
199 pub(crate) config: PhysicsConfig3D,
200 /// The persistent broad-phase grid, cleared and re-inserted each step so its
201 /// `HashMap` allocation is reused instead of rebuilt per step.
202 #[get_mut(pub(crate))]
203 #[set(pub(crate))]
204 #[new(skip)]
205 pub(crate) grid: SpatialHashGrid3D,
206 /// Scratch buffer reused by grid queries to avoid a per-query `Vec` allocation.
207 #[get_mut(pub(crate))]
208 #[new(skip)]
209 pub(crate) query_buffer: Vec<usize>,
210 /// Scratch `HashSet` reused by grid queries for candidate dedup.
211 #[get_mut(pub(crate))]
212 #[new(skip)]
213 pub(crate) query_seen: HashSet<usize>,
214 /// Reusable broad-phase candidate pair list, rebuilt once per step and
215 /// iterated by every solver iteration (the grid is unchanged between them).
216 #[get_mut(pub(crate))]
217 #[new(skip)]
218 pub(crate) pair_buffer: Vec<(usize, usize)>,
219 /// OPT 33: reusable scratch vector for per-body AABB3D snapshots taken
220 /// before draining the spatial grid. The 3D `resolve_collisions` path
221 /// needs to materialise bbox bounds into a Vec to satisfy the
222 /// immutable-then-mutable borrow split; persisting the buffer avoids a
223 /// per-step `Vec::with_capacity(bodies.len())` allocation.
224 #[get_mut(pub(crate))]
225 #[new(skip)]
226 pub(crate) bbox_buffer: Vec<(usize, AABB3D)>,
227}