BsCCollider.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. destroyInternal();
  71. }
  72. void CCollider::onDisabled()
  73. {
  74. destroyInternal();
  75. }
  76. void CCollider::onEnabled()
  77. {
  78. restoreInternal();
  79. }
  80. void CCollider::onTransformChanged(TransformChangedFlags flags)
  81. {
  82. if (!SO()->getActive())
  83. return;
  84. if ((flags & TCF_Parent) != 0)
  85. updateParentRigidbody();
  86. // Don't update the transform if it's due to Physics update since then we can guarantee it will remain at the same
  87. // relative transform to its parent
  88. if (gPhysics()._isUpdateInProgress())
  89. return;
  90. if ((flags & (TCF_Parent | TCF_Transform)) != 0)
  91. updateTransform();
  92. }
  93. void CCollider::setRigidbody(const HRigidbody& rigidbody, bool internal)
  94. {
  95. Rigidbody* rigidBodyPtr;
  96. if (rigidbody != nullptr)
  97. rigidBodyPtr = rigidbody->_getInternal();
  98. if (mInternal == nullptr)
  99. {
  100. if (rigidbody == mParent)
  101. return;
  102. if (!internal)
  103. {
  104. if (mParent != nullptr)
  105. mParent->removeCollider(mThisHandle);
  106. mInternal->setRigidbody(rigidBodyPtr);
  107. if (rigidbody != nullptr)
  108. rigidbody->addCollider(mThisHandle);
  109. }
  110. mParent = rigidbody;
  111. }
  112. }
  113. void CCollider::restoreInternal()
  114. {
  115. if (mInternal == nullptr)
  116. {
  117. mInternal = createInternal();
  118. mInternal->onCollisionBegin.connect(std::bind(&CCollider::triggerOnCollisionBegin, this, _1));
  119. mInternal->onCollisionStay.connect(std::bind(&CCollider::triggerOnCollisionStay, this, _1));
  120. mInternal->onCollisionEnd.connect(std::bind(&CCollider::triggerOnCollisionEnd, this, _1));
  121. }
  122. // Note: Merge into one call to avoid many virtual function calls
  123. mInternal->setIsTrigger(mIsTrigger);
  124. mInternal->setMass(mMass);
  125. mInternal->setMaterial(mMaterial);
  126. mInternal->setContactOffset(mContactOffset);
  127. mInternal->setRestOffset(mRestOffset);
  128. mInternal->setLayer(mLayer);
  129. updateParentRigidbody();
  130. updateTransform();
  131. }
  132. void CCollider::destroyInternal()
  133. {
  134. if (mParent != nullptr)
  135. mParent->removeCollider(mThisHandle);
  136. mParent = nullptr;
  137. // This should release the last reference and destroy the internal collider
  138. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  139. mInternal = nullptr;
  140. }
  141. void CCollider::updateParentRigidbody()
  142. {
  143. if (mIsTrigger)
  144. {
  145. setRigidbody(HRigidbody());
  146. return;
  147. }
  148. HSceneObject currentSO = SO();
  149. while (currentSO != nullptr)
  150. {
  151. HRigidbody parent = currentSO->getComponent<CRigidbody>();
  152. if (parent != nullptr)
  153. {
  154. if(currentSO->getActive() && isValidParent(parent))
  155. setRigidbody(parent);
  156. else
  157. setRigidbody(HRigidbody());
  158. return;
  159. }
  160. currentSO = currentSO->getParent();
  161. }
  162. // Not found
  163. setRigidbody(HRigidbody());
  164. }
  165. void CCollider::updateTransform()
  166. {
  167. if (mParent != nullptr)
  168. {
  169. Vector3 parentPos = mParent->SO()->getWorldPosition();
  170. Quaternion parentRot = mParent->SO()->getWorldRotation();
  171. Vector3 myPos = SO()->getWorldPosition();
  172. Quaternion myRot = SO()->getWorldRotation();
  173. Vector3 scale = mParent->SO()->getWorldScale();
  174. Vector3 invScale = scale;
  175. if (invScale.x != 0) invScale.x = 1.0f / invScale.x;
  176. if (invScale.y != 0) invScale.y = 1.0f / invScale.y;
  177. if (invScale.z != 0) invScale.z = 1.0f / invScale.z;
  178. Quaternion invRotation = parentRot.inverse();
  179. Vector3 relativePos = invRotation.rotate(myPos - parentPos) * invScale;
  180. Quaternion relativeRot = invRotation * myRot;
  181. relativePos = relativePos + myRot.rotate(mLocalPosition * scale);
  182. relativeRot = relativeRot * mLocalRotation;
  183. mInternal->setTransform(relativePos, relativeRot);
  184. mParent->_updateMassDistribution();
  185. }
  186. else
  187. {
  188. Vector3 myScale = SO()->getWorldScale();
  189. Quaternion myRot = SO()->getWorldRotation();
  190. Vector3 myPos = SO()->getWorldPosition() + myRot.rotate(mLocalPosition * myScale);
  191. myRot = myRot * mLocalRotation;
  192. mInternal->setTransform(myPos, myRot);
  193. }
  194. }
  195. void CCollider::triggerOnCollisionBegin(const CollisionData& data)
  196. {
  197. // Const-cast and modify is okay because we're the only object receiving this event
  198. CollisionData& hit = const_cast<CollisionData&>(data);
  199. hit.collider[0] = mThisHandle;
  200. if(hit.collidersRaw[1] != nullptr)
  201. {
  202. CCollider* other = (CCollider*)hit.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  203. hit.collider[1] = other->getHandle();
  204. }
  205. onCollisionBegin(hit);
  206. }
  207. void CCollider::triggerOnCollisionStay(const CollisionData& data)
  208. {
  209. // Const-cast and modify is okay because we're the only object receiving this event
  210. CollisionData& hit = const_cast<CollisionData&>(data);
  211. hit.collider[0] = mThisHandle;
  212. if (hit.collidersRaw[1] != nullptr)
  213. {
  214. CCollider* other = (CCollider*)hit.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  215. hit.collider[1] = other->getHandle();
  216. }
  217. onCollisionStay(hit);
  218. }
  219. void CCollider::triggerOnCollisionEnd(const CollisionData& data)
  220. {
  221. // Const-cast and modify is okay because we're the only object receiving this event
  222. CollisionData& hit = const_cast<CollisionData&>(data);
  223. hit.collider[0] = mThisHandle;
  224. if (hit.collidersRaw[1] != nullptr)
  225. {
  226. CCollider* other = (CCollider*)hit.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  227. hit.collider[1] = other->getHandle();
  228. }
  229. onCollisionEnd(hit);
  230. }
  231. RTTITypeBase* CCollider::getRTTIStatic()
  232. {
  233. return CColliderRTTI::instance();
  234. }
  235. RTTITypeBase* CCollider::getRTTI() const
  236. {
  237. return CCollider::getRTTIStatic();
  238. }
  239. }