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/// Reusable O(N) spatial-hash broad-phase accelerator and 2D impulse physics solver.
89#[derive(Clone, Data, Debug)]
90pub struct PhysicsWorld2D {
91    /// Active simulation parameters.
92    #[get(type(copy))]
93    pub(crate) config: PhysicsConfig,
94    /// Cell-indexed spatial hash buckets reused across steps.
95    #[get(pub(crate))]
96    #[get_mut(pub(crate))]
97    #[set(pub(crate))]
98    pub(crate) grid: HashMap<(i32, i32), Vec<usize>>,
99    /// Deduplication set for candidate broad-phase index pairs.
100    #[get(pub(crate))]
101    #[get_mut(pub(crate))]
102    #[set(pub(crate))]
103    pub(crate) seen_pairs: HashSet<(usize, usize)>,
104    /// Most recent step telemetry.
105    #[get(type(copy))]
106    pub(crate) last_stats: PhysicsStepStats,
107}