BsCRigidbody.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsCRigidbody.h"
  4. #include "BsSceneObject.h"
  5. #include "BsCCollider.h"
  6. #include "BsCJoint.h"
  7. #include "BsCRigidbodyRTTI.h"
  8. using namespace std::placeholders;
  9. namespace BansheeEngine
  10. {
  11. CRigidbody::CRigidbody(const HSceneObject& parent)
  12. : Component(parent)
  13. {
  14. setName("Rigidbody");
  15. mNotifyFlags = (TransformChangedFlags)(TCF_Parent | TCF_Transform);
  16. }
  17. void CRigidbody::move(const Vector3& position)
  18. {
  19. if (mInternal != nullptr)
  20. mInternal->move(position);
  21. mNotifyFlags = (TransformChangedFlags)0;
  22. SO()->setWorldPosition(position);
  23. mNotifyFlags = (TransformChangedFlags)(TCF_Parent | TCF_Transform);
  24. }
  25. void CRigidbody::rotate(const Quaternion& rotation)
  26. {
  27. if (mInternal != nullptr)
  28. mInternal->rotate(rotation);
  29. mNotifyFlags = (TransformChangedFlags)0;
  30. SO()->setWorldRotation(rotation);
  31. mNotifyFlags = (TransformChangedFlags)(TCF_Parent | TCF_Transform);
  32. }
  33. void CRigidbody::setMass(float mass)
  34. {
  35. mMass = mass;
  36. if(mInternal != nullptr)
  37. mInternal->setMass(mass);
  38. }
  39. void CRigidbody::setIsKinematic(bool kinematic)
  40. {
  41. if (mIsKinematic == kinematic)
  42. return;
  43. mIsKinematic = kinematic;
  44. if (mInternal != nullptr)
  45. {
  46. mInternal->setIsKinematic(kinematic);
  47. clearColliders();
  48. updateColliders();
  49. }
  50. }
  51. bool CRigidbody::isSleeping() const
  52. {
  53. if (mInternal != nullptr)
  54. return mInternal->isSleeping();
  55. return true;
  56. }
  57. void CRigidbody::sleep()
  58. {
  59. if (mInternal != nullptr)
  60. return mInternal->sleep();
  61. }
  62. void CRigidbody::wakeUp()
  63. {
  64. if (mInternal != nullptr)
  65. return mInternal->wakeUp();
  66. }
  67. void CRigidbody::setSleepThreshold(float threshold)
  68. {
  69. mSleepThreshold = threshold;
  70. if (mInternal != nullptr)
  71. mInternal->setSleepThreshold(threshold);
  72. }
  73. void CRigidbody::setUseGravity(bool gravity)
  74. {
  75. mUseGravity = gravity;
  76. if (mInternal != nullptr)
  77. mInternal->setUseGravity(gravity);
  78. }
  79. void CRigidbody::setVelocity(const Vector3& velocity)
  80. {
  81. if (mInternal != nullptr)
  82. mInternal->setVelocity(velocity);
  83. }
  84. Vector3 CRigidbody::getVelocity() const
  85. {
  86. if (mInternal != nullptr)
  87. return mInternal->getVelocity();
  88. return Vector3::ZERO;
  89. }
  90. void CRigidbody::setAngularVelocity(const Vector3& velocity)
  91. {
  92. if (mInternal != nullptr)
  93. mInternal->setAngularVelocity(velocity);
  94. }
  95. inline Vector3 CRigidbody::getAngularVelocity() const
  96. {
  97. if (mInternal != nullptr)
  98. return mInternal->getAngularVelocity();
  99. return Vector3::ZERO;
  100. }
  101. void CRigidbody::setDrag(float drag)
  102. {
  103. mLinearDrag = drag;
  104. if (mInternal != nullptr)
  105. mInternal->setDrag(drag);
  106. }
  107. void CRigidbody::setAngularDrag(float drag)
  108. {
  109. mAngularDrag = drag;
  110. if (mInternal != nullptr)
  111. mInternal->setAngularDrag(drag);
  112. }
  113. void CRigidbody::setInertiaTensor(const Vector3& tensor)
  114. {
  115. mInertiaTensor = tensor;
  116. if (mInternal != nullptr)
  117. mInternal->setInertiaTensor(tensor);
  118. }
  119. Vector3 CRigidbody::getInertiaTensor() const
  120. {
  121. if (mInternal != nullptr)
  122. return mInternal->getInertiaTensor();
  123. return Vector3::ZERO;
  124. }
  125. void CRigidbody::setMaxAngularVelocity(float maxVelocity)
  126. {
  127. mMaxAngularVelocity = maxVelocity;
  128. if (mInternal != nullptr)
  129. mInternal->setMaxAngularVelocity(maxVelocity);
  130. }
  131. void CRigidbody::setCenterOfMass(const Vector3& position, const Quaternion& rotation)
  132. {
  133. mCMassPosition = position;
  134. mCMassRotation = rotation;
  135. if (mInternal != nullptr)
  136. mInternal->setCenterOfMass(position, rotation);
  137. }
  138. Vector3 CRigidbody::getCenterOfMassPosition() const
  139. {
  140. if (mInternal != nullptr)
  141. return mInternal->getCenterOfMassPosition();
  142. return Vector3::ZERO;
  143. }
  144. Quaternion CRigidbody::getCenterOfMassRotation() const
  145. {
  146. if (mInternal != nullptr)
  147. return mInternal->getCenterOfMassRotation();
  148. return Quaternion::IDENTITY;
  149. }
  150. void CRigidbody::setPositionSolverCount(UINT32 count)
  151. {
  152. mPositionSolverCount = count;
  153. if (mInternal != nullptr)
  154. mInternal->setPositionSolverCount(count);
  155. }
  156. void CRigidbody::setVelocitySolverCount(UINT32 count)
  157. {
  158. mVelocitySolverCount = count;
  159. if (mInternal != nullptr)
  160. mInternal->setVelocitySolverCount(count);
  161. }
  162. void CRigidbody::setInterpolationMode(Rigidbody::InterpolationMode value)
  163. {
  164. mInterpolationMode = value;
  165. if (mInternal != nullptr)
  166. mInternal->setInterpolationMode(value);
  167. }
  168. void CRigidbody::setCollisionReportMode(CollisionReportMode mode)
  169. {
  170. if (mCollisionReportMode == mode)
  171. return;
  172. mCollisionReportMode = mode;
  173. for (auto& entry : mChildren)
  174. entry->updateCollisionReportMode();
  175. }
  176. void CRigidbody::setFlags(Rigidbody::Flag flags)
  177. {
  178. mFlags = flags;
  179. if (mInternal != nullptr)
  180. {
  181. mInternal->setFlags(flags);
  182. mInternal->updateMassDistribution();
  183. }
  184. }
  185. void CRigidbody::addForce(const Vector3& force, ForceMode mode)
  186. {
  187. if (mInternal != nullptr)
  188. mInternal->addForce(force, mode);
  189. }
  190. void CRigidbody::addTorque(const Vector3& torque, ForceMode mode)
  191. {
  192. if (mInternal != nullptr)
  193. mInternal->addTorque(torque, mode);
  194. }
  195. void CRigidbody::addForceAtPoint(const Vector3& force, const Vector3& position, PointForceMode mode)
  196. {
  197. if (mInternal != nullptr)
  198. mInternal->addForceAtPoint(force, position, mode);
  199. }
  200. Vector3 CRigidbody::getVelocityAtPoint(const Vector3& point) const
  201. {
  202. if (mInternal != nullptr)
  203. return mInternal->getVelocityAtPoint(point);
  204. return Vector3::ZERO;
  205. }
  206. void CRigidbody::_updateMassDistribution()
  207. {
  208. if (mInternal != nullptr)
  209. return mInternal->updateMassDistribution();
  210. }
  211. void CRigidbody::updateColliders()
  212. {
  213. Stack<HSceneObject> todo;
  214. todo.push(SO());
  215. while(!todo.empty())
  216. {
  217. HSceneObject currentSO = todo.top();
  218. todo.pop();
  219. if(currentSO->hasComponent<CCollider>())
  220. {
  221. Vector<HCollider> colliders = currentSO->getComponents<CCollider>();
  222. for (auto& entry : colliders)
  223. {
  224. if (!entry->isValidParent(mThisHandle))
  225. continue;
  226. Collider* collider = entry->_getInternal();
  227. if (collider == nullptr)
  228. continue;
  229. entry->setRigidbody(mThisHandle, true);
  230. mChildren.push_back(entry);
  231. collider->setRigidbody(mInternal.get());
  232. mInternal->addCollider(collider->_getInternal());
  233. }
  234. }
  235. UINT32 childCount = currentSO->getNumChildren();
  236. for (UINT32 i = 0; i < childCount; i++)
  237. {
  238. HSceneObject child = currentSO->getChild(i);
  239. if (child->hasComponent<CRigidbody>())
  240. continue;
  241. todo.push(child);
  242. }
  243. }
  244. }
  245. void CRigidbody::clearColliders()
  246. {
  247. for (auto& collider : mChildren)
  248. collider->setRigidbody(HRigidbody(), true);
  249. mChildren.clear();
  250. if (mInternal != nullptr)
  251. mInternal->removeColliders();
  252. }
  253. void CRigidbody::addCollider(const HCollider& collider)
  254. {
  255. if (mInternal == nullptr)
  256. return;
  257. mChildren.push_back(collider);
  258. mInternal->addCollider(collider->_getInternal()->_getInternal());
  259. }
  260. void CRigidbody::removeCollider(const HCollider& collider)
  261. {
  262. if (mInternal == nullptr)
  263. return;
  264. auto iterFind = std::find(mChildren.begin(), mChildren.end(), collider);
  265. if(iterFind != mChildren.end())
  266. {
  267. mInternal->removeCollider(collider->_getInternal()->_getInternal());
  268. mChildren.erase(iterFind);
  269. }
  270. }
  271. void CRigidbody::checkForNestedRigibody()
  272. {
  273. HSceneObject currentSO = SO()->getParent();
  274. while(currentSO != nullptr)
  275. {
  276. if(currentSO->hasComponent<CRigidbody>())
  277. {
  278. LOGWRN("Nested Rigidbodies detected. This will result in inconsistent transformations. To parent one " \
  279. "Rigidbody to another move its colliders to the new parent, but remove the Rigidbody component.");
  280. return;
  281. }
  282. currentSO = currentSO->getParent();
  283. }
  284. }
  285. void CRigidbody::processCollisionData(CollisionData& data)
  286. {
  287. if (data.collidersRaw[0] != nullptr)
  288. {
  289. CCollider* other = (CCollider*)data.collidersRaw[0]->_getOwner(PhysicsOwnerType::Component);
  290. data.collider[0] = other->getHandle();
  291. }
  292. if (data.collidersRaw[1] != nullptr)
  293. {
  294. CCollider* other = (CCollider*)data.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  295. data.collider[1] = other->getHandle();
  296. }
  297. }
  298. void CRigidbody::destroyInternal()
  299. {
  300. clearColliders();
  301. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  302. mInternal = nullptr;
  303. }
  304. void CRigidbody::triggerOnCollisionBegin(const CollisionData& data)
  305. {
  306. // Const-cast and modify is okay because we're the only object receiving this event
  307. CollisionData& hit = const_cast<CollisionData&>(data);
  308. processCollisionData(hit);
  309. onCollisionBegin(hit);
  310. }
  311. void CRigidbody::triggerOnCollisionStay(const CollisionData& data)
  312. {
  313. // Const-cast and modify is okay because we're the only object receiving this event
  314. CollisionData& hit = const_cast<CollisionData&>(data);
  315. processCollisionData(hit);
  316. onCollisionStay(hit);
  317. }
  318. void CRigidbody::triggerOnCollisionEnd(const CollisionData& data)
  319. {
  320. // Const-cast and modify is okay because we're the only object receiving this event
  321. CollisionData& hit = const_cast<CollisionData&>(data);
  322. processCollisionData(hit);
  323. onCollisionEnd(hit);
  324. }
  325. void CRigidbody::onInitialized()
  326. {
  327. }
  328. void CRigidbody::onDestroyed()
  329. {
  330. destroyInternal();
  331. }
  332. void CRigidbody::onDisabled()
  333. {
  334. destroyInternal();
  335. }
  336. void CRigidbody::onEnabled()
  337. {
  338. mInternal = Rigidbody::create(SO());
  339. mInternal->_setOwner(PhysicsOwnerType::Component, this);
  340. updateColliders();
  341. #if BS_DEBUG_MODE
  342. checkForNestedRigibody();
  343. #endif
  344. mInternal->onCollisionBegin.connect(std::bind(&CRigidbody::triggerOnCollisionBegin, this, _1));
  345. mInternal->onCollisionStay.connect(std::bind(&CRigidbody::triggerOnCollisionStay, this, _1));
  346. mInternal->onCollisionEnd.connect(std::bind(&CRigidbody::triggerOnCollisionEnd, this, _1));
  347. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  348. // Note: Merge into one call to avoid many virtual function calls
  349. mInternal->setPositionSolverCount(mPositionSolverCount);
  350. mInternal->setVelocitySolverCount(mVelocitySolverCount);
  351. mInternal->setMaxAngularVelocity(mMaxAngularVelocity);
  352. mInternal->setDrag(mLinearDrag);
  353. mInternal->setAngularDrag(mAngularDrag);
  354. mInternal->setSleepThreshold(mSleepThreshold);
  355. mInternal->setUseGravity(mUseGravity);
  356. mInternal->setIsKinematic(mIsKinematic);
  357. mInternal->setInterpolationMode(mInterpolationMode);
  358. mInternal->setFlags(mFlags);
  359. if(((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) == 0)
  360. {
  361. mInternal->setCenterOfMass(mCMassPosition, mCMassRotation);
  362. mInternal->setInertiaTensor(mInertiaTensor);
  363. mInternal->setMass(mMass);
  364. }
  365. else
  366. {
  367. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoMass) == 0)
  368. mInternal->setMass(mMass);
  369. mInternal->updateMassDistribution();
  370. }
  371. }
  372. void CRigidbody::onTransformChanged(TransformChangedFlags flags)
  373. {
  374. if (!SO()->getActive())
  375. return;
  376. if((flags & TCF_Parent) != 0)
  377. {
  378. clearColliders();
  379. updateColliders();
  380. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) != 0)
  381. mInternal->updateMassDistribution();
  382. #if BS_DEBUG_MODE
  383. checkForNestedRigibody();
  384. #endif
  385. }
  386. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  387. if (mParentJoint != nullptr)
  388. mParentJoint->notifyRigidbodyMoved(mThisHandle);
  389. }
  390. RTTITypeBase* CRigidbody::getRTTIStatic()
  391. {
  392. return CRigidbodyRTTI::instance();
  393. }
  394. RTTITypeBase* CRigidbody::getRTTI() const
  395. {
  396. return CRigidbody::getRTTIStatic();
  397. }
  398. }