-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
world.rs
342 lines (278 loc) · 11.9 KB
/
world.rs
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use std::slice::Iter;
use std::iter::Map;
use std::rc::Rc;
use std::cell::RefCell;
use na;
use ncollide::bounding_volume::AABB;
use ncollide::utils::data::hash_map::{HashMap, Entry};
use ncollide::utils::data::hash::UintTWHash;
use ncollide::broad_phase::{BroadPhase, DBVTBroadPhase};
use ncollide::narrow_phase::ContactSignalHandler;
use ncollide::world::{CollisionWorld, CollisionObject, CollisionGroups};
use integration::{Integrator, BodySmpEulerIntegrator, BodyForceGenerator,
TranslationalCCDMotionClamping};
use detection::ActivationManager;
use detection::Detector;
use detection::constraint::Constraint;
use detection::joint::{JointManager, BallInSocket, Fixed};
use resolution::{Solver, AccumulatedImpulseSolver, CorrectionMode};
use object::{RigidBody, RigidBodyHandle};
use math::{Scalar, Point, Vect, Matrix};
/// The default broad phase.
pub type WorldBroadPhase = DBVTBroadPhase<Point, RigidBodyHandle, AABB<Point>>;
/// An iterator visiting rigid bodies.
pub type RigidBodies<'a> = Map<Iter<'a, Entry<usize, RigidBodyHandle>>, fn(&'a Entry<usize, RigidBodyHandle>) -> &'a RigidBodyHandle>;
/// Type of the collision world containing rigid bodies.
pub type RigidBodyCollisionWorld = CollisionWorld<Point, Matrix, RigidBodyHandle>;
/// Type of a collision object containing a rigid body as its data.
pub type RigidBodyCollisionObject = CollisionObject<Point, Matrix, RigidBodyHandle>;
/// The physical world.
///
/// This is the main structure of the physics engine.
pub struct World {
cworld: RigidBodyCollisionWorld,
bodies: HashMap<usize, RigidBodyHandle, UintTWHash>,
forces: BodyForceGenerator,
integrator: BodySmpEulerIntegrator,
sleep: Rc<RefCell<ActivationManager>>, // FIXME: avoid sharing (needed for the contact signal handler)
ccd: TranslationalCCDMotionClamping,
joints: JointManager,
solver: AccumulatedImpulseSolver,
}
impl World {
/// Creates a new physics world.
pub fn new() -> World {
/*
* Setup the physics world
*/
// For the intergration
let forces = BodyForceGenerator::new(na::zero(), na::zero());
let integrator = BodySmpEulerIntegrator::new();
/*
* For the collision detection
*/
// Collision world
let mut cworld = CollisionWorld::new(na::cast(0.10f64), na::cast(0.10f64), false);
// CCD handler
let ccd = TranslationalCCDMotionClamping::new();
// Deactivation
let sleep = Rc::new(RefCell::new(ActivationManager::new(na::cast(0.01f64))));
// Setup contact handler to reactivate sleeping objects that loose contact.
let handler = ObjectActivationOnContactHandler::new(sleep.clone());
cworld.register_contact_signal_handler("__nphysics_internal_ObjectActivationOnContactHandler", handler);
// Joints
let joints = JointManager::new();
/*
* For constraints resolution
*/
let solver = AccumulatedImpulseSolver::new(
na::cast(0.1f64),
CorrectionMode::VelocityAndPosition(na::cast(0.2f64), na::cast(0.2f64), na::cast(0.08f64)),
na::cast(0.4f64),
na::cast(1.0f64),
10,
10);
World {
cworld: cworld,
bodies: HashMap::new(UintTWHash::new()),
forces: forces,
integrator: integrator,
sleep: sleep,
ccd: ccd,
joints: joints,
solver: solver,
}
}
/// Updates the physics world.
pub fn step(&mut self, dt: Scalar) {
for e in self.bodies.elements_mut().iter_mut() {
let mut rb = e.value.borrow_mut();
if rb.is_active() {
self.forces.update(dt.clone(), &mut *rb);
self.integrator.update(dt.clone(), &mut *rb);
self.cworld.deferred_set_position(&*e.value as *const RefCell<RigidBody> as usize,
rb.position().clone());
}
}
self.cworld.perform_position_update();
self.cworld.perform_broad_phase();
self.ccd.update(&mut self.cworld);
self.cworld.perform_narrow_phase();
self.joints.update(&mut *self.sleep.borrow_mut());
self.sleep.borrow_mut().update(&mut self.cworld, &self.joints, &self.bodies);
// XXX: use `self.collector` instead to avoid allocation.
let mut collector = Vec::new();
for (b1, b2, c) in self.cworld.contacts() {
if b1.data.borrow().is_active() || b2.data.borrow().is_active() {
let m1 = b1.data.borrow().margin();
let m2 = b2.data.borrow().margin();
let mut c = c.clone();
c.depth = c.depth + m1 + m2;
collector.push(Constraint::RBRB(b1.data.clone(), b2.data.clone(), c));
}
}
self.joints.interferences(&mut collector);
self.solver.solve(dt, &collector[..]);
collector.clear();
}
/// Adds a rigid body to the physics world.
pub fn add_body(&mut self, rb: RigidBody) -> RigidBodyHandle {
let position = rb.position().clone();
let shape = rb.shape().clone();
let mut groups = CollisionGroups::new();
const STATIC_GROUP_ID: usize = 29;
groups.modify_whitelist(STATIC_GROUP_ID, false); // The static group is special.
if rb.can_move() {
// This is a dynamic object, remove it from the static objects group.
groups.modify_membership(STATIC_GROUP_ID, false);
}
else {
// This is a dynamic object, keep is on the static objects group but prevent it from
// colliding with other static objects.
groups.modify_blacklist(STATIC_GROUP_ID, true);
}
let handle = Rc::new(RefCell::new(rb));
let uid = &*handle as *const RefCell<RigidBody> as usize;;
self.bodies.insert(uid, handle.clone());
self.cworld.add(uid, position, shape, groups, handle.clone());
handle
}
/// Remove a rigid body from the physics world.
pub fn remove_body(&mut self, b: &RigidBodyHandle) {
let uid = &**b as *const RefCell<RigidBody> as usize;
self.cworld.deferred_remove(uid);
self.cworld.perform_removals_and_broad_phase();
self.joints.remove(b, &mut *self.sleep.borrow_mut());
self.ccd.remove_ccd_from(b);
self.bodies.remove(&uid);
b.borrow_mut().delete();
}
// XXX: keep this reference mutable?
/// Gets a mutable reference to the force generator.
pub fn forces_generator(&mut self) -> &mut BodyForceGenerator {
&mut self.forces
}
// XXX: keep this reference mutable?
/// Gets a mutable reference to the position and orientation integrator.
pub fn integrator(&mut self) -> &mut BodySmpEulerIntegrator {
&mut self.integrator
}
// XXX: keep this reference mutable?
/// Gets a mutable reference to the CCD manager.
pub fn ccd_manager(&mut self) -> &mut TranslationalCCDMotionClamping {
&mut self.ccd
}
// XXX: keep this reference mutable?
/// Gets a mutable reference to the joint manager.
pub fn joint_manager(&mut self) -> &mut JointManager {
&mut self.joints
}
// XXX: keep this reference mutable?
/// Gets a mutable reference to the constraint solver.
pub fn constraints_solver(&mut self) -> &mut AccumulatedImpulseSolver {
&mut self.solver
}
/// Gets the underlying collision world.
pub fn collision_world(&self) -> &RigidBodyCollisionWorld {
&self.cworld
}
/// Sets the linear acceleration afecting every dynamic rigid body.
pub fn set_gravity(&mut self, gravity: Vect) {
self.forces.set_lin_acc(gravity);
}
/// Sets the angular acceleration afecting every dynamic rigid body.
// pub fn set_angular_acceleration(&mut self, accel: Orientation) {
// self.forces.set_ang_acc(accel)
// }
/// Gets the linear acceleration afecting every dynamic rigid body.
pub fn gravity(&self) -> Vect {
self.forces.lin_acc()
}
/// Gets the angular acceleration afecting every dynamic rigid body.
// pub fn angular_acceleration(&self) -> Orientation {
// self.forces.ang_acc()
// }
/// Adds continuous collision detection to the given rigid body.
pub fn add_ccd_to(&mut self, body: &RigidBodyHandle, motion_thresold: Scalar) {
self.ccd.add_ccd_to(body.clone(), motion_thresold)
}
/// Adds a ball-in-socket joint to the world.
pub fn add_ball_in_socket(&mut self, joint: BallInSocket) -> Rc<RefCell<BallInSocket>> {
let res = Rc::new(RefCell::new(joint));
self.joints.add_ball_in_socket(res.clone(), &mut *self.sleep.borrow_mut());
res
}
/// Removes a ball-in-socket joint from the world.
pub fn remove_ball_in_socket(&mut self, joint: &Rc<RefCell<BallInSocket>>) {
self.joints.remove_ball_in_socket(joint, &mut *self.sleep.borrow_mut())
}
/// Adds a fixed joint to the world.
pub fn add_fixed(&mut self, joint: Fixed) -> Rc<RefCell<Fixed>> {
let res = Rc::new(RefCell::new(joint));
self.joints.add_fixed(res.clone(), &mut *self.sleep.borrow_mut());
res
}
/// Removes a fixed joint from the world.
pub fn remove_fixed(&mut self, joint: &Rc<RefCell<Fixed>>) {
self.joints.remove_joint(joint, &mut *self.sleep.borrow_mut())
}
/// Collects every interferences detected since the last update.
pub fn interferences(&mut self, out: &mut Vec<Constraint>) {
// FIXME: ugly.
for (b1, b2, c) in self.cworld.contacts() {
let m1 = b1.data.borrow().margin();
let m2 = b2.data.borrow().margin();
let mut c = c.clone();
c.depth = c.depth + m1 + m2;
out.push(Constraint::RBRB(b1.data.clone(), b2.data.clone(), c));
}
self.joints.interferences(out);
}
/// An iterator visiting all rigid bodies on this world.
pub fn bodies(&self) -> RigidBodies {
fn extract_value(e: &Entry<usize, RigidBodyHandle>) -> &RigidBodyHandle {
&e.value
}
let extract_value_fn: fn(_) -> _ = extract_value;
self.bodies.elements().iter().map(extract_value_fn)
}
/* FIXME
/// Registers a handler for proximity start/stop events.
pub fn register_proximity_signal_handler<H>(&mut self, name: &str, handler: H)
where H: ProximitySignalHandler<RigidBodyHandle> + 'static {
self.cworld.register_proximity_signal_handler(name, handler)
}
/// Unregisters a handler for proximity start/stop events.
pub fn unregister_proximity_signal_handler(&mut self, name: &str) {
self.cworld.unregister_proximity_signal_handler(name)
}
*/
/// Registers a handler for contact start/stop events.
pub fn register_contact_signal_handler<H>(&mut self, name: &str, handler: H)
where H: ContactSignalHandler<RigidBodyHandle> + 'static {
self.cworld.register_contact_signal_handler(name, handler)
}
/// Unregisters a handler for contact start/stop events.
pub fn unregister_contact_signal_handler(&mut self, name: &str) {
self.cworld.unregister_contact_signal_handler(name)
}
}
struct ObjectActivationOnContactHandler {
sleep: Rc<RefCell<ActivationManager>>
}
impl ObjectActivationOnContactHandler {
pub fn new(sleep: Rc<RefCell<ActivationManager>>) -> ObjectActivationOnContactHandler {
ObjectActivationOnContactHandler {
sleep: sleep
}
}
}
impl ContactSignalHandler<RigidBodyHandle> for ObjectActivationOnContactHandler {
fn handle_contact(&mut self, b1: &RigidBodyHandle, b2: &RigidBodyHandle, started: bool) {
// Wake on collision lost.
if !started {
self.sleep.borrow_mut().deferred_activate(b1);
self.sleep.borrow_mut().deferred_activate(b2);
}
}
}