BsCRigidbody.cpp 8.7 KB

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