Skip to main content

crcbl_phys/
lib.rs

1//! Physics: broadphase queries, forces, continuous collision detection.
2//!
3//! This crate provides the engine's physics pillar: collider shapes, bounding
4//! volumes, broadphase spatial queries (BVH), continuous collision detection
5//! (swept sphere and swept capsule TOI), and the capsule character controller
6//! built on them.
7//!
8//! # Layered architecture
9//!
10//! | Layer | Contents | Status |
11//! | ----- | -------- | ------ |
12//! | **L0** | Queries + kinematics: ray/segment/sweep/overlap, trigger volumes, character controller | Current |
13//! | **L1** | Forces + ballistics + orbits: gravity, drag, thrust, integrators, Kepler propagation | Current |
14//! | **CCD** | Swept collision: TOI, motion-inflated broadphase | Current |
15//! | **L2** | Contact solver: sequential impulses, warm starting, islands, sleep, sweeps | Rungs 1–4 |
16//! | **L3** | Joints in the same solver: distance, revolute, prismatic, weld, spherical | Rung 5 |
17//!
18//! L1 today is the force pipeline and one integrator: [`GravityForce`],
19//! [`DragForce`], [`DampingForce`] and [`ThrustForce`] feed
20//! [`SemiImplicitEuler`] through [`ForceProvider`]. The integrator turns bodies
21//! as well as moving them — torque, an inertia tensor from [`MassProperties`],
22//! the gyroscopic term by the implicit midpoint rule, and the quaternion turned
23//! to match — which is contact-solver rung 0 (`docs/notes/simulation.md`). This
24//! crate's `clippy.toml` refuses the platform's transcendental functions: the
25//! sine and cosine it constructs are `crcbl_core::trig`'s, and the three
26//! platform calls still standing — `AtmosphericDrag`'s exponential, the sphere
27//! of influence's power and the Kepler solution's Stumpff functions — each say
28//! why where they are made.
29//! [`SurfaceMaterial`] carries each body's friction and restitution.
30//!
31//! L2 is contact-solver rungs 1 to 3, in [`contact`]: a
32//! system made with [`PhysicsSystem::with_contacts`] collides spheres,
33//! capsules and boxes through split broadphase trees, analytic manifolds, a
34//! cached separating axis test with clipping for box pairs, and a substepped
35//! soft solver with warm starting by feature id, centroid and twist friction,
36//! speculative contacts and a restitution pass, and raises a
37//! [`KineticContact`] for each hard impact. Still bodies gather into
38//! persistent islands that sleep, costing nothing until something wakes them.
39//! [`Joint`]s are rung 5: constraints in the same solver, with limits,
40//! motors and breaking, and a body can ask for more substeps for its group
41//! with [`PhysicsSystem::set_substeps`]. Convex hulls are a later rung.
42//!
43//! [`Atmosphere`] and its
44//! quadratic [`AtmosphericDrag`] have landed, the [`Frames`] hierarchy carries
45//! sphere-of-influence crossings, and [`propagate`] is the analytic Kepler
46//! solution a coasting body is put on rails with.
47//!
48//! L0's kinematics side is [`CharacterController`]: a capsule that walks,
49//! slides, climbs steps and stays on its ground, moved by
50//! [`PhysicsWorld::sweep_capsule`] and dug out by
51//! [`PhysicsWorld::capsule_penetrations_into`]. It takes a world-space
52//! displacement and knows nothing about any camera, which is what lets one
53//! controller serve a first-person and a third-person game.
54//!
55//! [`TriangleMesh`] is level geometry — floors, ramps, stairs — for static and
56//! kinematic bodies: the contact solver collides it triangle by triangle, one
57//! side of each, with Jolt's active edges so a body slides across a seam
58//! without catching, and [`PhysicsWorld::add_mesh`] puts it in the query world,
59//! whose rays, sweeps and overlaps hit its triangles exactly.
60//!
61//! [`AabbCompound`] is L0's query surface for a rigid body made of several
62//! boxes: a ray cast and a closest-point query against local-space parts at a
63//! [`Transform`], naming the part each answer came from. [`CompoundShape`] is
64//! the same kind of body to the contact solver: boxes fixed in its frame, each
65//! colliding on its own, with mass and inertia summed from the parts.
66//!
67//! All spatial types use `f64` for determinism. Downcasting to `f32` happens
68//! only at the render boundary via `crcbl_core::WorldPos::relative_to`.
69//!
70//! The design's rules are in `docs/notes/simulation.md`, under "What the deleted
71//! 05-physics plan left behind"; what is still owed is in `docs/backlog.md`.
72
73pub mod atmosphere;
74pub mod broadphase;
75pub mod character;
76pub mod collider;
77pub mod components;
78pub mod compound;
79pub mod compound_shape;
80pub mod contact;
81pub mod dispersion;
82pub mod flight;
83pub mod forces;
84pub mod frames;
85pub mod impulse;
86pub mod integrator;
87pub mod joint;
88pub mod mass;
89pub mod material;
90pub mod mesh;
91pub mod orbit;
92pub mod query;
93pub mod system;
94pub mod wind;
95pub mod world;
96
97pub use atmosphere::{Atmosphere, AtmosphericDrag};
98pub use broadphase::{Bvh, BvhHit, Ray, Segment};
99pub use character::{
100    CharacterConfig, CharacterController, GroundContact, GroundProbe, LyingMoveOutcome,
101    LyingTurnOutcome, MoveOutcome, SlideContact,
102};
103pub use collider::{Aabb, BoxCollider, Capsule, LyingCapsule, Sphere};
104pub use components::{ColliderComponent, RigidBody, Transform};
105pub use compound::{AabbCompound, CompoundError, CompoundHit, CompoundPoint};
106pub use compound_shape::{CompoundBody, CompoundPart, CompoundShape};
107pub use contact::{
108    ContactBody, ContactCounters, ContactReport, ContactSettings, KineticContact, KineticSource,
109    PlaneId, StageTimes,
110};
111pub use forces::{DampingForce, DragForce, ForceProvider, GravityForce, PointGravity, ThrustForce};
112pub use frames::{FrameId, Frames, State, sphere_of_influence};
113pub use impulse::ImpulseError;
114pub use integrator::{
115    GYROSCOPIC_ITERATIONS, Integrator, MAX_ROTATION_LENGTH_ERROR, SemiImplicitEuler, SpinStep,
116    cayley_rotation, gyroscopic_step, integrate_rotation, rotation_from_scaled_axis,
117};
118pub use joint::{
119    DistanceJoint, Joint, JointBreak, JointDrift, JointError, JointId, JointKind, PrismaticJoint,
120    RevoluteJoint, SphericalJoint, Spring, WeldJoint,
121};
122pub use mass::MassProperties;
123pub use material::{CombineRule, ContactMaterial, SurfaceMaterial};
124pub use mesh::{MeshError, MeshHit, TriangleMesh};
125pub use orbit::{Orbit, propagate};
126pub use query::{
127    ARC_MAX_ITERATIONS, ARC_TIME_TOLERANCE, AcceleratedPath, ArcHit, OverlapHit, Penetration,
128    ShapeHit, aabb_overlaps_box, capsule_penetration_vs_aabb, capsule_penetration_vs_box,
129    capsule_penetration_vs_capsule, capsule_penetration_vs_sphere, ray_vs_aabb, ray_vs_box,
130    ray_vs_capsule, ray_vs_sphere, sphere_overlap_vs_aabb, sphere_overlap_vs_box,
131    sphere_overlap_vs_capsule, sphere_overlap_vs_sphere, sphere_overlaps_aabb, sphere_overlaps_box,
132    sphere_overlaps_capsule, sphere_overlaps_sphere, swept_capsule_vs_aabb, swept_capsule_vs_box,
133    swept_capsule_vs_capsule, swept_capsule_vs_sphere, swept_sphere_vs_aabb, swept_sphere_vs_box,
134    swept_sphere_vs_capsule, swept_sphere_vs_sphere,
135};
136pub use system::{
137    CONTACT_NORMAL_LENGTH, CollisionFilter, ContactPairError, EntityOverlapQueries,
138    PhysicsDebugLine, PhysicsSystem,
139};
140pub use wind::WindQuery;
141pub use world::{
142    ALL_LAYERS, BroadphaseStats, ColliderId, LayerMatch, OverlapQueries, PhysicsWorld, QueryFilter,
143    QueryScratch,
144};