ferravex_physics/
lib.rs

1//! Original rigid-body simulation for the recovered Ferravex games.
2//!
3//! Primitive parts have analytical mass and angular inertia.
4//! Bodies can own locally transformed primitive parts and fixed, hinge,
5//! distance or spring connections. Capsule/cylinder contact pairs remain limited
6//! to the documented primitive matrix.
7//! This is a small native solver, not a compatibility implementation of Rapier.
8pub mod assembly;
9mod body;
10pub mod character;
11mod geometry;
12mod joints;
13mod mass;
14mod material;
15pub mod radial;
16pub mod triggers;
17pub use joints::{
18    BreakLimits, Joint, JointBreakEvent, JointDesc, JointHandle, JointKind, JointLoad, JointMotor,
19    JointSet, JointValidationError, MAX_JOINTS,
20};
21pub use mass::{MassProperties, MassPropertyError, MassPropertyIssue};
22pub use material::{CombineError, CombineRule, MaterialError, PhysicsMaterial};
23mod world;
24pub use body::{
25    BodySet, BodyValidationError, Collider, ColliderHandle, ColliderSet, RigidBodyHandle,
26};
27use ferravex_math::{Quat, Vec2, Vec3};
28pub use world::{
29    BodySleepState, ContactEvent, ContactManifold, ContactPair, ContactPoint, ImpactRecord,
30    ImpactRecordingError, ImpactSettings, MAX_CHILDREN, MAX_COLLIDERS, MAX_IMPACT_RECORDS,
31    SleepStats, SolverSettings, StepStats,
32};
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35pub enum PhysicsError {
36    InvalidBody,
37    InvalidCollider,
38    InvalidMass,
39    InvalidHandle,
40    BodyCapacity,
41    ColliderCapacity,
42    ChildCapacity,
43    InvalidJoint,
44    JointCapacity,
45}
46impl std::fmt::Display for PhysicsError {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "rigid physics: {self:?}")
49    }
50}
51impl std::error::Error for PhysicsError {}
52
53/// A planar rotation, measured in radians.
54#[derive(Clone, Copy, Debug, Default, PartialEq)]
55pub struct Rotation2(f32);
56impl Rotation2 {
57    pub fn from_angle(angle: f32) -> Self {
58        assert!(angle.is_finite());
59        Self(angle)
60    }
61    pub fn angle(self) -> f32 {
62        self.0
63    }
64}
65impl std::ops::Mul<Vec2> for Rotation2 {
66    type Output = Vec2;
67    fn mul(self, p: Vec2) -> Vec2 {
68        let (s, c) = self.0.sin_cos();
69        Vec2::new(c * p.x - s * p.y, s * p.x + c * p.y)
70    }
71}
72
73#[cfg(feature = "dim2")]
74pub mod d2 {
75    use super::*;
76    pub type Vector = Vec2;
77    pub type Rotation = Rotation2;
78    pub type Angular = f32;
79    fn to3(v: Vector) -> Vec3 {
80        Vec3::new(v.x, v.y, 0.)
81    }
82    fn from3(v: Vec3) -> Vector {
83        Vec2::new(v.x, v.y)
84    }
85    fn angular_to3(v: Angular) -> Vec3 {
86        Vec3::Z * v
87    }
88    fn angular_from3(v: Vec3) -> Angular {
89        v.z
90    }
91    fn rotation(core: &body::Core) -> &Rotation {
92        &core.rotation2
93    }
94    fn rotation_to3(r: Rotation) -> Quat {
95        Quat::from_rotation_z(r.angle())
96    }
97    const PLANAR: bool = true;
98    include!("api.rs");
99    impl ColliderBuilder {
100        pub fn cuboid(x: f32, y: f32) -> Self {
101            Self::shape(body::Shape::Box(Vec3::new(x, y, 0.5)))
102        }
103        pub fn capsule_from_endpoints(a: Vector, b: Vector, radius: f32) -> Self {
104            Self::shape(body::Shape::Capsule {
105                a: to3(a),
106                b: to3(b),
107                radius,
108            })
109        }
110    }
111}
112#[cfg(feature = "dim3")]
113pub mod d3 {
114    use super::*;
115    pub type Vector = Vec3;
116    pub type Rotation = Quat;
117    pub type Angular = Vec3;
118    fn to3(v: Vector) -> Vec3 {
119        v
120    }
121    fn from3(v: Vec3) -> Vector {
122        v
123    }
124    fn angular_to3(v: Angular) -> Vec3 {
125        v
126    }
127    fn angular_from3(v: Vec3) -> Angular {
128        v
129    }
130    fn rotation(core: &body::Core) -> &Rotation {
131        &core.rotation
132    }
133    fn rotation_to3(r: Rotation) -> Quat {
134        r
135    }
136    const PLANAR: bool = false;
137    include!("api.rs");
138    impl JointBuilder {
139        pub fn axes(mut self, axis1: Vector, axis2: Vector) -> Self {
140            self.desc.kind = crate::JointKind::Hinge {
141                axis1: to3(axis1).normalize_or_zero(),
142                axis2: to3(axis2).normalize_or_zero(),
143            };
144            self
145        }
146    }
147    impl ColliderBuilder {
148        pub fn cuboid(x: f32, y: f32, z: f32) -> Self {
149            Self::shape(body::Shape::Box(Vec3::new(x, y, z)))
150        }
151        pub fn cylinder(half_height: f32, radius: f32) -> Self {
152            Self::shape(body::Shape::Cylinder {
153                half_height,
154                radius,
155            })
156        }
157    }
158}