|
| 1 | +//! Rapier-backed 2D physics backend. |
| 2 | +//! |
| 3 | +//! This module provides a minimal wrapper around `rapier2d` to support the |
| 4 | +//! higher-level `lambda-rs` physics APIs without exposing vendor types outside |
| 5 | +//! of the platform layer. |
| 6 | +
|
| 7 | +use rapier2d::prelude::*; |
| 8 | + |
| 9 | +/// A 2D physics backend powered by `rapier2d`. |
| 10 | +/// |
| 11 | +/// This type is an internal implementation detail used by `lambda-rs`. |
| 12 | +pub struct PhysicsBackend2D { |
| 13 | + gravity: Vector, |
| 14 | + integration_parameters: IntegrationParameters, |
| 15 | + islands: IslandManager, |
| 16 | + broad_phase: BroadPhaseBvh, |
| 17 | + narrow_phase: NarrowPhase, |
| 18 | + bodies: RigidBodySet, |
| 19 | + colliders: ColliderSet, |
| 20 | + impulse_joints: ImpulseJointSet, |
| 21 | + multibody_joints: MultibodyJointSet, |
| 22 | + ccd_solver: CCDSolver, |
| 23 | + pipeline: PhysicsPipeline, |
| 24 | +} |
| 25 | + |
| 26 | +impl PhysicsBackend2D { |
| 27 | + /// Creates a new empty 2D physics backend. |
| 28 | + /// |
| 29 | + /// # Arguments |
| 30 | + /// - `gravity`: The gravity vector in meters per second squared. |
| 31 | + /// - `timestep_seconds`: The fixed integration timestep in seconds. |
| 32 | + /// |
| 33 | + /// # Returns |
| 34 | + /// Returns a new `PhysicsBackend2D` with no bodies, colliders, or joints. |
| 35 | + pub fn new(gravity: [f32; 2], timestep_seconds: f32) -> Self { |
| 36 | + let gravity_vector = Vector::new(gravity[0], gravity[1]); |
| 37 | + |
| 38 | + let integration_parameters = IntegrationParameters { |
| 39 | + dt: timestep_seconds, |
| 40 | + ..Default::default() |
| 41 | + }; |
| 42 | + |
| 43 | + return Self { |
| 44 | + gravity: gravity_vector, |
| 45 | + integration_parameters, |
| 46 | + islands: IslandManager::new(), |
| 47 | + broad_phase: BroadPhaseBvh::new(), |
| 48 | + narrow_phase: NarrowPhase::new(), |
| 49 | + bodies: RigidBodySet::new(), |
| 50 | + colliders: ColliderSet::new(), |
| 51 | + impulse_joints: ImpulseJointSet::new(), |
| 52 | + multibody_joints: MultibodyJointSet::new(), |
| 53 | + ccd_solver: CCDSolver::new(), |
| 54 | + pipeline: PhysicsPipeline::new(), |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + /// Returns the gravity vector used by this backend. |
| 59 | + /// |
| 60 | + /// # Returns |
| 61 | + /// Returns the gravity vector in meters per second squared. |
| 62 | + pub fn gravity(&self) -> [f32; 2] { |
| 63 | + return [self.gravity.x, self.gravity.y]; |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns the fixed integration timestep in seconds. |
| 67 | + /// |
| 68 | + /// # Returns |
| 69 | + /// Returns the timestep used for each simulation step. |
| 70 | + pub fn timestep_seconds(&self) -> f32 { |
| 71 | + return self.integration_parameters.dt; |
| 72 | + } |
| 73 | + |
| 74 | + /// Advances the simulation by one fixed timestep. |
| 75 | + /// |
| 76 | + /// # Returns |
| 77 | + /// Returns `()` after applying integration and constraint solving for the |
| 78 | + /// configured timestep. |
| 79 | + pub fn step(&mut self) { |
| 80 | + return self.step_with_timestep_seconds(self.integration_parameters.dt); |
| 81 | + } |
| 82 | + |
| 83 | + /// Advances the simulation by the given timestep. |
| 84 | + /// |
| 85 | + /// # Arguments |
| 86 | + /// - `timestep_seconds`: The timestep used for this step. |
| 87 | + /// |
| 88 | + /// # Returns |
| 89 | + /// Returns `()` after applying integration and constraint solving. |
| 90 | + pub fn step_with_timestep_seconds(&mut self, timestep_seconds: f32) { |
| 91 | + self.integration_parameters.dt = timestep_seconds; |
| 92 | + |
| 93 | + self.pipeline.step( |
| 94 | + self.gravity, |
| 95 | + &self.integration_parameters, |
| 96 | + &mut self.islands, |
| 97 | + &mut self.broad_phase, |
| 98 | + &mut self.narrow_phase, |
| 99 | + &mut self.bodies, |
| 100 | + &mut self.colliders, |
| 101 | + &mut self.impulse_joints, |
| 102 | + &mut self.multibody_joints, |
| 103 | + &mut self.ccd_solver, |
| 104 | + &(), |
| 105 | + &(), |
| 106 | + ); |
| 107 | + |
| 108 | + return; |
| 109 | + } |
| 110 | +} |
0 commit comments