00001
00008 #include "SO3Body.h"
00009 #include "..\SO3SceneGraph\SO3NodeScol.h"
00010 #include "..\SO3SceneGraph\SO3Scene.h"
00011
00012 namespace SO3
00013 {
00014
00015 SBody::SBody()
00016 {
00017
00018 }
00019
00020 SBody::SBody(SNode* node, SShape* shape)
00021 {
00022 mScene = node->GetParentScene();
00023 mNode = node;
00024 mShape = shape;
00025 mUpJoint = 0;
00026
00027 mMass = 0.0;
00028
00029 force = Ogre::Vector3::ZERO;
00030 torque = Ogre::Vector3::ZERO;
00031 localForce = Ogre::Vector3::ZERO;
00032 globalForce = Ogre::Vector3::ZERO;
00033 globalLocationForce = Ogre::Vector3::ZERO;
00034 localLocationForce = Ogre::Vector3::ZERO;
00035 constantForce = Ogre::Vector3::ZERO;
00036 constantTorque = Ogre::Vector3::ZERO;
00037
00038 O3Body = new OgreNewt::Body(mScene->GetPhysicsWorld()->GetPhysicWorld(), mShape->GetOgreNewtCollisionPtr());
00039 O3Body->freeze();
00040 O3Body->setAutoSleep(1);
00041
00042
00043 O3Body->setUserData((Ogre::Any) node);
00044
00045
00046 Ogre::Quaternion prevOrientation = node->GetGlobalOrientation();
00047 Ogre::Vector3 prevPosition = node->GetGlobalPosition();
00048
00049
00050 SetPositionOrientation(prevPosition, prevOrientation);
00051
00052 O3Body->setCenterOfMass(mShape->CalculateOffset());
00053
00054
00055 O3Body->attachNode(node->GetOgreSceneNodePointer());
00056
00057
00058 O3Body->setMaterialGroupID(mScene->GetPhysicsWorld()->GetPhysicWorld()->getDefaultMaterialID());
00059
00060
00061
00062
00063 O3Body->setCustomForceAndTorqueCallback<SBody>(&SBody::BodyAddForceCallback, this);
00064 }
00065
00066 SBody::~SBody()
00067 {
00068 RemoveUpJoint();
00069
00070 if(mScene->GetPhysicsWorld()->GetPhysicWorld())
00071 {
00072
00073 const BodyPairMap bodyMapList = mScene->GetPhysicsWorld()->bodyPairMap;
00074 BodyPairMap::const_iterator iBodyPairSearched = bodyMapList.begin();
00075 while (iBodyPairSearched != bodyMapList.end())
00076 {
00077 SBody** bpair = iBodyPairSearched->second;
00078 if ((bpair[0] == this) || (bpair[1] == this))
00079 {
00080 free(bpair);
00081 mScene->GetPhysicsWorld()->bodyPairMap.erase(iBodyPairSearched->first);
00082 }
00083
00084 iBodyPairSearched++;
00085 }
00086
00087
00088 SAFE_DELETE(O3Body);
00089 }
00090
00091 mNode = 0;
00092 mScene = 0;
00093 }
00094
00095 void SBody::BodyAddForceCallback(OgreNewt::Body* body, float timeStep, int threadIndex)
00096 {
00097
00098 Ogre::Real mass;
00099 Ogre::Vector3 inertia;
00100
00101 body->getMassMatrix(mass, inertia);
00102 if(mNode!=NULL)
00103 {
00104 Ogre::Vector3 gravityForce = mNode->GetParentScene()->GetPhysicsWorld()->GetPhysicGravity() * mass;
00105 body->addForce(gravityForce);
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 body->addForce(force);
00122 force = Ogre::Vector3::ZERO;
00123
00124 body->addTorque(constantTorque);
00125
00126 body->addTorque(torque);
00127 torque = Ogre::Vector3::ZERO;
00128
00129 body->addLocalForce(localForce, localLocationForce);
00130 localForce = Ogre::Vector3::ZERO;
00131 localLocationForce = Ogre::Vector3::ZERO;
00132
00133 body->addLocalForce(constantForce, Ogre::Vector3::ZERO);
00134
00135 body->addGlobalForce(globalForce, globalLocationForce);
00136 globalForce = Ogre::Vector3::ZERO;
00137 globalLocationForce = Ogre::Vector3::ZERO;
00138 }
00139
00140 OgreNewt::Body* SBody::getOgreNewtBodyPointer()
00141 {
00142 return O3Body;
00143 }
00144
00145 Ogre::Real SBody::GetMass()
00146 {
00147 return mMass;
00148 }
00149
00150 bool SBody::GetFreezeState()
00151 {
00152 return O3Body->isFreezed();
00153 }
00154
00155 Ogre::Vector3 SBody::GetForce()
00156 {
00157 return force;
00158 }
00159
00160 Ogre::Vector3 SBody::GetTorque()
00161 {
00162 return torque;
00163 }
00164
00165 Ogre::Vector3 SBody::GetLocalForce()
00166 {
00167 return localForce;
00168 }
00169
00170 Ogre::Vector3 SBody::GetGlobalForce()
00171 {
00172 return globalForce;
00173 }
00174
00175 Ogre::Vector3 SBody::GetGlobalLocationForce()
00176 {
00177 return globalLocationForce;
00178 }
00179
00180 Ogre::Vector3 SBody::GetLocalLocationForce()
00181 {
00182 return localLocationForce;
00183 }
00184
00185 void SBody::SetMass(Ogre::Real mass)
00186 {
00187 mMass = mass;
00188 O3Body->setMassMatrix(mMass, mMass * mShape->CalculateInertialMatrix());
00189 }
00190
00191 void SBody::SetMassMatrix(Ogre::Real mass, Ogre::Vector3 inertial)
00192 {
00193 mMass = mass;
00194 O3Body->setMassMatrix(mMass, mMass * inertial);
00195 }
00196
00197
00198 void SBody::SetMaterialID(OgreNewt::MaterialID* mMatID)
00199 {
00200 O3Body->setMaterialGroupID(mMatID);
00201 }
00202
00203 const OgreNewt::MaterialID* SBody::GetMaterialID()
00204 {
00205 return O3Body->getMaterialGroupID();
00206 }
00207
00208 void SBody::SetFreezeState(bool state)
00209 {
00210 if (state)
00211 {
00212 O3Body->freeze();
00213 }
00214 else
00215 {
00216 O3Body->unFreeze();
00217 }
00218 }
00219
00220 void SBody::SetForce(Ogre::Vector3 mForce)
00221 {
00222 force = mForce;
00223 }
00224
00225 void SBody::SetTorque(Ogre::Vector3 mTorque)
00226 {
00227 torque = mTorque;
00228 }
00229
00230 void SBody::SetOmega(Ogre::Vector3 omega)
00231 {
00232 O3Body->setOmega(omega);
00233 }
00234
00235 Ogre::Vector3 SBody::GetOmega()
00236 {
00237 return O3Body->getOmega();
00238 }
00239
00240 void SBody::SetConstantForce(Ogre::Vector3 mForce)
00241 {
00242 constantForce = mForce;
00243 }
00244
00245 void SBody::SetConstantTorque(Ogre::Vector3 mTorque)
00246 {
00247 constantTorque = mTorque;
00248 }
00249
00250 void SBody::AddConstantForce(Ogre::Vector3 mForce)
00251 {
00252 constantForce = constantForce + mForce;
00253 }
00254
00255 void SBody::AddConstantTorque(Ogre::Vector3 mTorque)
00256 {
00257 constantTorque = constantTorque + mTorque;
00258 }
00259
00260 Ogre::Vector3 SBody::GetConstantForce()
00261 {
00262 return constantForce;
00263 }
00264
00265 Ogre::Vector3 SBody::GetConstantTorque()
00266 {
00267 return constantTorque;
00268 }
00269
00270 void SBody::SetLocalForce(Ogre::Vector3 mLocalForce)
00271 {
00272 localForce = mLocalForce;
00273 }
00274
00275 void SBody::SetGlobalForce(Ogre::Vector3 mGlobalForce)
00276 {
00277 globalForce = mGlobalForce;
00278 }
00279
00280 void SBody::SetGlobalLocationForce(Ogre::Vector3 mGlobalLocationForce)
00281 {
00282 globalLocationForce = mGlobalLocationForce;
00283 }
00284
00285 void SBody::SetLocalLocationForce(Ogre::Vector3 mLocalLocationForce)
00286 {
00287 localLocationForce = mLocalLocationForce;
00288 }
00289
00290 void SBody::SetPositionOrientation(Ogre::Vector3 mDerivedPos, Ogre::Quaternion mDerivedOrientation)
00291 {
00292 O3Body->setPositionOrientation(mDerivedPos, mDerivedOrientation);
00293 }
00294
00295 void SBody::UpdatePositionOrientation()
00296 {
00297 O3Body->setPositionOrientationFromNode();
00298 }
00299
00300 void SBody::SetMatrix(Ogre::Vector3 mDerivedPos, Ogre::Quaternion mDerivedOrientation)
00301 {
00302 float matrix[16];
00303 OgreNewt::Converters::QuatPosToMatrix(mDerivedOrientation, mDerivedPos, &matrix[0]);
00304 NewtonBodySetMatrix(O3Body->getNewtonBody(), &matrix[0]);
00305 }
00306
00307 void SBody::SetVelocity(Ogre::Vector3 velocity)
00308 {
00309 O3Body->setVelocity(velocity);
00310 }
00311
00312 Ogre::Vector3 SBody::GetVelocity()
00313 {
00314 return O3Body->getVelocity();
00315 }
00316
00317 void SBody::UpdateMatrix()
00318 {
00319 SetMatrix(mNode->GetGlobalPosition(), mNode->GetGlobalOrientation());
00320 }
00321
00322 void SBody::AddImpulse(Ogre::Vector3 delta, Ogre::Vector3 pos)
00323 {
00324 O3Body->addImpulse(delta, pos);
00325 }
00326
00327 SNode* SBody::GetParentNode()
00328 {
00329 return mNode;
00330 }
00331
00332 void SBody::SetUpJoint(Ogre::Vector3 vec)
00333 {
00334 SAFE_DELETE(mUpJoint);
00335 mUpJoint = new OgreNewt::UpVector(O3Body, vec);
00336 }
00337
00338 void SBody::RemoveUpJoint()
00339 {
00340 SAFE_DELETE(mUpJoint);
00341 }
00342
00343 void SBody::SetAngularDamping(Ogre::Vector3 vec)
00344 {
00345 O3Body->setAngularDamping(vec);
00346 }
00347
00348 Ogre::Vector3 SBody::GetAngularDamping()
00349 {
00350 return O3Body->getAngularDamping();
00351 }
00352
00353 void SBody::SetCenterOfMass(Ogre::Vector3 vec)
00354 {
00355 O3Body->setCenterOfMass(vec);
00356 }
00357
00358 Ogre::Vector3 SBody::GetCenterOfMass()
00359 {
00360 return O3Body->getCenterOfMass();
00361 }
00362
00363 void SBody::SetLinearDamping(Ogre::Real ang)
00364 {
00365 O3Body->setLinearDamping(ang);
00366 }
00367
00368 Ogre::Real SBody::GetLinearDamping()
00369 {
00370 return O3Body->getLinearDamping();
00371 }
00372
00373 void SBody::SetAutoSleep(bool enable)
00374 {
00375 O3Body->setAutoSleep(enable ? 1 : 0);
00376 }
00377
00378 bool SBody::GetAutoSleep()
00379 {
00380 return O3Body->getAutoSleep() ? true : false;
00381 }
00382
00383 void SBody::SetContinuousCollisionMode(bool enable)
00384 {
00385 O3Body->setContinuousCollisionMode(enable ? 1 : 0);
00386 }
00387
00388 bool SBody::GetContinuousCollisionMode()
00389 {
00390 return O3Body->getContinuousCollisionMode() ? true : false;
00391 }
00392
00393 void SBody::GetInvMass(Ogre::Real &mass, Ogre::Vector3 &inertia)
00394 {
00395 O3Body->getInvMass(mass, inertia);
00396 }
00397
00398 void SBody::GetMassMatrix(Ogre::Real &mass, Ogre::Vector3 &inertia)
00399 {
00400 O3Body->getMassMatrix(mass, inertia);
00401 }
00402
00403 bool SBody::GetSleepState()
00404 {
00405
00406 return NewtonBodyGetSleepState(O3Body->getNewtonBody()) ? true : false;
00407 }
00408
00409 void SBody::GetPositionOrientation(Ogre::Vector3 &pos, Ogre::Quaternion &orient)
00410 {
00411 O3Body->getPositionOrientation(pos, orient);
00412 }
00413
00414 void SBody::SetType(int value)
00415 {
00416 O3Body->setType(value);
00417 }
00418
00419 int SBody::GetType()
00420 {
00421 return O3Body->getType();
00422 }
00423
00424 }