BsCRigidbody.cpp 11 KB

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