[21] | 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((long unsigned int)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 | ; |
---|
| 579 | for (GeometryArray::iterator it = _geometries.begin (); |
---|
| 580 | it != _geometries.end (); |
---|
| 581 | ++it) |
---|
| 582 | { |
---|
| 583 | if (*it = g) |
---|
| 584 | { |
---|
| 585 | _geometries.erase (it); |
---|
| 586 | return; |
---|
| 587 | } |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | //----------------------------------------------------------------------- |
---|
| 591 | void Body::setAffectedByGravity(bool on) |
---|
| 592 | { |
---|
| 593 | dBodySetGravityMode(_body,((on)?1:0)); |
---|
| 594 | } |
---|
| 595 | //----------------------------------------------------------------------- |
---|
| 596 | bool Body::getAffectedByGravity() |
---|
| 597 | { |
---|
| 598 | return ((dBodyGetGravityMode(_body))?true:false); |
---|
| 599 | } |
---|
| 600 | //----------------------------------------------------------------------- |
---|
| 601 | dBodyID Body::getBodyID() const |
---|
| 602 | { |
---|
| 603 | return _body; |
---|
| 604 | } |
---|
| 605 | //----------------------------------------------------------------------- |
---|
| 606 | const Ogre::String& Body::getName(void) const |
---|
| 607 | { |
---|
| 608 | return _name; |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | //----------------------------------------------------------------------- |
---|
| 612 | void Body::deriveLocation() |
---|
| 613 | { |
---|
| 614 | if (mParentNode) |
---|
| 615 | { |
---|
| 616 | setPosition(mParentNode->getPosition()); |
---|
| 617 | setOrientation(mParentNode->getOrientation()); |
---|
| 618 | } |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | //----------------------------------------------------------------------- |
---|
| 622 | void Body::recursiveSetMode(SceneNode* node) |
---|
| 623 | { |
---|
| 624 | for(unsigned short i = 0;i < node->numChildren();i++) |
---|
| 625 | { |
---|
| 626 | recursiveSetMode(static_cast<SceneNode*>(node->getChild(i))); |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | if (_debug_node) |
---|
| 630 | { |
---|
| 631 | for(unsigned short j = 0;j < node->numAttachedObjects();j++) |
---|
| 632 | { |
---|
| 633 | static_cast<DebugObject*>(node->getAttachedObject(j))->setMode( |
---|
| 634 | (dBodyIsEnabled(_body))? |
---|
| 635 | DebugObject::Mode_Enabled |
---|
| 636 | : |
---|
| 637 | DebugObject::Mode_Disabled); |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | //----------------------------------------------------------------------- |
---|
| 642 | void Body::setDamping(Real linear_damping,Real angular_damping) |
---|
| 643 | { |
---|
| 644 | _linear_damping = -(dReal)linear_damping; |
---|
| 645 | _is_linear_damped = (_linear_damping < 0.0); |
---|
| 646 | _angular_damping = -(dReal)angular_damping; |
---|
| 647 | _is_angular_damped = (_angular_damping < 0.0); |
---|
| 648 | |
---|
| 649 | _is_damped = _linear_damping || _is_angular_damped; |
---|
| 650 | } |
---|
| 651 | //----------------------------------------------------------------------- |
---|
| 652 | void Body::setLinearDamping(Real linear_damping) |
---|
| 653 | { |
---|
| 654 | _linear_damping = -(dReal)linear_damping; |
---|
| 655 | _is_linear_damped = (_linear_damping < 0.0); |
---|
| 656 | |
---|
| 657 | _is_damped = _linear_damping || _is_angular_damped; |
---|
| 658 | } |
---|
| 659 | //----------------------------------------------------------------------- |
---|
| 660 | void Body::setAngularDamping(Real angular_damping) |
---|
| 661 | { |
---|
| 662 | _angular_damping = -(dReal)angular_damping; |
---|
| 663 | _is_angular_damped = (_angular_damping < 0.0); |
---|
| 664 | |
---|
| 665 | _is_damped = _linear_damping || _is_angular_damped; |
---|
| 666 | } |
---|
| 667 | //----------------------------------------------------------------------- |
---|
| 668 | Real Body::getLinearDamping() |
---|
| 669 | { |
---|
| 670 | return -(Real)_linear_damping; |
---|
| 671 | } |
---|
| 672 | //----------------------------------------------------------------------- |
---|
| 673 | Real Body::getAngularDamping() |
---|
| 674 | { |
---|
| 675 | return -(Real)_angular_damping; |
---|
| 676 | } |
---|
| 677 | //----------------------------------------------------------------------- |
---|
| 678 | void Body::applyDamping() |
---|
| 679 | { |
---|
| 680 | assert (_isEnabled && _is_damped); |
---|
| 681 | |
---|
| 682 | if (_linear_damping) |
---|
| 683 | { |
---|
| 684 | const dReal * const v = dBodyGetLinearVel(_body); |
---|
| 685 | dBodyAddForce(_body, v[0] * _linear_damping, v[1] * _linear_damping, v[2] * _linear_damping); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | if (_is_angular_damped) |
---|
| 689 | { |
---|
| 690 | const dReal * const v = dBodyGetAngularVel(_body); |
---|
| 691 | dBodyAddTorque(_body, v[0] * _angular_damping, v[1] * _angular_damping, v[2] * _angular_damping); |
---|
| 692 | } |
---|
| 693 | } |
---|
| 694 | //----------------------------------------------------------------------- |
---|
| 695 | void Body::setDebug(const bool debug) |
---|
| 696 | { |
---|
| 697 | destroyDebugNode(); |
---|
| 698 | } |
---|
| 699 | //----------------------------------------------------------------------- |
---|
| 700 | void Body::setUserData(unsigned long user_data) |
---|
| 701 | { |
---|
| 702 | _user_data = user_data; |
---|
| 703 | } |
---|
| 704 | //----------------------------------------------------------------------- |
---|
| 705 | unsigned long Body::getUserData() |
---|
| 706 | { |
---|
| 707 | return _user_data; |
---|
| 708 | } |
---|
| 709 | //----------------------------------------------------------------------- |
---|
| 710 | unsigned long Body::getID() |
---|
| 711 | { |
---|
| 712 | return (unsigned long)_body; |
---|
| 713 | } |
---|
| 714 | //----------------------------------------------------------------------- |
---|
| 715 | Body::~Body() |
---|
| 716 | { |
---|
| 717 | destroyDebugNode(); |
---|
| 718 | delete _mass; |
---|
| 719 | |
---|
| 720 | _world->getBodyList().unregisterItem((long unsigned int)_body); |
---|
| 721 | dBodyDestroy(_body); |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | //----------------------------------------------------------------------- |
---|
| 725 | bool Body::collide(void* data, Geometry *otherGeometry) |
---|
| 726 | { |
---|
| 727 | bool collided = false; |
---|
| 728 | dContactGeom contactGeom; |
---|
| 729 | Geometry *o1; |
---|
| 730 | GeometryArray::const_iterator proxy1, proxy1end; |
---|
| 731 | proxy1end = _geometries.end(); |
---|
| 732 | |
---|
| 733 | //dContact contact; |
---|
| 734 | //Real bounce, velThresh, softness; |
---|
| 735 | |
---|
| 736 | //CollisionInfo collInfo; |
---|
| 737 | |
---|
| 738 | // @@TODO : HAS TO HANDLE TransFormedGeometry and GoemtryOffset |
---|
| 739 | for (proxy1 = _geometries.begin(); proxy1 != proxy1end; ++proxy1) |
---|
| 740 | { |
---|
| 741 | o1 = *proxy1; |
---|
| 742 | const int numc = dCollide(o1->getGeometryID(), otherGeometry->getGeometryID(), 0, &contactGeom, sizeof(dContactGeom)); |
---|
| 743 | if (numc) |
---|
| 744 | { |
---|
| 745 | // Create contact joints if either object is dynamics simulated |
---|
| 746 | // If one is not, then sim will not affect it anyway, it will be fixed |
---|
| 747 | // However if one is enabled, we need the contact joint |
---|
| 748 | if (this->isAwake()) |
---|
| 749 | { |
---|
| 750 | _world->collisionCallback(data, o1->getGeometryID(), otherGeometry->getGeometryID()); |
---|
| 751 | } |
---|
| 752 | |
---|
| 753 | // set return |
---|
| 754 | collided = true; |
---|
| 755 | |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | return collided; |
---|
| 759 | } |
---|
| 760 | //----------------------------------------------------------------------- |
---|
| 761 | bool Body::collide(void* data, Body *otherBody) |
---|
| 762 | { |
---|
| 763 | bool collided = false; |
---|
| 764 | dContactGeom contactGeom; |
---|
| 765 | Geometry *o1, *o2; |
---|
| 766 | GeometryArray::const_iterator proxy1, proxy2, proxy1end, proxy2end; |
---|
| 767 | proxy1end = _geometries.end(); |
---|
| 768 | proxy2end = otherBody->getGeometries()->end(); |
---|
| 769 | |
---|
| 770 | //dContact contact; |
---|
| 771 | //Real bounce, velThresh, softness; |
---|
| 772 | |
---|
| 773 | //CollisionInfo collInfo; |
---|
| 774 | |
---|
| 775 | // @@TODO : HAS TO HANDLE TransFormedGeometry and GoemtryOffset |
---|
| 776 | for (proxy1 = _geometries.begin(); proxy1 != proxy1end; ++proxy1) |
---|
| 777 | { |
---|
| 778 | for (proxy2 = otherBody->getGeometries()->begin(); proxy2 != proxy2end; ++proxy2) |
---|
| 779 | { |
---|
| 780 | o1 = *proxy1; |
---|
| 781 | o2 = *proxy2; |
---|
| 782 | const int numc = dCollide(o1->getGeometryID(), o2->getGeometryID(), 0, &contactGeom, sizeof(dContactGeom)); |
---|
| 783 | if (numc) |
---|
| 784 | { |
---|
| 785 | // Create contact joints if either object is dynamics simulated |
---|
| 786 | // If one is not, then sim will not affect it anyway, it will be fixed |
---|
| 787 | // However if one is enabled, we need the contact joint |
---|
| 788 | if (this->isAwake() || otherBody->isAwake()) |
---|
| 789 | { |
---|
| 790 | |
---|
| 791 | _world->collisionCallback(data, o1->getGeometryID(), o2->getGeometryID()); |
---|
| 792 | |
---|
| 793 | } |
---|
| 794 | // set return |
---|
| 795 | collided = true; |
---|
| 796 | } |
---|
| 797 | } |
---|
| 798 | } |
---|
| 799 | return collided; |
---|
| 800 | } |
---|
| 801 | //------------------------------------------------------------------------- |
---|
| 802 | bool Body::collidePlaneBounds(void* data, Ogre::SceneQuery::WorldFragment *wf) |
---|
| 803 | { |
---|
| 804 | bool collided = false; |
---|
| 805 | dContactGeom contactGeom; |
---|
| 806 | Geometry *geom; |
---|
| 807 | GeometryArray::const_iterator proxy, proxyend; |
---|
| 808 | proxyend = _geometries.end(); |
---|
| 809 | |
---|
| 810 | std::list<Ogre::Plane>::const_iterator pi, piend; |
---|
| 811 | piend = wf->planes->end(); |
---|
| 812 | |
---|
| 813 | //CollisionInfo collInfo; |
---|
| 814 | |
---|
| 815 | // @@TODO : HAS TO HANDLE TransFormedGeometry and GoemtryOffset |
---|
| 816 | for (proxy = _geometries.begin(); proxy != proxyend; ++proxy) |
---|
| 817 | { |
---|
| 818 | // Hack, simply collide against planes which is facing towards center |
---|
| 819 | // We can't do this properly without mesh collision |
---|
| 820 | geom = *proxy; |
---|
| 821 | Ogre::Real maxdist = -1.0f; |
---|
| 822 | const Ogre::Plane* bestPlane = 0; |
---|
| 823 | for (pi = wf->planes->begin(); pi != piend; ++pi) |
---|
| 824 | { |
---|
| 825 | const Ogre::Plane * const boundPlane = &(*pi); |
---|
| 826 | const Ogre::Real dist = boundPlane->getDistance (this->getPosition()); |
---|
| 827 | if (dist >= 0.0f) |
---|
| 828 | { |
---|
| 829 | |
---|
| 830 | //@@@@ TODO reuse a static InfinitePlaneGeometry ? |
---|
| 831 | InfinitePlaneGeometry *sliding_plane = new InfinitePlaneGeometry(*boundPlane, 0); |
---|
| 832 | |
---|
| 833 | |
---|
| 834 | int numc = dCollide(geom->getGeometryID(), |
---|
| 835 | sliding_plane->getGeometryID(), |
---|
| 836 | 0, |
---|
| 837 | &contactGeom, |
---|
| 838 | sizeof(dContactGeom)); |
---|
| 839 | if (numc) |
---|
| 840 | { |
---|
| 841 | // Create contact joints if object is dynamics simulated |
---|
| 842 | if (this->isAwake()) |
---|
| 843 | { |
---|
| 844 | _world->collisionCallback(data, geom->getGeometryID(), sliding_plane->getGeometryID()); |
---|
| 845 | |
---|
| 846 | } |
---|
| 847 | // set return |
---|
| 848 | collided = true; |
---|
| 849 | } |
---|
| 850 | } |
---|
| 851 | } |
---|
| 852 | |
---|
| 853 | } |
---|
| 854 | return collided; |
---|
| 855 | } |
---|
| 856 | |
---|