BsCRigidbody.cpp 11 KB

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