BsCRigidbody.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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::setInterpolationMode(Rigidbody::InterpolationMode value)
  156. {
  157. mInterpolationMode = value;
  158. if (mInternal != nullptr)
  159. mInternal->setInterpolationMode(value);
  160. }
  161. void CRigidbody::setCollisionReportMode(CollisionReportMode mode)
  162. {
  163. if (mCollisionReportMode == mode)
  164. return;
  165. mCollisionReportMode = mode;
  166. for (auto& entry : mChildren)
  167. entry->updateCollisionReportMode();
  168. }
  169. void CRigidbody::setFlags(Rigidbody::Flag flags)
  170. {
  171. mFlags = flags;
  172. if (mInternal != nullptr)
  173. {
  174. mInternal->setFlags(flags);
  175. mInternal->updateMassDistribution();
  176. }
  177. }
  178. void CRigidbody::addForce(const Vector3& force, ForceMode mode)
  179. {
  180. if (mInternal != nullptr)
  181. mInternal->addForce(force, mode);
  182. }
  183. void CRigidbody::addTorque(const Vector3& torque, ForceMode mode)
  184. {
  185. if (mInternal != nullptr)
  186. mInternal->addTorque(torque, mode);
  187. }
  188. void CRigidbody::addForceAtPoint(const Vector3& force, const Vector3& position, PointForceMode mode)
  189. {
  190. if (mInternal != nullptr)
  191. mInternal->addForceAtPoint(force, position, mode);
  192. }
  193. Vector3 CRigidbody::getVelocityAtPoint(const Vector3& point) const
  194. {
  195. if (mInternal != nullptr)
  196. return mInternal->getVelocityAtPoint(point);
  197. return Vector3::ZERO;
  198. }
  199. void CRigidbody::_updateMassDistribution()
  200. {
  201. if (mInternal != nullptr)
  202. return mInternal->updateMassDistribution();
  203. }
  204. void CRigidbody::updateColliders()
  205. {
  206. Stack<HSceneObject> todo;
  207. todo.push(SO());
  208. while(!todo.empty())
  209. {
  210. HSceneObject currentSO = todo.top();
  211. todo.pop();
  212. if(currentSO->hasComponent<CCollider>())
  213. {
  214. Vector<HCollider> colliders = currentSO->getComponents<CCollider>();
  215. for (auto& entry : colliders)
  216. {
  217. if (!entry->isValidParent(mThisHandle))
  218. continue;
  219. entry->setRigidbody(mThisHandle, true);
  220. mChildren.push_back(entry);
  221. mInternal->addCollider(entry->_getInternal()->_getInternal());
  222. }
  223. }
  224. UINT32 childCount = currentSO->getNumChildren();
  225. for (UINT32 i = 0; i < childCount; i++)
  226. {
  227. HSceneObject child = currentSO->getChild(i);
  228. if (child->hasComponent<CRigidbody>())
  229. continue;
  230. todo.push(child);
  231. }
  232. }
  233. }
  234. void CRigidbody::clearColliders()
  235. {
  236. for (auto& collider : mChildren)
  237. collider->setRigidbody(HRigidbody(), true);
  238. mChildren.clear();
  239. if (mInternal != nullptr)
  240. mInternal->removeColliders();
  241. }
  242. void CRigidbody::addCollider(const HCollider& collider)
  243. {
  244. if (mInternal == nullptr)
  245. return;
  246. mChildren.push_back(collider);
  247. mInternal->addCollider(collider->_getInternal()->_getInternal());
  248. }
  249. void CRigidbody::removeCollider(const HCollider& collider)
  250. {
  251. if (mInternal == nullptr)
  252. return;
  253. auto iterFind = std::find(mChildren.begin(), mChildren.end(), collider);
  254. if(iterFind != mChildren.end())
  255. {
  256. mInternal->removeCollider(collider->_getInternal()->_getInternal());
  257. mChildren.erase(iterFind);
  258. }
  259. }
  260. void CRigidbody::checkForNestedRigibody()
  261. {
  262. HSceneObject currentSO = SO()->getParent();
  263. while(currentSO != nullptr)
  264. {
  265. if(currentSO->hasComponent<CRigidbody>())
  266. {
  267. LOGWRN("Nested Rigidbodies detected. This will result in inconsistent transformations. To parent one " \
  268. "Rigidbody to another move its colliders to the new parent, but remove the Rigidbody component.");
  269. return;
  270. }
  271. currentSO = currentSO->getParent();
  272. }
  273. }
  274. void CRigidbody::processCollisionData(CollisionData& data)
  275. {
  276. if (data.collidersRaw[0] != nullptr)
  277. {
  278. CCollider* other = (CCollider*)data.collidersRaw[0]->_getOwner(PhysicsOwnerType::Component);
  279. data.collider[0] = other->getHandle();
  280. }
  281. if (data.collidersRaw[1] != nullptr)
  282. {
  283. CCollider* other = (CCollider*)data.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  284. data.collider[1] = other->getHandle();
  285. }
  286. }
  287. void CRigidbody::destroyInternal()
  288. {
  289. clearColliders();
  290. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  291. mInternal = nullptr;
  292. }
  293. void CRigidbody::triggerOnCollisionBegin(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. onCollisionBegin(hit);
  299. }
  300. void CRigidbody::triggerOnCollisionStay(const CollisionData& data)
  301. {
  302. // Const-cast and modify is okay because we're the only object receiving this event
  303. CollisionData& hit = const_cast<CollisionData&>(data);
  304. processCollisionData(hit);
  305. onCollisionStay(hit);
  306. }
  307. void CRigidbody::triggerOnCollisionEnd(const CollisionData& data)
  308. {
  309. // Const-cast and modify is okay because we're the only object receiving this event
  310. CollisionData& hit = const_cast<CollisionData&>(data);
  311. processCollisionData(hit);
  312. onCollisionEnd(hit);
  313. }
  314. void CRigidbody::onInitialized()
  315. {
  316. }
  317. void CRigidbody::onDestroyed()
  318. {
  319. destroyInternal();
  320. }
  321. void CRigidbody::onDisabled()
  322. {
  323. destroyInternal();
  324. }
  325. void CRigidbody::onEnabled()
  326. {
  327. mInternal = Rigidbody::create(SO());
  328. mInternal->_setOwner(PhysicsOwnerType::Component, this);
  329. updateColliders();
  330. #if BS_DEBUG_MODE
  331. checkForNestedRigibody();
  332. #endif
  333. mInternal->onCollisionBegin.connect(std::bind(&CRigidbody::triggerOnCollisionBegin, this, _1));
  334. mInternal->onCollisionStay.connect(std::bind(&CRigidbody::triggerOnCollisionStay, this, _1));
  335. mInternal->onCollisionEnd.connect(std::bind(&CRigidbody::triggerOnCollisionEnd, this, _1));
  336. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  337. // Note: Merge into one call to avoid many virtual function calls
  338. mInternal->setPositionSolverCount(mPositionSolverCount);
  339. mInternal->setVelocitySolverCount(mVelocitySolverCount);
  340. mInternal->setMaxAngularVelocity(mMaxAngularVelocity);
  341. mInternal->setDrag(mLinearDrag);
  342. mInternal->setAngularDrag(mAngularDrag);
  343. mInternal->setSleepThreshold(mSleepThreshold);
  344. mInternal->setUseGravity(mUseGravity);
  345. mInternal->setIsKinematic(mIsKinematic);
  346. mInternal->setInterpolationMode(mInterpolationMode);
  347. mInternal->setFlags(mFlags);
  348. if(((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) == 0)
  349. {
  350. mInternal->setCenterOfMass(mCMassPosition, mCMassRotation);
  351. mInternal->setInertiaTensor(mIntertiaTensor);
  352. mInternal->setMass(mMass);
  353. }
  354. else
  355. {
  356. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoMass) == 0)
  357. mInternal->setMass(mMass);
  358. mInternal->updateMassDistribution();
  359. }
  360. }
  361. void CRigidbody::onTransformChanged(TransformChangedFlags flags)
  362. {
  363. if (!SO()->getActive())
  364. return;
  365. if((flags & TCF_Parent) != 0)
  366. {
  367. clearColliders();
  368. updateColliders();
  369. if (((UINT32)mFlags & (UINT32)Rigidbody::Flag::AutoTensors) != 0)
  370. mInternal->updateMassDistribution();
  371. #if BS_DEBUG_MODE
  372. checkForNestedRigibody();
  373. #endif
  374. }
  375. mInternal->setTransform(SO()->getWorldPosition(), SO()->getWorldRotation());
  376. if (mParentJoint != nullptr)
  377. mParentJoint->notifyRigidbodyMoved(mThisHandle);
  378. }
  379. RTTITypeBase* CRigidbody::getRTTIStatic()
  380. {
  381. return CRigidbodyRTTI::instance();
  382. }
  383. RTTITypeBase* CRigidbody::getRTTI() const
  384. {
  385. return CRigidbody::getRTTIStatic();
  386. }
  387. }