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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#[cfg(feature = "dim2")]
use ncollide::shape::ConvexPolygon;
use ncollide::shape::{Ball, Capsule, Cuboid, HeightField, ShapeHandle};
use nphysics::force_generator::DefaultForceGeneratorSet;
use nphysics::joint::{
DefaultJointConstraintSet, FixedConstraint, PrismaticConstraint, RevoluteConstraint,
};
use nphysics::object::{
BodyPartHandle, ColliderDesc, DefaultBodyHandle, DefaultBodySet, DefaultColliderSet,
RigidBodyDesc,
};
use nphysics::world::{DefaultGeometricalWorld, DefaultMechanicalWorld};
use rapier::counters::Counters;
use rapier::dynamics::{
IntegrationParameters, JointParams, JointSet, RigidBodyHandle, RigidBodySet,
};
use rapier::geometry::{Collider, ColliderSet};
use rapier::math::Vector;
use std::collections::HashMap;
#[cfg(feature = "dim3")]
use {ncollide::shape::TriMesh, nphysics::joint::BallConstraint};
pub struct NPhysicsWorld {
rapier2nphysics: HashMap<RigidBodyHandle, DefaultBodyHandle>,
mechanical_world: DefaultMechanicalWorld<f32>,
geometrical_world: DefaultGeometricalWorld<f32>,
bodies: DefaultBodySet<f32>,
colliders: DefaultColliderSet<f32>,
joints: DefaultJointConstraintSet<f32>,
force_generators: DefaultForceGeneratorSet<f32>,
}
impl NPhysicsWorld {
pub fn from_rapier(
gravity: Vector<f32>,
bodies: &RigidBodySet,
colliders: &ColliderSet,
joints: &JointSet,
) -> Self {
let mut rapier2nphysics = HashMap::new();
let mechanical_world = DefaultMechanicalWorld::new(gravity);
let geometrical_world = DefaultGeometricalWorld::new();
let mut nphysics_bodies = DefaultBodySet::new();
let mut nphysics_colliders = DefaultColliderSet::new();
let mut nphysics_joints = DefaultJointConstraintSet::new();
let force_generators = DefaultForceGeneratorSet::new();
for (rapier_handle, rb) in bodies.iter() {
// let material = physics.create_material(rb.collider.friction, rb.collider.friction, 0.0);
let nphysics_rb = RigidBodyDesc::new().position(*rb.position()).build();
let nphysics_rb_handle = nphysics_bodies.insert(nphysics_rb);
rapier2nphysics.insert(rapier_handle, nphysics_rb_handle);
}
for (_, collider) in colliders.iter() {
let parent = &bodies[collider.parent()];
let nphysics_rb_handle = rapier2nphysics[&collider.parent()];
if let Some(collider) =
nphysics_collider_from_rapier_collider(&collider, parent.is_dynamic())
{
let nphysics_collider = collider.build(BodyPartHandle(nphysics_rb_handle, 0));
nphysics_colliders.insert(nphysics_collider);
} else {
eprintln!("Creating shape unknown to the nphysics backend.")
}
}
for joint in joints.iter() {
let b1 = BodyPartHandle(rapier2nphysics[&joint.1.body1], 0);
let b2 = BodyPartHandle(rapier2nphysics[&joint.1.body2], 0);
match &joint.1.params {
JointParams::FixedJoint(params) => {
let c = FixedConstraint::new(
b1,
b2,
params.local_anchor1.translation.vector.into(),
params.local_anchor1.rotation,
params.local_anchor2.translation.vector.into(),
params.local_anchor2.rotation,
);
nphysics_joints.insert(c);
}
#[cfg(feature = "dim3")]
JointParams::BallJoint(params) => {
let c = BallConstraint::new(b1, b2, params.local_anchor1, params.local_anchor2);
nphysics_joints.insert(c);
}
#[cfg(feature = "dim2")]
JointParams::BallJoint(params) => {
let c =
RevoluteConstraint::new(b1, b2, params.local_anchor1, params.local_anchor2);
nphysics_joints.insert(c);
}
#[cfg(feature = "dim3")]
JointParams::RevoluteJoint(params) => {
let c = RevoluteConstraint::new(
b1,
b2,
params.local_anchor1,
params.local_axis1,
params.local_anchor2,
params.local_axis2,
);
nphysics_joints.insert(c);
}
JointParams::PrismaticJoint(params) => {
let mut c = PrismaticConstraint::new(
b1,
b2,
params.local_anchor1,
params.local_axis1(),
params.local_anchor2,
);
if params.limits_enabled {
c.enable_min_offset(params.limits[0]);
c.enable_max_offset(params.limits[1]);
}
nphysics_joints.insert(c);
} // JointParams::GenericJoint(_) => {
// eprintln!(
// "Joint type currently unsupported by the nphysics backend: GenericJoint."
// )
// }
}
}
Self {
rapier2nphysics,
mechanical_world,
geometrical_world,
bodies: nphysics_bodies,
colliders: nphysics_colliders,
joints: nphysics_joints,
force_generators,
}
}
pub fn step(&mut self, counters: &mut Counters, params: &IntegrationParameters) {
self.mechanical_world
.integration_parameters
.max_position_iterations = params.max_position_iterations;
self.mechanical_world
.integration_parameters
.max_velocity_iterations = params.max_velocity_iterations;
self.mechanical_world
.integration_parameters
.set_dt(params.dt);
self.mechanical_world.integration_parameters.warmstart_coeff = params.warmstart_coeff;
counters.step_started();
self.mechanical_world.step(
&mut self.geometrical_world,
&mut self.bodies,
&mut self.colliders,
&mut self.joints,
&mut self.force_generators,
);
counters.step_completed();
}
pub fn sync(&self, bodies: &mut RigidBodySet, colliders: &mut ColliderSet) {
for (rapier_handle, nphysics_handle) in self.rapier2nphysics.iter() {
let rb = bodies.get_mut(*rapier_handle).unwrap();
let ra = self.bodies.rigid_body(*nphysics_handle).unwrap();
let pos = *ra.position();
rb.set_position(pos, false);
for coll_handle in rb.colliders() {
let collider = &mut colliders[*coll_handle];
collider.set_position_debug(pos * collider.position_wrt_parent());
}
}
}
}
fn nphysics_collider_from_rapier_collider(
collider: &Collider,
is_dynamic: bool,
) -> Option<ColliderDesc<f32>> {
let mut margin = ColliderDesc::<f32>::default_margin();
let mut pos = *collider.position_wrt_parent();
let shape = collider.shape();
let shape = if let Some(cuboid) = shape.as_cuboid() {
ShapeHandle::new(Cuboid::new(cuboid.half_extents.map(|e| e - margin)))
} else if let Some(cuboid) = shape.as_round_cuboid() {
margin = cuboid.border_radius;
ShapeHandle::new(Cuboid::new(cuboid.base_shape.half_extents))
} else if let Some(ball) = shape.as_ball() {
ShapeHandle::new(Ball::new(ball.radius - margin))
} else if let Some(capsule) = shape.as_capsule() {
pos *= capsule.transform_wrt_y();
ShapeHandle::new(Capsule::new(capsule.half_height(), capsule.radius))
} else if let Some(heightfield) = shape.as_heightfield() {
let heights = heightfield.heights();
let scale = heightfield.scale();
let heightfield = HeightField::new(heights.clone(), *scale);
ShapeHandle::new(heightfield)
} else {
#[cfg(feature = "dim3")]
if let Some(trimesh) = shape.as_trimesh() {
ShapeHandle::new(TriMesh::new(
trimesh.vertices().to_vec(),
trimesh
.indices()
.iter()
.map(|idx| na::Point3::new(idx[0] as usize, idx[1] as usize, idx[2] as usize))
.collect(),
None,
))
}
|