BsCRigidbody.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 "RTTI/BsCRigidbodyRTTI.h"
  8. using namespace std::placeholders;
  9. namespace bs
  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::setCenterOfMassPosition(const Vector3& position)
  132. {
  133. mCMassPosition = position;
  134. if (mInternal != nullptr)
  135. mInternal->setCenterOfMass(position, mCMassRotation);
  136. }
  137. void CRigidbody::setCenterOfMassRotation(const Quaternion& rotation)
  138. {
  139. mCMassRotation = rotation;
  140. if (mInternal != nullptr)
  141. mInternal->setCenterOfMass(mCMassPosition, rotation);
  142. }
  143. Vector3 CRigidbody::getCenterOfMassPosition() const
  144. {
  145. if (mInternal != nullptr)
  146. return mInternal->getCenterOfMassPosition();
  147. return Vector3::ZERO;
  148. }
  149. Quaternion CRigidbody::getCenterOfMassRotation() const
  150. {
  151. if (mInternal != nullptr)
  152. return mInternal->getCenterOfMassRotation();
  153. return Quaternion::IDENTITY;
  154. }
  155. void CRigidbody::setPositionSolverCount(UINT32 count)
  156. {
  157. mPositionSolverCount = count;
  158. if (mInternal != nullptr)
  159. mInternal->setPositionSolverCount(count);
  160. }
  161. void CRigidbody::setVelocitySolverCount(UINT32 count)
  162. {
  163. mVelocitySolverCount = count;
  164. if (mInternal != nullptr)
  165. mInternal->setVelocitySolverCount(count);
  166. }
  167. void CRigidbody::setCollisionReportMode(CollisionReportMode mode)
  168. {
  169. if (mCollisionReportMode == mode)
  170. return;
  171. mCollisionReportMode = mode;
  172. for (auto& entry : mChildren)
  173. entry->updateCollisionReportMode();
  174. }
  175. void CRigidbody::setFlags(RigidbodyFlag flags)
  176. {
  177. mFlags = flags;
  178. if (mInternal != nullptr)
  179. {
  180. mInternal->setFlags(flags);
  181. mInternal->updateMassDistribution();
  182. }
  183. }
  184. void CRigidbody::addForce(const Vector3& force, ForceMode mode)
  185. {
  186. if (mInternal != nullptr)
  187. mInternal->addForce(force, mode);
  188. }
  189. void CRigidbody::addTorque(const Vector3& torque, ForceMode mode)
  190. {
  191. if (mInternal != nullptr)
  192. mInternal->addTorque(torque, mode);
  193. }
  194. void CRigidbody::addForceAtPoint(const Vector3& force, const Vector3& position, PointForceMode mode)
  195. {
  196. if (mInternal != nullptr)
  197. mInternal->addForceAtPoint(force, position, mode);
  198. }
  199. Vector3 CRigidbody::getVelocityAtPoint(const Vector3& point) const
  200. {
  201. if (mInternal != nullptr)
  202. return mInternal->getVelocityAtPoint(point);
  203. return Vector3::ZERO;
  204. }
  205. void CRigidbody::_updateMassDistribution()
  206. {
  207. if (mInternal != nullptr)
  208. return mInternal->updateMassDistribution();
  209. }
  210. void CRigidbody::updateColliders()
  211. {
  212. Stack<HSceneObject> todo;
  213. todo.push(SO());
  214. while(!todo.empty())
  215. {
  216. HSceneObject currentSO = todo.top();
  217. todo.pop();
  218. if(currentSO->hasComponent<CCollider>())
  219. {
  220. Vector<HCollider> colliders = currentSO->getComponents<CCollider>();
  221. for (auto& entry : colliders)
  222. {
  223. if (!entry->isValidParent(mThisHandle))
  224. continue;
  225. Collider* collider = entry->_getInternal();
  226. if (collider == nullptr)
  227. continue;
  228. entry->setRigidbody(mThisHandle, true);
  229. mChildren.push_back(entry);
  230. collider->setRigidbody(mInternal.get());
  231. mInternal->addCollider(collider->_getInternal());
  232. }
  233. }
  234. UINT32 childCount = currentSO->getNumChildren();
  235. for (UINT32 i = 0; i < childCount; i++)
  236. {
  237. HSceneObject child = currentSO->getChild(i);
  238. if (child->hasComponent<CRigidbody>())
  239. continue;
  240. todo.push(child);
  241. }
  242. }
  243. }
  244. void CRigidbody::clearColliders()
  245. {
  246. for (auto& collider : mChildren)
  247. collider->setRigidbody(HRigidbody(), true);
  248. mChildren.clear();
  249. if (mInternal != nullptr)
  250. mInternal->removeColliders();
  251. }
  252. void CRigidbody::addCollider(const HCollider& collider)
  253. {
  254. if (mInternal == nullptr)
  255. return;
  256. mChildren.push_back(collider);
  257. mInternal->addCollider(collider->_getInternal()->_getInternal());
  258. }
  259. void CRigidbody::removeCollider(const HCollider& collider)
  260. {
  261. if (mInternal == nullptr)
  262. return;
  263. auto iterFind = std::find(mChildren.begin(), mChildren.end(), collider);
  264. if(iterFind != mChildren.end())
  265. {
  266. mInternal->removeCollider(collider->_getInternal()->_getInternal());
  267. mChildren.erase(iterFind);
  268. }
  269. }
  270. void CRigidbody::checkForNestedRigibody()
  271. {
  272. HSceneObject currentSO = SO()->getParent();
  273. while(currentSO != nullptr)
  274. {
  275. if(currentSO->hasComponent<CRigidbody>())
  276. {
  277. LOGWRN("Nested Rigidbodies detected. This will result in inconsistent transformations. To parent one " \
  278. "Rigidbody to another move its colliders to the new parent, but remove the Rigidbody component.");
  279. return;
  280. }
  281. currentSO = currentSO->getParent();
  282. }
  283. }
  284. void CRigidbody::processCollisionData(const CollisionDataRaw& data, CollisionData& output)
  285. {
  286. output.contactPoints = std::move(data.contactPoints);
  287. if (data.colliders[0] != nullptr)
  288. {
  289. CCollider* other = (CCollider*)data.colliders[0]->_getOwner(PhysicsOwnerType::Component);
  290. output.collider[0] = other->getHandle();
  291. }
  292. if (data.colliders[1] != nullptr)
  293. {
  294. CCollider* other = (CCollider*)data.colliders[1]->_getOwner(PhysicsOwnerType::Component);
  295. output.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 CollisionDataRaw& data)
  305. {
  306. CollisionData hit;
  307. processCollisionData(data, hit);
  308. onCollisionBegin(hit);
  309. }
  310. void CRigidbody::triggerOnCollisionStay(const CollisionDataRaw& data)
  311. {
  312. CollisionData hit;
  313. processCollisionData(data, hit);
  314. onCollisionStay(hit);
  315. }
  316. void CRigidbody::triggerOnCollisionEnd(const CollisionDataRaw& data)
  317. {
  318. CollisionData hit;
  319. processCollisionData(data, hit);
  320. onCollisionEnd(hit);
  321. }
  322. void CRigidbody::onInitialized()
  323. {
  324. }
  325. void CRigidbody::onDestroyed()
  326. {
  327. destroyInternal();
  328. }
  329. void CRigidbody::onDisabled()
  330. {
  331. destroyInternal();
  332. }
  333. void CRigidbody::onEnabled()
  334. {
  335. mInternal = Rigidbody::create(SO());
  336. mInternal->_setOwner(PhysicsOwnerType::Component, this);
  337. updateColliders();
  338. #if BS_DEBUG_MODE
  339. checkForNestedRigibody();
  340. #endif
  341. mInternal->onCollisionBegin.connect(std::bind(&CRigidbody::triggerOnCollisionBegin, this, _1));
  342. mInternal->onCollisionStay.connect(std::bind(&CRigidbody::triggerOnCollisionStay, this, _1));
  343. mInternal->onCollisionEnd.connect(std::bind(&CRigidbody::triggerOnCollisionEnd, this, _1));
  344. const Transform& tfrm = SO()->getTransform();
  345. mInternal->setTransform(tfrm.getPosition(), tfrm.getRotation());
  346. // Note: Merge into one call to avoid many virtual function calls
  347. mInternal->setPositionSolverCount(mPositionSolverCount);
  348. mInternal->setVelocitySolverCount(mVelocitySolverCount);
  349. mInternal->setMaxAngularVelocity(mMaxAngularVelocity);
  350. mInternal->setDrag(mLinearDrag);
  351. mInternal->setAngularDrag(mAngularDrag);
  352. mInternal->setSleepThreshold(mSleepThreshold);
  353. mInternal->setUseGravity(mUseGravity);
  354. mInternal->setIsKinematic(mIsKinematic);
  355. mInternal->setFlags(mFlags);
  356. if(((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoTensors) == 0)
  357. {
  358. mInternal->setCenterOfMass(mCMassPosition, mCMassRotation);
  359. mInternal->setInertiaTensor(mInertiaTensor);
  360. mInternal->setMass(mMass);
  361. }
  362. else
  363. {
  364. if (((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoMass) == 0)
  365. mInternal->setMass(mMass);
  366. mInternal->updateMassDistribution();
  367. }
  368. }
  369. void CRigidbody::onTransformChanged(TransformChangedFlags flags)
  370. {
  371. if (!SO()->getActive())
  372. return;
  373. if((flags & TCF_Parent) != 0)
  374. {
  375. clearColliders();
  376. updateColliders();
  377. if (((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoTensors) != 0)
  378. mInternal->updateMassDistribution();
  379. #if BS_DEBUG_MODE
  380. checkForNestedRigibody();
  381. #endif
  382. }
  383. const Transform& tfrm = SO()->getTransform();
  384. mInternal->setTransform(tfrm.getPosition(), tfrm.getRotation());
  385. if (mParentJoint != nullptr)
  386. mParentJoint->notifyRigidbodyMoved(mThisHandle);
  387. }
  388. RTTITypeBase* CRigidbody::getRTTIStatic()
  389. {
  390. return CRigidbodyRTTI::instance();
  391. }
  392. RTTITypeBase* CRigidbody::getRTTI() const
  393. {
  394. return CRigidbody::getRTTIStatic();
  395. }
  396. }