Skip to main content

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 restitution (bounciness) coefficient in the range 0.0 to 1.0.
166    #[get(type(copy))]
167    pub(crate) restitution: f64,
168    /// The friction coefficient for surface contact.
169    #[get(type(copy))]
170    pub(crate) friction: f64,
171    /// How this body participates in the simulation.
172    #[get(type(copy))]
173    pub(crate) body_type: BodyType,
174    /// The 3D collider shape attached to this body.
175    #[get(type(copy))]
176    #[new(skip)]
177    pub(crate) collider: Option<BodyCollider3D>,
178}
179
180/// The 3D physics world managing all rigid bodies and simulation steps.
181#[derive(Clone, Data, Debug, New, PartialEq)]
182pub struct PhysicsWorld3D {
183    /// All rigid bodies in the world.
184    #[get(pub(crate))]
185    #[get_mut(pub(crate))]
186    #[set(pub(crate))]
187    #[new(skip)]
188    pub(crate) bodies: Vec<RigidBody3D>,
189    /// The simulation configuration.
190    #[get(type(copy))]
191    pub(crate) config: PhysicsConfig3D,
192    /// The persistent broad-phase grid, cleared and re-inserted each step so its
193    /// `HashMap` allocation is reused instead of rebuilt per step.
194    #[get_mut(pub(crate))]
195    #[set(pub(crate))]
196    #[new(skip)]
197    pub(crate) grid: SpatialHashGrid3D,
198    /// Scratch buffer reused by grid queries to avoid a per-query `Vec` allocation.
199    #[get_mut(pub(crate))]
200    #[new(skip)]
201    pub(crate) query_buffer: Vec<usize>,
202    /// Scratch `HashSet` reused by grid queries for candidate dedup.
203    #[get_mut(pub(crate))]
204    #[new(skip)]
205    pub(crate) query_seen: HashSet<usize>,
206    /// Reusable broad-phase candidate pair list, rebuilt once per step and
207    /// iterated by every solver iteration (the grid is unchanged between them).
208    #[get_mut(pub(crate))]
209    #[new(skip)]
210    pub(crate) pair_buffer: Vec<(usize, usize)>,
211}