BsCCollider.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsCCollider.h"
  4. #include "BsSceneObject.h"
  5. #include "BsCRigidbody.h"
  6. #include "BsPhysics.h"
  7. #include "BsCColliderRTTI.h"
  8. using namespace std::placeholders;
  9. namespace BansheeEngine
  10. {
  11. CCollider::CCollider(const HSceneObject& parent)
  12. : Component(parent)
  13. {
  14. setName("Collider");
  15. mNotifyFlags = (TransformChangedFlags)(TCF_Parent | TCF_Transform);
  16. }
  17. void CCollider::setIsTrigger(bool value)
  18. {
  19. if (mIsTrigger == value)
  20. return;
  21. mIsTrigger = value;
  22. if (mInternal != nullptr)
  23. {
  24. mInternal->setIsTrigger(value);
  25. updateParentRigidbody();
  26. updateTransform();
  27. }
  28. }
  29. void CCollider::setMass(float mass)
  30. {
  31. if (mMass == mass)
  32. return;
  33. mMass = mass;
  34. if (mInternal != nullptr)
  35. {
  36. mInternal->setMass(mass);
  37. if (mParent != nullptr)
  38. mParent->_updateMassDistribution();
  39. }
  40. }
  41. void CCollider::setMaterial(const HPhysicsMaterial& material)
  42. {
  43. mMaterial = material;
  44. if (mInternal != nullptr)
  45. mInternal->setMaterial(material);
  46. }
  47. void CCollider::setContactOffset(float value)
  48. {
  49. mContactOffset = value;
  50. if (mInternal != nullptr)
  51. mInternal->setContactOffset(value);
  52. }
  53. void CCollider::setRestOffset(float value)
  54. {
  55. mRestOffset = value;
  56. if (mInternal != nullptr)
  57. mInternal->setRestOffset(value);
  58. }
  59. void CCollider::setLayer(UINT64 layer)
  60. {
  61. mLayer = layer;
  62. if (mInternal != nullptr)
  63. mInternal->setLayer(layer);
  64. }
  65. void CCollider::onInitialized()
  66. {
  67. }
  68. void CCollider::onDestroyed()
  69. {
  70. if (mParent != nullptr)
  71. mParent->removeCollider(mThisHandle);
  72. mParent = nullptr;
  73. // This should release the last reference and destroy the internal collider
  74. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  75. mInternal = nullptr;
  76. }
  77. void CCollider::onDisabled()
  78. {
  79. if (mParent != nullptr)
  80. mParent->removeCollider(mThisHandle);
  81. mParent = nullptr;
  82. // This should release the last reference and destroy the internal collider
  83. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  84. mInternal = nullptr;
  85. }
  86. void CCollider::onEnabled()
  87. {
  88. restoreInternal();
  89. }
  90. void CCollider::onTransformChanged(TransformChangedFlags flags)
  91. {
  92. if (!SO()->getActive())
  93. return;
  94. if ((flags & TCF_Parent) != 0)
  95. updateParentRigidbody();
  96. // Don't update the transform if it's due to Physics update since then we can guarantee it will remain at the same
  97. // relative transform to its parent
  98. if (gPhysics()._isUpdateInProgress())
  99. return;
  100. if ((flags & (TCF_Parent | TCF_Transform)) != 0)
  101. updateTransform();
  102. }
  103. void CCollider::setRigidbody(const HRigidbody& rigidbody, bool internal)
  104. {
  105. SPtr<Rigidbody> rigidBodyPtr;
  106. if (rigidbody != nullptr)
  107. rigidBodyPtr = rigidbody->_getInternal();
  108. if (mInternal == nullptr)
  109. {
  110. if (rigidbody == mParent)
  111. return;
  112. if (!internal)
  113. {
  114. if (mParent != nullptr)
  115. mParent->removeCollider(mThisHandle);
  116. mInternal->setRigidbody(rigidBodyPtr);
  117. if (rigidbody != nullptr)
  118. rigidbody->addCollider(mThisHandle);
  119. }
  120. mParent = rigidbody;
  121. }
  122. }
  123. void CCollider::restoreInternal()
  124. {
  125. if (mInternal == nullptr)
  126. {
  127. mInternal = createInternal();
  128. mInternal->onCollisionBegin.connect(std::bind(&CCollider::triggerOnCollisionBegin, this, _1));
  129. mInternal->onCollisionStay.connect(std::bind(&CCollider::triggerOnCollisionStay, this, _1));
  130. mInternal->onCollisionEnd.connect(std::bind(&CCollider::triggerOnCollisionEnd, this, _1));
  131. }
  132. // Note: Merge into one call to avoid many virtual function calls
  133. mInternal->setIsTrigger(mIsTrigger);
  134. mInternal->setMass(mMass);
  135. mInternal->setMaterial(mMaterial);
  136. mInternal->setContactOffset(mContactOffset);
  137. mInternal->setRestOffset(mRestOffset);
  138. mInternal->setLayer(mLayer);
  139. updateParentRigidbody();
  140. updateTransform();
  141. }
  142. void CCollider::updateParentRigidbody()
  143. {
  144. if (mIsTrigger)
  145. {
  146. setRigidbody(HRigidbody());
  147. return;
  148. }
  149. HSceneObject currentSO = SO();
  150. while (currentSO != nullptr)
  151. {
  152. HRigidbody parent = currentSO->getComponent<CRigidbody>();
  153. if (parent != nullptr)
  154. {
  155. if(currentSO->getActive() && isValidParent(parent))
  156. setRigidbody(parent);
  157. else
  158. setRigidbody(HRigidbody());
  159. return;
  160. }
  161. currentSO = currentSO->getParent();
  162. }
  163. // Not found
  164. setRigidbody(HRigidbody());
  165. }
  166. void CCollider::updateTransform()
  167. {
  168. if (mParent != nullptr)
  169. {
  170. Vector3 parentPos = mParent->SO()->getWorldPosition();
  171. Quaternion parentRot = mParent->SO()->getWorldRotation();
  172. Vector3 myPos = SO()->getWorldPosition();
  173. Quaternion myRot = SO()->getWorldRotation();
  174. Vector3 scale = mParent->SO()->getWorldScale();
  175. Vector3 invScale = scale;
  176. if (invScale.x != 0) invScale.x = 1.0f / invScale.x;
  177. if (invScale.y != 0) invScale.y = 1.0f / invScale.y;
  178. if (invScale.z != 0) invScale.z = 1.0f / invScale.z;
  179. Quaternion invRotation = parentRot.inverse();
  180. Vector3 relativePos = invRotation.rotate(myPos - parentPos) * invScale;
  181. Quaternion relativeRot = invRotation * myRot;
  182. relativePos = relativePos + myRot.rotate(mLocalPosition * scale);
  183. relativeRot = relativeRot * mLocalRotation;
  184. mInternal->setTransform(relativePos, relativeRot);
  185. mParent->_updateMassDistribution();
  186. }
  187. else
  188. {
  189. Vector3 myScale = SO()->getWorldScale();
  190. Quaternion myRot = SO()->getWorldRotation();
  191. Vector3 myPos = SO()->getWorldPosition() + myRot.rotate(mLocalPosition * myScale);
  192. myRot = myRot * mLocalRotation;
  193. mInternal->setTransform(myPos, myRot);
  194. }
  195. }
  196. void CCollider::triggerOnCollisionBegin(const CollisionData& data)
  197. {
  198. // Const-cast and modify is okay because we're the only object receiving this event
  199. CollisionData& hit = const_cast<CollisionData&>(data);
  200. hit.collider = mThisHandle;
  201. onCollisionBegin(hit);
  202. }
  203. void CCollider::triggerOnCollisionStay(const CollisionData& data)
  204. {
  205. // Const-cast and modify is okay because we're the only object receiving this event
  206. CollisionData& hit = const_cast<CollisionData&>(data);
  207. hit.collider = mThisHandle;
  208. onCollisionStay(hit);
  209. }
  210. void CCollider::triggerOnCollisionEnd(const CollisionData& data)
  211. {
  212. // Const-cast and modify is okay because we're the only object receiving this event
  213. CollisionData& hit = const_cast<CollisionData&>(data);
  214. hit.collider = mThisHandle;
  215. onCollisionEnd(hit);
  216. }
  217. RTTITypeBase* CCollider::getRTTIStatic()
  218. {
  219. return CColliderRTTI::instance();
  220. }
  221. RTTITypeBase* CCollider::getRTTI() const
  222. {
  223. return CCollider::getRTTIStatic();
  224. }
  225. }