BsCRigidbody.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. mChildren.push_back(entry);
  222. mInternal->addCollider(entry->_getInternal()->_getInternal());
  223. }
  224. }
  225. UINT32 childCount = currentSO->getNumChildren();
  226. for (UINT32 i = 0; i < childCount; i++)
  227. {
  228. HSceneObject child = currentSO->getChild(i);
  229. if (child->hasComponent<CRigidbody>())
  230. continue;
  231. todo.push(child);
  232. }
  233. }
  234. }
  235. void CRigidbody::clearColliders()
  236. {
  237. for (auto& collider : mChildren)
  238. collider->setRigidbody(HRigidbody(), true);
  239. mChildren.clear();
  240. if (mInternal != nullptr)
  241. mInternal->removeColliders();
  242. }
  243. void CRigidbody::addCollider(const HCollider& collider)
  244. {
  245. if (mInternal == nullptr)
  246. return;
  247. mChildren.push_back(collider);
  248. mInternal->addCollider(collider->_getInternal()->_getInternal());
  249. }
  250. void CRigidbody::removeCollider(const HCollider& collider)
  251. {
  252. if (mInternal == nullptr)
  253. return;
  254. auto iterFind = std::find(mChildren.begin(), mChildren.end(), collider);
  255. if(iterFind != mChildren.end())
  256. {
  257. mInternal->removeCollider(collider->_getInternal()->_getInternal());
  258. mChildren.erase(iterFind);
  259. }
  260. }
  261. void CRigidbody::checkForNestedRigibody()
  262. {
  263. HSceneObject currentSO = SO()->getParent();
  264. while(currentSO != nullptr)
  265. {
  266. if(currentSO->hasComponent<CRigidbody>())
  267. {
  268. LOGWRN("Nested Rigidbodies detected. This will result in inconsistent transformations. To parent one " \
  269. "Rigidbody to another move its colliders to the new parent, but remove the Rigidbody component.");
  270. return;
  271. }
  272. currentSO = currentSO->getParent();
  273. }
  274. }
  275. void CRigidbody::processCollisionData(CollisionData& data)
  276. {
  277. if (data.collidersRaw[0] != nullptr)
  278. {
  279. CCollider* other = (CCollider*)data.collidersRaw[0]->_getOwner(PhysicsOwnerType::Component);
  280. data.collider[0] = other->getHandle();
  281. }
  282. if (data.collidersRaw[1] != nullptr)
  283. {
  284. CCollider* other = (CCollider*)data.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  285. data.collider[1] = other->getHandle();
  286. }
  287. }
  288. void CRigidbody::destroyInternal()
  289. {
  290. clearColliders();
  291. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  292. mInternal = nullptr;
  293. }
  294. void CRigidbody::triggerOnCollisionBegin(const CollisionData& data)
  295. {
  296. // Const-cast and modify is okay because we're the only object receiving this event
  297. CollisionData& hit = const_cast<CollisionData&>(data);
  298. processCollisionData(hit);
  299. onCollisionBegin(hit);
  300. }
  301. void CRigidbody::triggerOnCollisionStay(const CollisionData& data)
  302. {
  303. // Const-cast and modify is okay because we're the only object receiving this event
  304. CollisionData& hit = const_cast<CollisionData&>(data);
  305. processCollisionData(hit);
  306. onCollisionStay(hit);
  307. }
  308. void CRigidbody::triggerOnCollisionEnd(const CollisionData& data)
  309. {
  310. // Const-cast and modify is okay because we're the only object receiving this event
  311. CollisionData& hit = const_cast<CollisionData&>(data);
  312. processCollisionData(hit);
  313. onCollisionEnd(hit);
  314. }
  315. void CRigidbody::onInitialized()
  316. {
  317. }
  318. void CRigidbody::onDestroyed()
  319. {
  320. destroyInternal();
  321. }
  322. void CRigidbody::onDisabled()
  323. {
  324. destroyInternal();
  325. }
  326. void CRigidbody::onEnabled()
  327. {
  328. mInternal = Rigidbody::create(SO());
  329. mInternal->_setOwner(PhysicsOwnerType::Component, this);
  330. updateColliders();
  331. #if BS_DEBUG_MODE
  332. checkForNestedRigibody();
  333. #endif
  334. mInternal->onCollisionBegin.connect(std::bind(&CRigidbody::triggerOnCollisionBegin, this, _1));
  335. mInternal->onCollisionStay.connect(std::bind(&CRigidbody::triggerOnCollisionStay, this, _1));
  336. mInternal->onCollisionEnd.connect(std::bind(&CRigidbody::triggerOnCollisionEnd, this, _1));
  337. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  338. // Note: Merge into one call to avoid many virtual function calls
  339. mInternal->setPositionSolverCount(mPositionSolverCount);
  340. mInternal->setVelocitySolverCount(mVelocitySolverCount);
  341. mInternal->setMaxAngularVelocity(mMaxAngularVelocity);
  342. mInternal->setDrag(mLinearDrag);
  343. mInternal->setAngularDrag(mAngularDrag);
  344. mInternal->setSleepThreshold(mSleepThreshold);
  345. mInternal->setUseGravity(mUseGravity);
  346. mInternal->setIsKinematic(mIsKinematic);
  347. mInternal->setInterpolationMode(mInterpolationMode);
  348. mInternal->setFlags(mFlags);
  349. if(((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) == 0)
  350. {
  351. mInternal->setCenterOfMass(mCMassPosition, mCMassRotation);
  352. mInternal->setInertiaTensor(mInertiaTensor);
  353. mInternal->setMass(mMass);
  354. }
  355. else
  356. {
  357. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoMass) == 0)
  358. mInternal->setMass(mMass);
  359. mInternal->updateMassDistribution();
  360. }
  361. }
  362. void CRigidbody::onTransformChanged(TransformChangedFlags flags)
  363. {
  364. if (!SO()->getActive())
  365. return;
  366. if((flags & TCF_Parent) != 0)
  367. {
  368. clearColliders();
  369. updateColliders();
  370. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) != 0)
  371. mInternal->updateMassDistribution();
  372. #if BS_DEBUG_MODE
  373. checkForNestedRigibody();
  374. #endif
  375. }
  376. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  377. if (mParentJoint != nullptr)
  378. mParentJoint->notifyRigidbodyMoved(mThisHandle);
  379. }
  380. RTTITypeBase* CRigidbody::getRTTIStatic()
  381. {
  382. return CRigidbodyRTTI::instance();
  383. }
  384. RTTITypeBase* CRigidbody::getRTTI() const
  385. {
  386. return CRigidbody::getRTTIStatic();
  387. }
  388. }