BsCRigidbody.cpp 11 KB

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