BsCRigidbody.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. }
  22. void CRigidbody::rotate(const Quaternion& rotation)
  23. {
  24. if (mInternal != nullptr)
  25. mInternal->rotate(rotation);
  26. }
  27. void CRigidbody::setMass(float mass)
  28. {
  29. mMass = mass;
  30. if(mInternal != nullptr)
  31. mInternal->setMass(mass);
  32. }
  33. void CRigidbody::setIsKinematic(bool kinematic)
  34. {
  35. if (mIsKinematic == kinematic)
  36. return;
  37. mIsKinematic = kinematic;
  38. if (mInternal != nullptr)
  39. {
  40. mInternal->setIsKinematic(kinematic);
  41. clearColliders();
  42. updateColliders();
  43. }
  44. }
  45. bool CRigidbody::isSleeping() const
  46. {
  47. if (mInternal != nullptr)
  48. return mInternal->isSleeping();
  49. return true;
  50. }
  51. void CRigidbody::sleep()
  52. {
  53. if (mInternal != nullptr)
  54. return mInternal->sleep();
  55. }
  56. void CRigidbody::wakeUp()
  57. {
  58. if (mInternal != nullptr)
  59. return mInternal->wakeUp();
  60. }
  61. void CRigidbody::setSleepThreshold(float threshold)
  62. {
  63. mSleepThreshold = threshold;
  64. if (mInternal != nullptr)
  65. mInternal->setSleepThreshold(threshold);
  66. }
  67. void CRigidbody::setUseGravity(bool gravity)
  68. {
  69. mUseGravity = gravity;
  70. if (mInternal != nullptr)
  71. mInternal->setUseGravity(gravity);
  72. }
  73. void CRigidbody::setVelocity(const Vector3& velocity)
  74. {
  75. if (mInternal != nullptr)
  76. mInternal->setVelocity(velocity);
  77. }
  78. Vector3 CRigidbody::getVelocity() const
  79. {
  80. if (mInternal != nullptr)
  81. return mInternal->getVelocity();
  82. return Vector3::ZERO;
  83. }
  84. void CRigidbody::setAngularVelocity(const Vector3& velocity)
  85. {
  86. if (mInternal != nullptr)
  87. mInternal->setAngularVelocity(velocity);
  88. }
  89. inline Vector3 CRigidbody::getAngularVelocity() const
  90. {
  91. if (mInternal != nullptr)
  92. return mInternal->getAngularVelocity();
  93. return Vector3::ZERO;
  94. }
  95. void CRigidbody::setDrag(float drag)
  96. {
  97. mLinearDrag = drag;
  98. if (mInternal != nullptr)
  99. mInternal->setDrag(drag);
  100. }
  101. void CRigidbody::setAngularDrag(float drag)
  102. {
  103. mAngularDrag = drag;
  104. if (mInternal != nullptr)
  105. mInternal->setAngularDrag(drag);
  106. }
  107. void CRigidbody::setInertiaTensor(const Vector3& tensor)
  108. {
  109. mInertiaTensor = tensor;
  110. if (mInternal != nullptr)
  111. mInternal->setInertiaTensor(tensor);
  112. }
  113. Vector3 CRigidbody::getInertiaTensor() const
  114. {
  115. if (mInternal != nullptr)
  116. return mInternal->getInertiaTensor();
  117. return Vector3::ZERO;
  118. }
  119. void CRigidbody::setMaxAngularVelocity(float maxVelocity)
  120. {
  121. mMaxAngularVelocity = maxVelocity;
  122. if (mInternal != nullptr)
  123. mInternal->setMaxAngularVelocity(maxVelocity);
  124. }
  125. void CRigidbody::setCenterOfMass(const Vector3& position, const Quaternion& rotation)
  126. {
  127. mCMassPosition = position;
  128. mCMassRotation = rotation;
  129. if (mInternal != nullptr)
  130. mInternal->setCenterOfMass(position, rotation);
  131. }
  132. Vector3 CRigidbody::getCenterOfMassPosition() const
  133. {
  134. if (mInternal != nullptr)
  135. return mInternal->getCenterOfMassPosition();
  136. return Vector3::ZERO;
  137. }
  138. Quaternion CRigidbody::getCenterOfMassRotation() const
  139. {
  140. if (mInternal != nullptr)
  141. return mInternal->getCenterOfMassRotation();
  142. return Quaternion::IDENTITY;
  143. }
  144. void CRigidbody::setPositionSolverCount(UINT32 count)
  145. {
  146. mPositionSolverCount = count;
  147. if (mInternal != nullptr)
  148. mInternal->setPositionSolverCount(count);
  149. }
  150. void CRigidbody::setVelocitySolverCount(UINT32 count)
  151. {
  152. mVelocitySolverCount = count;
  153. if (mInternal != nullptr)
  154. mInternal->setVelocitySolverCount(count);
  155. }
  156. void CRigidbody::setInterpolationMode(Rigidbody::InterpolationMode value)
  157. {
  158. mInterpolationMode = value;
  159. if (mInternal != nullptr)
  160. mInternal->setInterpolationMode(value);
  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. entry->setRigidbody(mThisHandle, true);
  221. entry->_getInternal()->setRigidbody(mInternal.get());
  222. mChildren.push_back(entry);
  223. mInternal->addCollider(entry->_getInternal()->_getInternal());
  224. }
  225. }
  226. UINT32 childCount = currentSO->getNumChildren();
  227. for (UINT32 i = 0; i < childCount; i++)
  228. {
  229. HSceneObject child = currentSO->getChild(i);
  230. if (child->hasComponent<CRigidbody>())
  231. continue;
  232. todo.push(child);
  233. }
  234. }
  235. }
  236. void CRigidbody::clearColliders()
  237. {
  238. for (auto& collider : mChildren)
  239. collider->setRigidbody(HRigidbody(), true);
  240. mChildren.clear();
  241. if (mInternal != nullptr)
  242. mInternal->removeColliders();
  243. }
  244. void CRigidbody::addCollider(const HCollider& collider)
  245. {
  246. if (mInternal == nullptr)
  247. return;
  248. mChildren.push_back(collider);
  249. mInternal->addCollider(collider->_getInternal()->_getInternal());
  250. }
  251. void CRigidbody::removeCollider(const HCollider& collider)
  252. {
  253. if (mInternal == nullptr)
  254. return;
  255. auto iterFind = std::find(mChildren.begin(), mChildren.end(), collider);
  256. if(iterFind != mChildren.end())
  257. {
  258. mInternal->removeCollider(collider->_getInternal()->_getInternal());
  259. mChildren.erase(iterFind);
  260. }
  261. }
  262. void CRigidbody::checkForNestedRigibody()
  263. {
  264. HSceneObject currentSO = SO()->getParent();
  265. while(currentSO != nullptr)
  266. {
  267. if(currentSO->hasComponent<CRigidbody>())
  268. {
  269. LOGWRN("Nested Rigidbodies detected. This will result in inconsistent transformations. To parent one " \
  270. "Rigidbody to another move its colliders to the new parent, but remove the Rigidbody component.");
  271. return;
  272. }
  273. currentSO = currentSO->getParent();
  274. }
  275. }
  276. void CRigidbody::processCollisionData(CollisionData& data)
  277. {
  278. if (data.collidersRaw[0] != nullptr)
  279. {
  280. CCollider* other = (CCollider*)data.collidersRaw[0]->_getOwner(PhysicsOwnerType::Component);
  281. data.collider[0] = other->getHandle();
  282. }
  283. if (data.collidersRaw[1] != nullptr)
  284. {
  285. CCollider* other = (CCollider*)data.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  286. data.collider[1] = other->getHandle();
  287. }
  288. }
  289. void CRigidbody::destroyInternal()
  290. {
  291. clearColliders();
  292. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  293. mInternal = nullptr;
  294. }
  295. void CRigidbody::triggerOnCollisionBegin(const CollisionData& data)
  296. {
  297. // Const-cast and modify is okay because we're the only object receiving this event
  298. CollisionData& hit = const_cast<CollisionData&>(data);
  299. processCollisionData(hit);
  300. onCollisionBegin(hit);
  301. }
  302. void CRigidbody::triggerOnCollisionStay(const CollisionData& data)
  303. {
  304. // Const-cast and modify is okay because we're the only object receiving this event
  305. CollisionData& hit = const_cast<CollisionData&>(data);
  306. processCollisionData(hit);
  307. onCollisionStay(hit);
  308. }
  309. void CRigidbody::triggerOnCollisionEnd(const CollisionData& data)
  310. {
  311. // Const-cast and modify is okay because we're the only object receiving this event
  312. CollisionData& hit = const_cast<CollisionData&>(data);
  313. processCollisionData(hit);
  314. onCollisionEnd(hit);
  315. }
  316. void CRigidbody::onInitialized()
  317. {
  318. }
  319. void CRigidbody::onDestroyed()
  320. {
  321. destroyInternal();
  322. }
  323. void CRigidbody::onDisabled()
  324. {
  325. destroyInternal();
  326. }
  327. void CRigidbody::onEnabled()
  328. {
  329. mInternal = Rigidbody::create(SO());
  330. mInternal->_setOwner(PhysicsOwnerType::Component, this);
  331. updateColliders();
  332. #if BS_DEBUG_MODE
  333. checkForNestedRigibody();
  334. #endif
  335. mInternal->onCollisionBegin.connect(std::bind(&CRigidbody::triggerOnCollisionBegin, this, _1));
  336. mInternal->onCollisionStay.connect(std::bind(&CRigidbody::triggerOnCollisionStay, this, _1));
  337. mInternal->onCollisionEnd.connect(std::bind(&CRigidbody::triggerOnCollisionEnd, this, _1));
  338. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  339. // Note: Merge into one call to avoid many virtual function calls
  340. mInternal->setPositionSolverCount(mPositionSolverCount);
  341. mInternal->setVelocitySolverCount(mVelocitySolverCount);
  342. mInternal->setMaxAngularVelocity(mMaxAngularVelocity);
  343. mInternal->setDrag(mLinearDrag);
  344. mInternal->setAngularDrag(mAngularDrag);
  345. mInternal->setSleepThreshold(mSleepThreshold);
  346. mInternal->setUseGravity(mUseGravity);
  347. mInternal->setIsKinematic(mIsKinematic);
  348. mInternal->setInterpolationMode(mInterpolationMode);
  349. mInternal->setFlags(mFlags);
  350. if(((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) == 0)
  351. {
  352. mInternal->setCenterOfMass(mCMassPosition, mCMassRotation);
  353. mInternal->setInertiaTensor(mInertiaTensor);
  354. mInternal->setMass(mMass);
  355. }
  356. else
  357. {
  358. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoMass) == 0)
  359. mInternal->setMass(mMass);
  360. mInternal->updateMassDistribution();
  361. }
  362. }
  363. void CRigidbody::onTransformChanged(TransformChangedFlags flags)
  364. {
  365. if (!SO()->getActive())
  366. return;
  367. if((flags & TCF_Parent) != 0)
  368. {
  369. clearColliders();
  370. updateColliders();
  371. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) != 0)
  372. mInternal->updateMassDistribution();
  373. #if BS_DEBUG_MODE
  374. checkForNestedRigibody();
  375. #endif
  376. }
  377. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  378. if (mParentJoint != nullptr)
  379. mParentJoint->notifyRigidbodyMoved(mThisHandle);
  380. }
  381. RTTITypeBase* CRigidbody::getRTTIStatic()
  382. {
  383. return CRigidbodyRTTI::instance();
  384. }
  385. RTTITypeBase* CRigidbody::getRTTI() const
  386. {
  387. return CRigidbody::getRTTIStatic();
  388. }
  389. }