Skip to main content

euv_core/game/engine3d/physics/
struct.rs

1use super::*;
2
3/// Dynamic or static 3D rigid body with axis-aligned box collider and physical restitution/friction.
4#[derive(Clone, Copy, Data, Debug, PartialEq)]
5pub struct RigidBody3D {
6    /// Center position in world meters.
7    #[get(type(copy))]
8    pub(crate) position: Vec3,
9    /// Linear velocity in meters per second.
10    #[get(type(copy))]
11    pub(crate) velocity: Vec3,
12    /// Angular velocity in radians per second around `(X, Y, Z)` axes.
13    #[get(type(copy))]
14    pub(crate) angular_velocity: Vec3,
15    /// Axis-aligned box collider half-extents `(hx, hy, hz)`.
16    #[get(type(copy))]
17    pub(crate) half_extents: Vec3,
18    /// Inverse mass (`0.0` = immovable static collider).
19    #[get(type(copy))]
20    pub(crate) inv_mass: f32,
21    /// Coefficient of restitution (`0.0` = inelastic, `1.0` = perfectly bouncy).
22    #[get(type(copy))]
23    pub(crate) restitution: f32,
24    /// Coulomb surface friction coefficient (`0.0` = frictionless, `1.0` = high grip).
25    #[get(type(copy))]
26    pub(crate) friction: f32,
27    /// Linear velocity damping factor per second (`0.0..=1.0`).
28    #[get(type(copy))]
29    pub(crate) linear_damping: f32,
30    /// Accumulated low-motion rest duration in seconds used for Chaos-style sleep deactivation.
31    #[get(type(copy))]
32    pub(crate) sleep_timer: f32,
33    /// Whether this body is currently asleep and skipped by integration and narrowphase solvers.
34    #[get(type(copy))]
35    pub(crate) sleeping: bool,
36    /// Whether this body is currently resting on the ground or another solid surface.
37    #[get(type(copy))]
38    pub(crate) grounded: bool,
39}
40
41/// Result of a 3D physics raycast against `PhysicsWorld3D` rigid bodies.
42#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
43pub struct RayHitPhysics3D {
44    /// Index of the hit `RigidBody3D`.
45    #[get(type(copy))]
46    pub(crate) body_index: usize,
47    /// Hit distance along the query ray in meters.
48    #[get(type(copy))]
49    pub(crate) distance: f32,
50    /// World-space contact point.
51    #[get(type(copy))]
52    pub(crate) point: Vec3,
53}
54
55/// Deterministic 3D rigid-body physics world with gravity, ground plane, spatial-hash broadphase, impulse resolution, and raycasting.
56#[derive(Clone, Data, Debug)]
57pub struct PhysicsWorld3D {
58    /// Uniform gravitational acceleration vector (defaults to `(0.0, -9.81, 0.0)`).
59    #[get(type(copy))]
60    pub(crate) gravity: Vec3,
61    /// Horizontal ground plane height in meters (`y = ground_y`).
62    #[get(type(copy))]
63    pub(crate) ground_y: f32,
64    /// Simulated 3D rigid bodies.
65    pub(crate) bodies: Vec<RigidBody3D>,
66    /// Reusable fixed-bucket spatial hash heads (`u32::MAX` = empty bucket) for O(N) 3D broadphase.
67    #[get(skip)]
68    #[get_mut(skip)]
69    #[set(skip)]
70    pub(crate) hash_heads: Vec<u32>,
71    /// Reusable linked-list next indices for entries inserted into `hash_heads`.
72    #[get(skip)]
73    #[get_mut(skip)]
74    #[set(skip)]
75    pub(crate) hash_next: Vec<u32>,
76    /// Reusable body index payload per hash-grid entry.
77    #[get(skip)]
78    #[get_mut(skip)]
79    #[set(skip)]
80    pub(crate) hash_body: Vec<u32>,
81    /// Indices of buckets touched during the current step so clearing touches only occupied slots.
82    #[get(skip)]
83    #[get_mut(skip)]
84    #[set(skip)]
85    pub(crate) touched_buckets: Vec<usize>,
86    /// Reusable deduplicated candidate pair buffer `(i, j)` with `i < j`.
87    #[get(skip)]
88    #[get_mut(skip)]
89    #[set(skip)]
90    pub(crate) pair_scratch: Vec<(usize, usize)>,
91}