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