BsCRigidbody.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "BsCMeshCollider.h"
  7. #include "BsPhysicsMesh.h"
  8. #include "BsCRigidbodyRTTI.h"
  9. using namespace std::placeholders;
  10. namespace BansheeEngine
  11. {
  12. CRigidbody::CRigidbody(const HSceneObject& parent)
  13. : Component(parent)
  14. {
  15. setName("Rigidbody");
  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. mIntertiaTensor = 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::setFlags(Rigidbody::Flag flags)
  157. {
  158. mFlags = flags;
  159. if (mInternal != nullptr)
  160. {
  161. mInternal->setFlags(flags);
  162. mInternal->updateMassDistribution();
  163. }
  164. }
  165. void CRigidbody::addForce(const Vector3& force, ForceMode mode)
  166. {
  167. if (mInternal != nullptr)
  168. mInternal->addForce(force, mode);
  169. }
  170. void CRigidbody::addTorque(const Vector3& torque, ForceMode mode)
  171. {
  172. if (mInternal != nullptr)
  173. mInternal->addTorque(torque, mode);
  174. }
  175. void CRigidbody::addForceAtPoint(const Vector3& force, const Vector3& position, PointForceMode mode)
  176. {
  177. if (mInternal != nullptr)
  178. mInternal->addForceAtPoint(force, position, mode);
  179. }
  180. Vector3 CRigidbody::getVelocityAtPoint(const Vector3& point) const
  181. {
  182. if (mInternal != nullptr)
  183. return mInternal->getVelocityAtPoint(point);
  184. return Vector3::ZERO;
  185. }
  186. void CRigidbody::_updateMassDistribution()
  187. {
  188. if (mInternal != nullptr)
  189. return mInternal->updateMassDistribution();
  190. }
  191. void CRigidbody::updateColliders()
  192. {
  193. Stack<HSceneObject> todo;
  194. todo.push(SO());
  195. while(!todo.empty())
  196. {
  197. HSceneObject currentSO = todo.top();
  198. todo.pop();
  199. if(currentSO->hasComponent<CCollider>())
  200. {
  201. Vector<HCollider> colliders = currentSO->getComponents<CCollider>();
  202. for (auto& entry : colliders)
  203. {
  204. if (!entry->isValidParent(mThisHandle))
  205. continue;
  206. entry->setRigidbody(mThisHandle, true);
  207. mChildren.push_back(entry);
  208. mInternal->addCollider(entry->_getInternal()->_getInternal());
  209. }
  210. }
  211. UINT32 childCount = currentSO->getNumChildren();
  212. for (UINT32 i = 0; i < childCount; i++)
  213. {
  214. HSceneObject child = currentSO->getChild(i);
  215. if (child->hasComponent<CRigidbody>())
  216. continue;
  217. todo.push(child);
  218. }
  219. }
  220. }
  221. void CRigidbody::clearColliders()
  222. {
  223. for (auto& collider : mChildren)
  224. collider->setRigidbody(HRigidbody(), true);
  225. mChildren.clear();
  226. if (mInternal != nullptr)
  227. mInternal->removeColliders();
  228. }
  229. void CRigidbody::addCollider(const HCollider& collider)
  230. {
  231. if (mInternal == nullptr)
  232. return;
  233. mChildren.push_back(collider);
  234. mInternal->addCollider(collider->_getInternal()->_getInternal());
  235. }
  236. void CRigidbody::removeCollider(const HCollider& collider)
  237. {
  238. if (mInternal == nullptr)
  239. return;
  240. auto iterFind = std::find(mChildren.begin(), mChildren.end(), collider);
  241. if(iterFind != mChildren.end())
  242. {
  243. mInternal->removeCollider(collider->_getInternal()->_getInternal());
  244. mChildren.erase(iterFind);
  245. }
  246. }
  247. void CRigidbody::checkForNestedRigibody()
  248. {
  249. HSceneObject currentSO = SO()->getParent();
  250. while(currentSO != nullptr)
  251. {
  252. if(currentSO->hasComponent<CRigidbody>())
  253. {
  254. LOGWRN("Nested Rigidbodies detected. This will result in inconsistent transformations. To parent one " \
  255. "Rigidbody to another move its colliders to the new parent, but remove the Rigidbody component.");
  256. return;
  257. }
  258. currentSO = currentSO->getParent();
  259. }
  260. }
  261. void CRigidbody::triggerOnCollisionBegin(const CollisionData& data)
  262. {
  263. onCollisionBegin(data);
  264. }
  265. void CRigidbody::triggerOnCollisionStay(const CollisionData& data)
  266. {
  267. onCollisionStay(data);
  268. }
  269. void CRigidbody::triggerOnCollisionEnd(const CollisionData& data)
  270. {
  271. onCollisionEnd(data);
  272. }
  273. void CRigidbody::onInitialized()
  274. {
  275. }
  276. void CRigidbody::onDestroyed()
  277. {
  278. clearColliders();
  279. mInternal = nullptr;
  280. }
  281. void CRigidbody::onDisabled()
  282. {
  283. clearColliders();
  284. mInternal = nullptr;
  285. }
  286. void CRigidbody::onEnabled()
  287. {
  288. mInternal = Rigidbody::create(SO());
  289. updateColliders();
  290. #if BS_DEBUG_MODE
  291. checkForNestedRigibody();
  292. #endif
  293. mInternal->onCollisionBegin.connect(std::bind(&CRigidbody::triggerOnCollisionBegin, this, _1));
  294. mInternal->onCollisionStay.connect(std::bind(&CRigidbody::triggerOnCollisionStay, this, _1));
  295. mInternal->onCollisionEnd.connect(std::bind(&CRigidbody::triggerOnCollisionEnd, this, _1));
  296. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  297. // Note: Merge into one call to avoid many virtual function calls
  298. mInternal->setPositionSolverCount(mPositionSolverCount);
  299. mInternal->setVelocitySolverCount(mVelocitySolverCount);
  300. mInternal->setMaxAngularVelocity(mMaxAngularVelocity);
  301. mInternal->setDrag(mLinearDrag);
  302. mInternal->setAngularDrag(mAngularDrag);
  303. mInternal->setSleepThreshold(mSleepThreshold);
  304. mInternal->setUseGravity(mUseGravity);
  305. mInternal->setIsKinematic(mIsKinematic);
  306. if(((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) == 0)
  307. {
  308. mInternal->setCenterOfMass(mCMassPosition, mCMassRotation);
  309. mInternal->setInertiaTensor(mIntertiaTensor);
  310. mInternal->setMass(mMass);
  311. }
  312. else
  313. {
  314. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoMass) == 0)
  315. mInternal->setMass(mMass);
  316. mInternal->updateMassDistribution();
  317. }
  318. }
  319. void CRigidbody::onTransformChanged(TransformChangedFlags flags)
  320. {
  321. if (!SO()->getActive())
  322. return;
  323. if((flags & TCF_Parent) != 0)
  324. {
  325. clearColliders();
  326. updateColliders();
  327. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) != 0)
  328. mInternal->updateMassDistribution();
  329. #if BS_DEBUG_MODE
  330. checkForNestedRigibody();
  331. #endif
  332. }
  333. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  334. }
  335. RTTITypeBase* CRigidbody::getRTTIStatic()
  336. {
  337. return CRigidbodyRTTI::instance();
  338. }
  339. RTTITypeBase* CRigidbody::getRTTI() const
  340. {
  341. return CRigidbody::getRTTIStatic();
  342. }
  343. }