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