BsCRigidbody.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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::processCollisionData(CollisionData& data)
  261. {
  262. if (data.collidersRaw[0] != nullptr)
  263. {
  264. CCollider* other = (CCollider*)data.collidersRaw[0]->_getOwner(PhysicsOwnerType::Component);
  265. data.collider[0] = other->getHandle();
  266. }
  267. if (data.collidersRaw[1] != nullptr)
  268. {
  269. CCollider* other = (CCollider*)data.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  270. data.collider[1] = other->getHandle();
  271. }
  272. }
  273. void CRigidbody::destroyInternal()
  274. {
  275. clearColliders();
  276. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  277. mInternal = nullptr;
  278. }
  279. void CRigidbody::triggerOnCollisionBegin(const CollisionData& data)
  280. {
  281. // Const-cast and modify is okay because we're the only object receiving this event
  282. CollisionData& hit = const_cast<CollisionData&>(data);
  283. processCollisionData(hit);
  284. onCollisionBegin(hit);
  285. }
  286. void CRigidbody::triggerOnCollisionStay(const CollisionData& data)
  287. {
  288. // Const-cast and modify is okay because we're the only object receiving this event
  289. CollisionData& hit = const_cast<CollisionData&>(data);
  290. processCollisionData(hit);
  291. onCollisionStay(hit);
  292. }
  293. void CRigidbody::triggerOnCollisionEnd(const CollisionData& data)
  294. {
  295. // Const-cast and modify is okay because we're the only object receiving this event
  296. CollisionData& hit = const_cast<CollisionData&>(data);
  297. processCollisionData(hit);
  298. onCollisionEnd(hit);
  299. }
  300. void CRigidbody::onInitialized()
  301. {
  302. }
  303. void CRigidbody::onDestroyed()
  304. {
  305. destroyInternal();
  306. }
  307. void CRigidbody::onDisabled()
  308. {
  309. destroyInternal();
  310. }
  311. void CRigidbody::onEnabled()
  312. {
  313. mInternal = Rigidbody::create(SO());
  314. mInternal->_setOwner(PhysicsOwnerType::Component, this);
  315. updateColliders();
  316. #if BS_DEBUG_MODE
  317. checkForNestedRigibody();
  318. #endif
  319. mInternal->onCollisionBegin.connect(std::bind(&CRigidbody::triggerOnCollisionBegin, this, _1));
  320. mInternal->onCollisionStay.connect(std::bind(&CRigidbody::triggerOnCollisionStay, this, _1));
  321. mInternal->onCollisionEnd.connect(std::bind(&CRigidbody::triggerOnCollisionEnd, this, _1));
  322. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  323. // Note: Merge into one call to avoid many virtual function calls
  324. mInternal->setPositionSolverCount(mPositionSolverCount);
  325. mInternal->setVelocitySolverCount(mVelocitySolverCount);
  326. mInternal->setMaxAngularVelocity(mMaxAngularVelocity);
  327. mInternal->setDrag(mLinearDrag);
  328. mInternal->setAngularDrag(mAngularDrag);
  329. mInternal->setSleepThreshold(mSleepThreshold);
  330. mInternal->setUseGravity(mUseGravity);
  331. mInternal->setIsKinematic(mIsKinematic);
  332. if(((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) == 0)
  333. {
  334. mInternal->setCenterOfMass(mCMassPosition, mCMassRotation);
  335. mInternal->setInertiaTensor(mIntertiaTensor);
  336. mInternal->setMass(mMass);
  337. }
  338. else
  339. {
  340. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoMass) == 0)
  341. mInternal->setMass(mMass);
  342. mInternal->updateMassDistribution();
  343. }
  344. }
  345. void CRigidbody::onTransformChanged(TransformChangedFlags flags)
  346. {
  347. if (!SO()->getActive())
  348. return;
  349. if((flags & TCF_Parent) != 0)
  350. {
  351. clearColliders();
  352. updateColliders();
  353. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) != 0)
  354. mInternal->updateMassDistribution();
  355. #if BS_DEBUG_MODE
  356. checkForNestedRigibody();
  357. #endif
  358. }
  359. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  360. if (mParentJoint != nullptr)
  361. mParentJoint->notifyRigidbodyMoved(mThisHandle);
  362. }
  363. RTTITypeBase* CRigidbody::getRTTIStatic()
  364. {
  365. return CRigidbodyRTTI::instance();
  366. }
  367. RTTITypeBase* CRigidbody::getRTTI() const
  368. {
  369. return CRigidbody::getRTTIStatic();
  370. }
  371. }