BsCRigidbody.cpp 9.7 KB

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