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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#![deny(future_incompatible, nonstandard_style)]
#![warn(missing_docs, rust_2018_idioms, clippy::pedantic)]
#![allow(clippy::needless_pass_by_value)]
#![cfg(all(
any(feature = "2d", feature = "3d"),
not(all(feature = "2d", feature = "3d")),
))]
#[cfg(feature = "2d")]
pub extern crate rapier2d as rapier;
#[cfg(feature = "3d")]
pub extern crate rapier3d as rapier;
use bevy::{ecs::schedule::ShouldRun, prelude::*};
use heron_core::{CollisionEvent, PhysicsTime};
use crate::body::HandleMap;
use crate::rapier::dynamics::{
CCDSolver, IntegrationParameters, JointSet, RigidBodyHandle, RigidBodySet,
};
use crate::rapier::geometry::{BroadPhase, ColliderHandle, ColliderSet, NarrowPhase};
pub use crate::rapier::na as nalgebra;
use crate::rapier::pipeline::PhysicsPipeline;
mod acceleration;
mod body;
pub mod convert;
mod pipeline;
mod velocity;
#[allow(unused)]
mod stage {
pub(crate) const PRE_STEP: &str = "heron-pre-step";
pub(crate) const STEP: &str = "heron-step";
pub(crate) const POST_STEP: &str = "heron-post-step";
}
mod system_label {
pub(crate) const PHYSICS_STEP: &str = "physics_step";
}
#[must_use]
#[derive(Clone)]
pub struct RapierPlugin {
pub step_per_second: Option<f64>,
pub parameters: IntegrationParameters,
}
#[derive(Debug, Copy, Clone)]
pub struct BodyHandle {
rigid_body: RigidBodyHandle,
collider: ColliderHandle,
}
impl RapierPlugin {
pub fn from_steps_per_second(steps_per_second: u8) -> Self {
assert!(
steps_per_second > 0,
"Invalid number of step per second: {}",
steps_per_second
);
let parameters = IntegrationParameters {
dt: 1.0 / f32::from(steps_per_second),
..IntegrationParameters::default()
};
Self {
parameters,
step_per_second: Some(steps_per_second.into()),
}
}
}
impl Default for RapierPlugin {
fn default() -> Self {
Self::from(IntegrationParameters::default())
}
}
impl From<IntegrationParameters> for RapierPlugin {
fn from(parameters: IntegrationParameters) -> Self {
Self {
#[allow(clippy::cast_possible_truncation)]
step_per_second: Some(1.0 / f64::from(parameters.dt)),
parameters,
}
}
}
impl Plugin for RapierPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_plugin(heron_core::CorePlugin {
steps_per_second: self.step_per_second,
})
.init_resource::<PhysicsPipeline>()
.init_resource::<HandleMap>()
.add_event::<CollisionEvent>()
.insert_resource(self.parameters)
.insert_resource(BroadPhase::new())
.insert_resource(NarrowPhase::new())
.insert_resource(RigidBodySet::new())
.insert_resource(ColliderSet::new())
.insert_resource(JointSet::new())
.insert_resource(CCDSolver::new())
.stage(heron_core::stage::ROOT, |schedule: &mut Schedule| {
schedule
.add_stage(
"heron-remove-invalid-bodies",
SystemStage::single_threaded().with_system(body::remove_bodies.system()),
)
.add_stage(
"heron-pre-step",
SystemStage::single_threaded()
.with_system(
bevy::transform::transform_propagate_system::transform_propagate_system
.system(),
)
.with_system(body::remove.system())
.with_system(body::update_rapier_position.system())
.with_system(velocity::update_rapier_velocity.system())
.with_system(body::update_rapier_status.system())
.with_system(acceleration::update_rapier_force_and_torque.system())
.with_system(body::create.system()),
)
.add_stage(
"heron-step",
SystemStage::single_threaded()
.with_run_criteria(should_run.system())
.with_system(
velocity::apply_velocity_to_kinematic_bodies
.system()
.before(system_label::PHYSICS_STEP),
)
.with_system(pipeline::step.system().label(system_label::PHYSICS_STEP)),
)
.add_stage(
"heron-post-step",
SystemStage::parallel()
.with_run_criteria(should_run.system())
.with_system(body::update_bevy_transform.system())
.with_system(velocity::update_velocity_component.system()),
)
});
}
}
fn should_run(physics_time: Res<'_, PhysicsTime>) -> ShouldRun {
if physics_time.get_scale() == 0.0 {
ShouldRun::No
} else {
ShouldRun::Yes
}
}
impl BodyHandle {
#[must_use]
pub fn new(rigid_body: RigidBodyHandle, collider: ColliderHandle) -> BodyHandle {
BodyHandle {
rigid_body,
collider,
}
}
#[must_use]
pub fn rigid_body(&self) -> RigidBodyHandle {
self.rigid_body
}
#[must_use]
pub fn collider(&self) -> ColliderHandle {
self.collider
}
}