1 | |
---|
2 | #include "OgreOdePrecompiledHeaders.h" |
---|
3 | |
---|
4 | #include "OgreOdeWorld.h" |
---|
5 | #include "OgreOdeBody.h" |
---|
6 | #include "OgreOdeMass.h" |
---|
7 | #include "OgreOdeDebugObject.h" |
---|
8 | #include "OgreOdeGeometry.h" |
---|
9 | |
---|
10 | using namespace OgreOde; |
---|
11 | using namespace Ogre; |
---|
12 | |
---|
13 | |
---|
14 | //----------------------------------------------------------------------- |
---|
15 | BodyState::BodyState(const Ogre::Vector3 &position, |
---|
16 | const Ogre::Quaternion &orientation) |
---|
17 | { |
---|
18 | _position = position; |
---|
19 | _orientation = orientation; |
---|
20 | } |
---|
21 | //----------------------------------------------------------------------- |
---|
22 | void BodyState::operator=(const BodyState &other) |
---|
23 | { |
---|
24 | _position = other._position; |
---|
25 | _orientation = other._orientation; |
---|
26 | } |
---|
27 | //----------------------------------------------------------------------- |
---|
28 | bool BodyState::operator==(const BodyState &other) const |
---|
29 | { |
---|
30 | return _position == other._position && |
---|
31 | _orientation == other._orientation; |
---|
32 | } |
---|
33 | //----------------------------------------------------------------------- |
---|
34 | bool BodyState::operator!=(const BodyState &other) const |
---|
35 | { |
---|
36 | return _position != other._position || |
---|
37 | _orientation != other._orientation; |
---|
38 | } |
---|
39 | //----------------------------------------------------------------------- |
---|
40 | /// compare with another physics state for "significant" differences. |
---|
41 | /// used for detecting position or orientation snaps which need smoothing. |
---|
42 | bool BodyState::isDifferent(const BodyState &other, const Ogre::Real threshold) const |
---|
43 | { |
---|
44 | return (other._position-_position).squaredLength() > threshold || |
---|
45 | (other._orientation-_orientation).normalise() > threshold; |
---|
46 | } |
---|
47 | |
---|
48 | //----------------------------------------------------------------------- |
---|
49 | void BodyState::interpolate(const BodyState * const _previous_state, |
---|
50 | const BodyState * const _current_state, |
---|
51 | const Ogre::Real alpha) |
---|
52 | { |
---|
53 | const Ogre::Real timeDiffuseless = Ogre::Real(0.0001); |
---|
54 | if (alpha - timeDiffuseless < 0) // alpha <= 0 |
---|
55 | { |
---|
56 | _position = _current_state->_position; |
---|
57 | _orientation = _current_state->_orientation; |
---|
58 | } |
---|
59 | else if (alpha - 1 - timeDiffuseless > 0) // alpha >= 1 |
---|
60 | { |
---|
61 | _position = _previous_state->_position; |
---|
62 | _orientation = _previous_state->_orientation; |
---|
63 | } |
---|
64 | else |
---|
65 | #if _DEBUG |
---|
66 | if (_previous_state->isDifferent(*_current_state, Ogre::Real(0.01))) |
---|
67 | { |
---|
68 | _position = _current_state->_position*alpha + _previous_state->_position * (1.0f - alpha); |
---|
69 | _orientation = Ogre::Quaternion::Slerp(alpha, _previous_state->_orientation, _current_state->_orientation, true); //shortest path ? |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | _position = _previous_state->_position; |
---|
74 | _orientation = _previous_state->_orientation; |
---|
75 | } |
---|
76 | #else |
---|
77 | { |
---|
78 | _position = _current_state->_position*alpha + _previous_state->_position * (1.0f - alpha); |
---|
79 | _orientation = Ogre::Quaternion::Slerp(alpha, _previous_state->_orientation, _current_state->_orientation, true); //shortest path ? |
---|
80 | } |
---|
81 | #endif |
---|
82 | } |
---|
83 | |
---|
84 | //----------------------------------------------------------------------- |
---|
85 | int Body::_body_count = 0; |
---|
86 | //----------------------------------------------------------------------- |
---|
87 | const Ogre::String Body::MovableType = "OgreOde::Body"; |
---|
88 | //----------------------------------------------------------------------- |
---|
89 | Body::Body(World *world, const Ogre::String& name): |
---|
90 | MovableObject(), |
---|
91 | //UserDefinedObject(), |
---|
92 | _name(name), |
---|
93 | _world(world), |
---|
94 | _modify_parent_orientation(true) |
---|
95 | { |
---|
96 | _body_count++; |
---|
97 | _body = dBodyCreate(_world->getWorldID()); |
---|
98 | |
---|
99 | _debug_node = 0; |
---|
100 | _mass = new Mass(); |
---|
101 | |
---|
102 | dBodySetData(_body,(void*)this); |
---|
103 | _world->getBodyList().registerItem(this); |
---|
104 | |
---|
105 | if (_name.empty()) |
---|
106 | _name = MovableType + Ogre::StringConverter::toString(_body_count); |
---|
107 | |
---|
108 | setDamping (_world->_linear_damping, |
---|
109 | _world->_angular_damping); |
---|
110 | _user_data = 0; |
---|
111 | |
---|
112 | _isEnabled = true; |
---|
113 | _historyResize(_world->getHistorySize()); |
---|
114 | // Tie the physical body to the movable |
---|
115 | // used when getting physical out of Movables. |
---|
116 | this->setUserObject (this); |
---|
117 | |
---|
118 | } |
---|
119 | //----------------------------------------------------------------------- |
---|
120 | void Body::_historyResize(const size_t size) |
---|
121 | { |
---|
122 | _state_history.resize (size); |
---|
123 | for (size_t i = 0; i < size; i++) |
---|
124 | { |
---|
125 | _state_history.add(new BodyState (_draw_state)); |
---|
126 | } |
---|
127 | } |
---|
128 | //----------------------------------------------------------------------- |
---|
129 | void Body::_notifyAttached(Node* parent,bool isTagPoint) |
---|
130 | { |
---|
131 | MovableObject::_notifyAttached(parent,isTagPoint); |
---|
132 | if (parent) |
---|
133 | { |
---|
134 | Body* other_body = _world->findBody(static_cast<SceneNode*>(parent)); |
---|
135 | if ((other_body) && (other_body != this)) |
---|
136 | { |
---|
137 | static_cast<SceneNode*>(parent)->detachObject(other_body); |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | setPosition(parent->getPosition()); |
---|
142 | setOrientation(parent->getOrientation()); |
---|
143 | } |
---|
144 | } |
---|
145 | //----------------------------------------------------------------------- |
---|
146 | void Body::destroyDebugNode() |
---|
147 | { |
---|
148 | if (_debug_node) |
---|
149 | { |
---|
150 | _world->notifyGeometry(this); |
---|
151 | |
---|
152 | SceneNode* sn = static_cast<SceneNode*>(_debug_node); |
---|
153 | sn->removeAndDestroyAllChildren (); |
---|
154 | sn = static_cast<SceneNode*>(_debug_node->getParent()); |
---|
155 | sn->removeAndDestroyChild(_debug_node->getName()); |
---|
156 | _debug_node = 0; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | //----------------------------------------------------------------------- |
---|
161 | void Body::addDebugNode(Node* node) |
---|
162 | { |
---|
163 | if (!_debug_node) |
---|
164 | { |
---|
165 | _debug_node = _world->_manager->getRootSceneNode()->createChild(_name + Ogre::String("_DebugBody")); |
---|
166 | } |
---|
167 | |
---|
168 | _debug_node->addChild(node); |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | //----------------------------------------------------------------------- |
---|
173 | void Body::setModifyParentOrientation(bool bModify) |
---|
174 | { |
---|
175 | _modify_parent_orientation = bModify; |
---|
176 | } |
---|
177 | //----------------------------------------------------------------------- |
---|
178 | void Body::setPosition(const Ogre::Vector3& position) |
---|
179 | { |
---|
180 | dBodySetPosition(_body,(dReal)position.x,(dReal)position.y,(dReal)position.z); |
---|
181 | |
---|
182 | _draw_state._position = position; |
---|
183 | for (size_t i = 0; i < _state_history.size (); i++) |
---|
184 | { |
---|
185 | _state_history[i]->_position = position; |
---|
186 | } |
---|
187 | |
---|
188 | if (mParentNode) |
---|
189 | mParentNode->setPosition(position); |
---|
190 | } |
---|
191 | |
---|
192 | //----------------------------------------------------------------------- |
---|
193 | void Body::setOrientation(const Ogre::Quaternion& orientation) |
---|
194 | { |
---|
195 | dQuaternion q; |
---|
196 | q[0] = (dReal)orientation.w; |
---|
197 | q[1] = (dReal)orientation.x; |
---|
198 | q[2] = (dReal)orientation.y; |
---|
199 | q[3] = (dReal)orientation.z; |
---|
200 | dBodySetQuaternion(_body,q); |
---|
201 | |
---|
202 | _draw_state._orientation = orientation; |
---|
203 | for (size_t i = 0; i < _state_history.size (); i++) |
---|
204 | { |
---|
205 | _state_history[i]->_orientation = orientation; |
---|
206 | } |
---|
207 | |
---|
208 | if (mParentNode && _modify_parent_orientation) |
---|
209 | mParentNode->setOrientation(orientation); |
---|
210 | } |
---|
211 | |
---|
212 | //----------------------------------------------------------------------- |
---|
213 | void Body::setLinearVelocity(const Ogre::Vector3& linear_velocity) |
---|
214 | { |
---|
215 | dBodySetLinearVel(_body,(dReal)linear_velocity.x,(dReal)linear_velocity.y,(dReal)linear_velocity.z); |
---|
216 | } |
---|
217 | |
---|
218 | //----------------------------------------------------------------------- |
---|
219 | void Body::setAngularVelocity(const Ogre::Vector3& angular_velocity) |
---|
220 | { |
---|
221 | dBodySetAngularVel(_body,(dReal)angular_velocity.x,(dReal)angular_velocity.y,(dReal)angular_velocity.z); |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | //----------------------------------------------------------------------- |
---|
226 | const Ogre::Vector3& Body::getLinearVelocity() |
---|
227 | { |
---|
228 | const dReal* linear_velocity = dBodyGetLinearVel(_body); |
---|
229 | _linear_vel.x = (Real)linear_velocity[0]; |
---|
230 | _linear_vel.y = (Real)linear_velocity[1]; |
---|
231 | _linear_vel.z = (Real)linear_velocity[2]; |
---|
232 | |
---|
233 | return _linear_vel; |
---|
234 | } |
---|
235 | |
---|
236 | //----------------------------------------------------------------------- |
---|
237 | const Ogre::Vector3& Body::getAngularVelocity() |
---|
238 | { |
---|
239 | const dReal* angular_velocity = dBodyGetAngularVel(_body); |
---|
240 | _angular_vel.x = (Real)angular_velocity[0]; |
---|
241 | _angular_vel.y = (Real)angular_velocity[1]; |
---|
242 | _angular_vel.z = (Real)angular_velocity[2]; |
---|
243 | |
---|
244 | return _angular_vel; |
---|
245 | } |
---|
246 | |
---|
247 | //----------------------------------------------------------------------- |
---|
248 | const Ogre::String& Body::getMovableType() const |
---|
249 | { |
---|
250 | return MovableType; |
---|
251 | } |
---|
252 | |
---|
253 | //----------------------------------------------------------------------- |
---|
254 | void Body::_notifyCurrentCamera(Camera* camera) |
---|
255 | { |
---|
256 | } |
---|
257 | |
---|
258 | //----------------------------------------------------------------------- |
---|
259 | const AxisAlignedBox& Body::getBoundingBox(void) const |
---|
260 | { |
---|
261 | return _bounding_box; |
---|
262 | } |
---|
263 | |
---|
264 | //----------------------------------------------------------------------- |
---|
265 | Real Body::getBoundingRadius(void) const |
---|
266 | { |
---|
267 | return Ogre::Real(0.0); |
---|
268 | } |
---|
269 | //----------------------------------------------------------------------- |
---|
270 | void Body::_updateRenderQueue(RenderQueue* queue) |
---|
271 | { |
---|
272 | } |
---|
273 | //----------------------------------------------------------------------- |
---|
274 | void Body::setMass(const Mass& mass) |
---|
275 | { |
---|
276 | dBodySetMass(_body,mass.getMassPtr()); |
---|
277 | } |
---|
278 | |
---|
279 | //----------------------------------------------------------------------- |
---|
280 | const Mass& Body::getMass() |
---|
281 | { |
---|
282 | dMass mass; |
---|
283 | dBodyGetMass(_body,&mass); |
---|
284 | *_mass = &mass; |
---|
285 | return *_mass; |
---|
286 | } |
---|
287 | |
---|
288 | //----------------------------------------------------------------------- |
---|
289 | void Body::addForce(const Ogre::Vector3& force) |
---|
290 | { |
---|
291 | dBodyAddForce(_body,(dReal)force.x,(dReal)force.y,(dReal)force.z); |
---|
292 | } |
---|
293 | |
---|
294 | //----------------------------------------------------------------------- |
---|
295 | void Body::addTorque(const Ogre::Vector3& torque) |
---|
296 | { |
---|
297 | dBodyAddTorque(_body,(dReal)torque.x,(dReal)torque.y,(dReal)torque.z); |
---|
298 | } |
---|
299 | |
---|
300 | //----------------------------------------------------------------------- |
---|
301 | void Body::addRelativeForce(const Ogre::Vector3& force) |
---|
302 | { |
---|
303 | dBodyAddRelForce(_body,(dReal)force.x,(dReal)force.y,(dReal)force.z); |
---|
304 | } |
---|
305 | |
---|
306 | //----------------------------------------------------------------------- |
---|
307 | void Body::addRelativeTorque(const Ogre::Vector3& torque) |
---|
308 | { |
---|
309 | dBodyAddRelTorque(_body,(dReal)torque.x,(dReal)torque.y,(dReal)torque.z); |
---|
310 | } |
---|
311 | |
---|
312 | //----------------------------------------------------------------------- |
---|
313 | void Body::addForceAt(const Ogre::Vector3& force,const Ogre::Vector3& position) |
---|
314 | { |
---|
315 | dBodyAddForceAtPos(_body,(dReal)force.x,(dReal)force.y,(dReal)force.z,(dReal)position.x,(dReal)position.y,(dReal)position.z); |
---|
316 | } |
---|
317 | |
---|
318 | //----------------------------------------------------------------------- |
---|
319 | void Body::addForceAtRelative(const Ogre::Vector3& force,const Ogre::Vector3& position) |
---|
320 | { |
---|
321 | dBodyAddForceAtRelPos(_body,(dReal)force.x,(dReal)force.y,(dReal)force.z,(dReal)position.x,(dReal)position.y,(dReal)position.z); |
---|
322 | } |
---|
323 | //----------------------------------------------------------------------- |
---|
324 | void Body::addRelativeForceAt(const Ogre::Vector3& force,const Ogre::Vector3& position) |
---|
325 | { |
---|
326 | dBodyAddRelForceAtPos(_body,(dReal)force.x,(dReal)force.y,(dReal)force.z,(dReal)position.x,(dReal)position.y,(dReal)position.z); |
---|
327 | } |
---|
328 | //----------------------------------------------------------------------- |
---|
329 | void Body::addRelativeForceAtRelative(const Ogre::Vector3& force,const Ogre::Vector3& position) |
---|
330 | { |
---|
331 | dBodyAddRelForceAtRelPos(_body,(dReal)force.x,(dReal)force.y,(dReal)force.z,(dReal)position.x,(dReal)position.y,(dReal)position.z); |
---|
332 | } |
---|
333 | //----------------------------------------------------------------------- |
---|
334 | const Ogre::Vector3& Body::getForce() |
---|
335 | { |
---|
336 | const dReal* force = dBodyGetForce(_body); |
---|
337 | _force.x = force[0]; |
---|
338 | _force.y = force[1]; |
---|
339 | _force.z = force[2]; |
---|
340 | |
---|
341 | return _force; |
---|
342 | } |
---|
343 | //----------------------------------------------------------------------- |
---|
344 | const Ogre::Vector3& Body::getTorque() |
---|
345 | { |
---|
346 | const dReal* torque = dBodyGetTorque(_body); |
---|
347 | _torque.x = torque[0]; |
---|
348 | _torque.y = torque[1]; |
---|
349 | _torque.z = torque[2]; |
---|
350 | |
---|
351 | return _torque; |
---|
352 | } |
---|
353 | //----------------------------------------------------------------------- |
---|
354 | void Body::setForce(const Ogre::Vector3& force) |
---|
355 | { |
---|
356 | dBodySetForce(_body, (dReal) force.x, (dReal)force.y, (dReal)force.z); |
---|
357 | } |
---|
358 | //----------------------------------------------------------------------- |
---|
359 | void Body::setTorque(const Ogre::Vector3& torque) |
---|
360 | { |
---|
361 | dBodySetTorque(_body, (dReal) torque.x, (dReal)torque.y, (dReal)torque.z); |
---|
362 | } |
---|
363 | //----------------------------------------------------------------------- |
---|
364 | /* |
---|
365 | Given a point on a body, get that point's position in the world |
---|
366 | */ |
---|
367 | Vector3 Body::getPointWorldPosition(const Ogre::Vector3& position) |
---|
368 | { |
---|
369 | dVector3 result; |
---|
370 | dBodyGetRelPointPos(_body,(dReal)position.x,(dReal)position.y,(dReal)position.z,result); |
---|
371 | |
---|
372 | return Ogre::Vector3(result[0],result[1],result[2]); |
---|
373 | } |
---|
374 | //----------------------------------------------------------------------- |
---|
375 | /* |
---|
376 | Given a point on a body, get that point's velocity in the world |
---|
377 | */ |
---|
378 | Vector3 Body::getPointWorldVelocity(const Ogre::Vector3& position) |
---|
379 | { |
---|
380 | dVector3 result; |
---|
381 | dBodyGetRelPointVel(_body,(dReal)position.x,(dReal)position.y,(dReal)position.z,result); |
---|
382 | |
---|
383 | return Ogre::Vector3(result[0],result[1],result[2]); |
---|
384 | } |
---|
385 | //----------------------------------------------------------------------- |
---|
386 | /* |
---|
387 | Given a point (in the world), get that point's velocity in the world with respect to the body |
---|
388 | i.e. convert the global point to a relative point on the body and compute the velocity of that |
---|
389 | point on the body |
---|
390 | */ |
---|
391 | Vector3 Body::getPointVelocity(const Ogre::Vector3& position) |
---|
392 | { |
---|
393 | dVector3 result; |
---|
394 | dBodyGetPointVel(_body,(dReal)position.x,(dReal)position.y,(dReal)position.z,result); |
---|
395 | |
---|
396 | return Ogre::Vector3(result[0],result[1],result[2]); |
---|
397 | } |
---|
398 | //----------------------------------------------------------------------- |
---|
399 | Vector3 Body::getPointBodyPosition(const Ogre::Vector3& position) |
---|
400 | { |
---|
401 | dVector3 result; |
---|
402 | dBodyGetPosRelPoint(_body,(dReal)position.x,(dReal)position.y,(dReal)position.z,result); |
---|
403 | |
---|
404 | return Ogre::Vector3(result[0],result[1],result[2]); |
---|
405 | } |
---|
406 | //----------------------------------------------------------------------- |
---|
407 | Vector3 Body::getVectorToWorld(const Ogre::Vector3& vector) |
---|
408 | { |
---|
409 | dVector3 result; |
---|
410 | dBodyVectorToWorld(_body,(dReal)vector.x,(dReal)vector.y,(dReal)vector.z,result); |
---|
411 | |
---|
412 | return Ogre::Vector3(result[0],result[1],result[2]); |
---|
413 | } |
---|
414 | //----------------------------------------------------------------------- |
---|
415 | Vector3 Body::getVectorFromWorld(const Ogre::Vector3& vector) |
---|
416 | { |
---|
417 | dVector3 result; |
---|
418 | dBodyVectorFromWorld(_body,(dReal)vector.x,(dReal)vector.y,(dReal)vector.z,result); |
---|
419 | |
---|
420 | return Ogre::Vector3(result[0],result[1],result[2]); |
---|
421 | } |
---|
422 | //----------------------------------------------------------------------- |
---|
423 | void Body::wake() |
---|
424 | { |
---|
425 | dBodyEnable(_body); |
---|
426 | _isEnabled = true; |
---|
427 | } |
---|
428 | //----------------------------------------------------------------------- |
---|
429 | void Body::sleep() |
---|
430 | { |
---|
431 | dBodyDisable(_body); |
---|
432 | _isEnabled = false; |
---|
433 | } |
---|
434 | //----------------------------------------------------------------------- |
---|
435 | void Body::setAutoSleep(bool auto_disable) |
---|
436 | { |
---|
437 | dBodySetAutoDisableFlag(_body,((auto_disable)?1:0)); |
---|
438 | } |
---|
439 | //----------------------------------------------------------------------- |
---|
440 | bool Body::getAutoSleep() |
---|
441 | { |
---|
442 | return ((dBodyGetAutoDisableFlag(_body))?true:false); |
---|
443 | } |
---|
444 | //----------------------------------------------------------------------- |
---|
445 | void Body::setAutoSleepLinearThreshold(Real linear_threshold) |
---|
446 | { |
---|
447 | dBodySetAutoDisableLinearThreshold(_body,(dReal)linear_threshold); |
---|
448 | } |
---|
449 | //----------------------------------------------------------------------- |
---|
450 | Real Body::getAutoSleepLinearThreshold() |
---|
451 | { |
---|
452 | return (Real)dBodyGetAutoDisableLinearThreshold(_body); |
---|
453 | } |
---|
454 | //----------------------------------------------------------------------- |
---|
455 | void Body::setAutoSleepAngularThreshold(Real angular_threshold) |
---|
456 | { |
---|
457 | dBodySetAutoDisableAngularThreshold(_body,(dReal)angular_threshold); |
---|
458 | } |
---|
459 | //----------------------------------------------------------------------- |
---|
460 | Real Body::getAutoSleepAngularThreshold() |
---|
461 | { |
---|
462 | return (Real)dBodyGetAutoDisableAngularThreshold(_body); |
---|
463 | } |
---|
464 | //----------------------------------------------------------------------- |
---|
465 | void Body::setAutoSleepSteps(int steps) |
---|
466 | { |
---|
467 | dBodySetAutoDisableSteps(_body,steps); |
---|
468 | } |
---|
469 | //----------------------------------------------------------------------- |
---|
470 | int Body::getAutoSleepSteps() |
---|
471 | { |
---|
472 | return dBodyGetAutoDisableSteps(_body); |
---|
473 | } |
---|
474 | //----------------------------------------------------------------------- |
---|
475 | void Body::setAutoSleepTime(Real time) |
---|
476 | { |
---|
477 | dBodySetAutoDisableTime(_body,(dReal)time); |
---|
478 | } |
---|
479 | //----------------------------------------------------------------------- |
---|
480 | Real Body::getAutoSleepTime() |
---|
481 | { |
---|
482 | return (Real)dBodyGetAutoDisableTime(_body); |
---|
483 | } |
---|
484 | //----------------------------------------------------------------------- |
---|
485 | void Body::setAutoSleepDefaults() |
---|
486 | { |
---|
487 | dBodySetAutoDisableDefaults(_body); |
---|
488 | } |
---|
489 | //----------------------------------------------------------------------- |
---|
490 | void Body::setFiniteRotationMode(bool on) |
---|
491 | { |
---|
492 | dBodySetFiniteRotationMode(_body,((on)?1:0)); |
---|
493 | } |
---|
494 | //----------------------------------------------------------------------- |
---|
495 | bool Body::getFiniteRotationMode() |
---|
496 | { |
---|
497 | return ((dBodyGetFiniteRotationMode(_body))?true:false); |
---|
498 | } |
---|
499 | //----------------------------------------------------------------------- |
---|
500 | void Body::setFiniteRotationAxis(const Ogre::Vector3& axis) |
---|
501 | { |
---|
502 | dBodySetFiniteRotationAxis(_body,(dReal)axis.x,(dReal)axis.y,(dReal)axis.z); |
---|
503 | } |
---|
504 | //----------------------------------------------------------------------- |
---|
505 | const Ogre::Vector3& Body::getFiniteRotationAxis() |
---|
506 | { |
---|
507 | dVector3 result; |
---|
508 | dBodyGetFiniteRotationAxis(_body,result); |
---|
509 | _finite_axis.x = result[0]; |
---|
510 | _finite_axis.y = result[1]; |
---|
511 | _finite_axis.z = result[2]; |
---|
512 | |
---|
513 | return _finite_axis; |
---|
514 | } |
---|
515 | //----------------------------------------------------------------------- |
---|
516 | int Body::getJointCount() |
---|
517 | { |
---|
518 | return dBodyGetNumJoints(_body); |
---|
519 | } |
---|
520 | //----------------------------------------------------------------------- |
---|
521 | Joint* Body::getJoint(int index) |
---|
522 | { |
---|
523 | return (Joint*)_world->getJointList().findItem((size_t)dBodyGetJoint(_body,index)); |
---|
524 | } |
---|
525 | //----------------------------------------------------------------------- |
---|
526 | size_t Body::getGeometryCount() |
---|
527 | { |
---|
528 | return _geometries.size(); |
---|
529 | // int rc = 0; |
---|
530 | // Geometry* g= 0; |
---|
531 | // MaintainedItemIterator<Geometry> it = _world->_geometry_list.getIterator(); |
---|
532 | // while(!it.end ()) |
---|
533 | // { |
---|
534 | // g = (Geometry*) (it.getNext ()); |
---|
535 | // if (this == g->getBody()) |
---|
536 | // ++rc; |
---|
537 | // } |
---|
538 | // |
---|
539 | // return rc; |
---|
540 | } |
---|
541 | //----------------------------------------------------------------------- |
---|
542 | Geometry* Body::getGeometry(int index) |
---|
543 | { |
---|
544 | return _geometries[index]; |
---|
545 | |
---|
546 | // Geometry* rc = 0; |
---|
547 | // int idx = 0; |
---|
548 | // Geometry* g = 0; |
---|
549 | // MaintainedItemIterator<Geometry> it = _world->_geometry_list.getIterator(); |
---|
550 | // while(!it.end ()) |
---|
551 | // { |
---|
552 | // g = (Geometry*) (it.getNext ()); |
---|
553 | // if (this == g->getBody()) |
---|
554 | // { |
---|
555 | // rc = g; |
---|
556 | // ++idx; |
---|
557 | // |
---|
558 | // if (idx > index) |
---|
559 | // break; |
---|
560 | // } |
---|
561 | // } |
---|
562 | // |
---|
563 | // return rc; |
---|
564 | } |
---|
565 | GeometryArray* Body::getGeometries() |
---|
566 | { |
---|
567 | return &_geometries; |
---|
568 | } |
---|
569 | |
---|
570 | //----------------------------------------------------------------------- |
---|
571 | void Body::addGeometry(Geometry *g) |
---|
572 | { |
---|
573 | _geometries.push_back(g); |
---|
574 | } |
---|
575 | //----------------------------------------------------------------------- |
---|
576 | void Body::removeGeometry(Geometry *g) |
---|
577 | { |
---|
578 | for (GeometryArray::iterator it = _geometries.begin (); |
---|
579 | it != _geometries.end (); |
---|
580 | ++it) |
---|
581 | { |
---|
582 | if (*it == g) |
---|
583 | { |
---|
584 | _geometries.erase (it); |
---|
585 | return; |
---|
586 | } |
---|
587 | } |
---|
588 | } |
---|
589 | //----------------------------------------------------------------------- |
---|
590 | void Body::setAffectedByGravity(bool on) |
---|
591 | { |
---|
592 | dBodySetGravityMode(_body,((on)?1:0)); |
---|
593 | } |
---|
594 | //----------------------------------------------------------------------- |
---|
595 | bool Body::getAffectedByGravity() |
---|
596 | { |
---|
597 | return ((dBodyGetGravityMode(_body))?true:false); |
---|
598 | } |
---|
599 | //----------------------------------------------------------------------- |
---|
600 | dBodyID Body::getBodyID() const |
---|
601 | { |
---|
602 | return _body; |
---|
603 | } |
---|
604 | //----------------------------------------------------------------------- |
---|
605 | const Ogre::String& Body::getName(void) const |
---|
606 | { |
---|
607 | return _name; |
---|
608 | } |
---|
609 | |
---|
610 | //----------------------------------------------------------------------- |
---|
611 | void Body::deriveLocation() |
---|
612 | { |
---|
613 | if (mParentNode) |
---|
614 | { |
---|
615 | setPosition(mParentNode->getPosition()); |
---|
616 | setOrientation(mParentNode->getOrientation()); |
---|
617 | } |
---|
618 | } |
---|
619 | |
---|
620 | //----------------------------------------------------------------------- |
---|
621 | void Body::recursiveSetMode(SceneNode* node) |
---|
622 | { |
---|
623 | for(unsigned short i = 0;i < node->numChildren();i++) |
---|
624 | { |
---|
625 | recursiveSetMode(static_cast<SceneNode*>(node->getChild(i))); |
---|
626 | } |
---|
627 | |
---|
628 | if (_debug_node) |
---|
629 | { |
---|
630 | for(unsigned short j = 0;j < node->numAttachedObjects();j++) |
---|
631 | { |
---|
632 | static_cast<DebugObject*>(node->getAttachedObject(j))->setMode( |
---|
633 | (dBodyIsEnabled(_body))? |
---|
634 | DebugObject::Mode_Enabled |
---|
635 | : |
---|
636 | DebugObject::Mode_Disabled); |
---|
637 | } |
---|
638 | } |
---|
639 | } |
---|
640 | //----------------------------------------------------------------------- |
---|
641 | void Body::setDamping(Real linear_damping,Real angular_damping) |
---|
642 | { |
---|
643 | _linear_damping = -(dReal)linear_damping; |
---|
644 | _is_linear_damped = (_linear_damping < 0.0); |
---|
645 | _angular_damping = -(dReal)angular_damping; |
---|
646 | _is_angular_damped = (_angular_damping < 0.0); |
---|
647 | |
---|
648 | _is_damped = _linear_damping || _is_angular_damped; |
---|
649 | } |
---|
650 | //----------------------------------------------------------------------- |
---|
651 | void Body::setLinearDamping(Real linear_damping) |
---|
652 | { |
---|
653 | _linear_damping = -(dReal)linear_damping; |
---|
654 | _is_linear_damped = (_linear_damping < 0.0); |
---|
655 | |
---|
656 | _is_damped = _linear_damping || _is_angular_damped; |
---|
657 | } |
---|
658 | //----------------------------------------------------------------------- |
---|
659 | void Body::setAngularDamping(Real angular_damping) |
---|
660 | { |
---|
661 | _angular_damping = -(dReal)angular_damping; |
---|
662 | _is_angular_damped = (_angular_damping < 0.0); |
---|
663 | |
---|
664 | _is_damped = _linear_damping || _is_angular_damped; |
---|
665 | } |
---|
666 | //----------------------------------------------------------------------- |
---|
667 | Real Body::getLinearDamping() |
---|
668 | { |
---|
669 | return -(Real)_linear_damping; |
---|
670 | } |
---|
671 | //----------------------------------------------------------------------- |
---|
672 | Real Body::getAngularDamping() |
---|
673 | { |
---|
674 | return -(Real)_angular_damping; |
---|
675 | } |
---|
676 | //----------------------------------------------------------------------- |
---|
677 | void Body::applyDamping() |
---|
678 | { |
---|
679 | assert (_isEnabled && _is_damped); |
---|
680 | |
---|
681 | if (_linear_damping) |
---|
682 | { |
---|
683 | const dReal * const v = dBodyGetLinearVel(_body); |
---|
684 | dBodyAddForce(_body, v[0] * _linear_damping, v[1] * _linear_damping, v[2] * _linear_damping); |
---|
685 | } |
---|
686 | |
---|
687 | if (_is_angular_damped) |
---|
688 | { |
---|
689 | const dReal * const v = dBodyGetAngularVel(_body); |
---|
690 | dBodyAddTorque(_body, v[0] * _angular_damping, v[1] * _angular_damping, v[2] * _angular_damping); |
---|
691 | } |
---|
692 | } |
---|
693 | //----------------------------------------------------------------------- |
---|
694 | void Body::setDebug(const bool debug) |
---|
695 | { |
---|
696 | destroyDebugNode(); |
---|
697 | } |
---|
698 | //----------------------------------------------------------------------- |
---|
699 | void Body::setUserData(size_t user_data) |
---|
700 | { |
---|
701 | _user_data = user_data; |
---|
702 | } |
---|
703 | //----------------------------------------------------------------------- |
---|
704 | size_t Body::getUserData() |
---|
705 | { |
---|
706 | return _user_data; |
---|
707 | } |
---|
708 | //----------------------------------------------------------------------- |
---|
709 | size_t Body::getID() |
---|
710 | { |
---|
711 | return (size_t)_body; |
---|
712 | } |
---|
713 | //----------------------------------------------------------------------- |
---|
714 | Body::~Body() |
---|
715 | { |
---|
716 | destroyDebugNode(); |
---|
717 | delete _mass; |
---|
718 | |
---|
719 | _world->getBodyList().unregisterItem((size_t)_body); |
---|
720 | dBodyDestroy(_body); |
---|
721 | } |
---|
722 | |
---|
723 | //----------------------------------------------------------------------- |
---|
724 | bool Body::collide(void* data, Geometry *otherGeometry) |
---|
725 | { |
---|
726 | bool collided = false; |
---|
727 | dContactGeom contactGeom; |
---|
728 | Geometry *o1; |
---|
729 | GeometryArray::const_iterator proxy1, proxy1end; |
---|
730 | proxy1end = _geometries.end(); |
---|
731 | |
---|
732 | //dContact contact; |
---|
733 | //Real bounce, velThresh, softness; |
---|
734 | |
---|
735 | //CollisionInfo collInfo; |
---|
736 | |
---|
737 | // @@TODO : HAS TO HANDLE TransFormedGeometry and GoemtryOffset |
---|
738 | for (proxy1 = _geometries.begin(); proxy1 != proxy1end; ++proxy1) |
---|
739 | { |
---|
740 | o1 = *proxy1; |
---|
741 | const int numc = dCollide(o1->getGeometryID(), otherGeometry->getGeometryID(), 0, &contactGeom, sizeof(dContactGeom)); |
---|
742 | if (numc) |
---|
743 | { |
---|
744 | // Create contact joints if either object is dynamics simulated |
---|
745 | // If one is not, then sim will not affect it anyway, it will be fixed |
---|
746 | // However if one is enabled, we need the contact joint |
---|
747 | if (this->isAwake()) |
---|
748 | { |
---|
749 | _world->collisionCallback(data, o1->getGeometryID(), otherGeometry->getGeometryID()); |
---|
750 | } |
---|
751 | |
---|
752 | // set return |
---|
753 | collided = true; |
---|
754 | |
---|
755 | } |
---|
756 | } |
---|
757 | return collided; |
---|
758 | } |
---|
759 | //----------------------------------------------------------------------- |
---|
760 | bool Body::collide(void* data, Body *otherBody) |
---|
761 | { |
---|
762 | bool collided = false; |
---|
763 | dContactGeom contactGeom; |
---|
764 | Geometry *o1, *o2; |
---|
765 | GeometryArray::const_iterator proxy1, proxy2, proxy1end, proxy2end; |
---|
766 | proxy1end = _geometries.end(); |
---|
767 | proxy2end = otherBody->getGeometries()->end(); |
---|
768 | |
---|
769 | //dContact contact; |
---|
770 | //Real bounce, velThresh, softness; |
---|
771 | |
---|
772 | //CollisionInfo collInfo; |
---|
773 | |
---|
774 | // @@TODO : HAS TO HANDLE TransFormedGeometry and GoemtryOffset |
---|
775 | for (proxy1 = _geometries.begin(); proxy1 != proxy1end; ++proxy1) |
---|
776 | { |
---|
777 | for (proxy2 = otherBody->getGeometries()->begin(); proxy2 != proxy2end; ++proxy2) |
---|
778 | { |
---|
779 | o1 = *proxy1; |
---|
780 | o2 = *proxy2; |
---|
781 | const int numc = dCollide(o1->getGeometryID(), o2->getGeometryID(), 0, &contactGeom, sizeof(dContactGeom)); |
---|
782 | if (numc) |
---|
783 | { |
---|
784 | // Create contact joints if either object is dynamics simulated |
---|
785 | // If one is not, then sim will not affect it anyway, it will be fixed |
---|
786 | // However if one is enabled, we need the contact joint |
---|
787 | if (this->isAwake() || otherBody->isAwake()) |
---|
788 | { |
---|
789 | |
---|
790 | _world->collisionCallback(data, o1->getGeometryID(), o2->getGeometryID()); |
---|
791 | |
---|
792 | } |
---|
793 | // set return |
---|
794 | collided = true; |
---|
795 | } |
---|
796 | } |
---|
797 | } |
---|
798 | return collided; |
---|
799 | } |
---|
800 | //------------------------------------------------------------------------- |
---|
801 | bool Body::collidePlaneBounds(void* data, Ogre::SceneQuery::WorldFragment *wf) |
---|
802 | { |
---|
803 | bool collided = false; |
---|
804 | dContactGeom contactGeom; |
---|
805 | Geometry *geom; |
---|
806 | GeometryArray::const_iterator proxy, proxyend; |
---|
807 | proxyend = _geometries.end(); |
---|
808 | |
---|
809 | std::list<Ogre::Plane>::const_iterator pi, piend; |
---|
810 | piend = wf->planes->end(); |
---|
811 | |
---|
812 | //CollisionInfo collInfo; |
---|
813 | |
---|
814 | // @@TODO : HAS TO HANDLE TransFormedGeometry and GoemtryOffset |
---|
815 | for (proxy = _geometries.begin(); proxy != proxyend; ++proxy) |
---|
816 | { |
---|
817 | // Hack, simply collide against planes which is facing towards center |
---|
818 | // We can't do this properly without mesh collision |
---|
819 | geom = *proxy; |
---|
820 | Ogre::Real maxdist = -1.0f; |
---|
821 | const Ogre::Plane* bestPlane = 0; |
---|
822 | for (pi = wf->planes->begin(); pi != piend; ++pi) |
---|
823 | { |
---|
824 | const Ogre::Plane * const boundPlane = &(*pi); |
---|
825 | const Ogre::Real dist = boundPlane->getDistance (this->getPosition()); |
---|
826 | if (dist >= 0.0f) |
---|
827 | { |
---|
828 | |
---|
829 | //@@@@ TODO reuse a static InfinitePlaneGeometry ? |
---|
830 | InfinitePlaneGeometry *sliding_plane = new InfinitePlaneGeometry(*boundPlane, 0); |
---|
831 | |
---|
832 | |
---|
833 | int numc = dCollide(geom->getGeometryID(), |
---|
834 | sliding_plane->getGeometryID(), |
---|
835 | 0, |
---|
836 | &contactGeom, |
---|
837 | sizeof(dContactGeom)); |
---|
838 | if (numc) |
---|
839 | { |
---|
840 | // Create contact joints if object is dynamics simulated |
---|
841 | if (this->isAwake()) |
---|
842 | { |
---|
843 | _world->collisionCallback(data, geom->getGeometryID(), sliding_plane->getGeometryID()); |
---|
844 | |
---|
845 | } |
---|
846 | // set return |
---|
847 | collided = true; |
---|
848 | } |
---|
849 | } |
---|
850 | } |
---|
851 | |
---|
852 | } |
---|
853 | return collided; |
---|
854 | } |
---|