BsCCollider.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. bool CCollider::rayCast(const Ray& ray, PhysicsQueryHit& hit, float maxDist) const
  114. {
  115. if (mInternal == nullptr)
  116. return false;
  117. return mInternal->rayCast(ray, hit, maxDist);
  118. }
  119. bool CCollider::rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
  120. float maxDist) const
  121. {
  122. if (mInternal == nullptr)
  123. return false;
  124. return mInternal->rayCast(origin, unitDir, hit, maxDist);
  125. }
  126. void CCollider::restoreInternal()
  127. {
  128. if (mInternal == nullptr)
  129. {
  130. mInternal = createInternal();
  131. mInternal->onCollisionBegin.connect(std::bind(&CCollider::triggerOnCollisionBegin, this, _1));
  132. mInternal->onCollisionStay.connect(std::bind(&CCollider::triggerOnCollisionStay, this, _1));
  133. mInternal->onCollisionEnd.connect(std::bind(&CCollider::triggerOnCollisionEnd, this, _1));
  134. }
  135. // Note: Merge into one call to avoid many virtual function calls
  136. mInternal->setIsTrigger(mIsTrigger);
  137. mInternal->setMass(mMass);
  138. mInternal->setMaterial(mMaterial);
  139. mInternal->setContactOffset(mContactOffset);
  140. mInternal->setRestOffset(mRestOffset);
  141. mInternal->setLayer(mLayer);
  142. updateParentRigidbody();
  143. updateTransform();
  144. }
  145. void CCollider::destroyInternal()
  146. {
  147. if (mParent != nullptr)
  148. mParent->removeCollider(mThisHandle);
  149. mParent = nullptr;
  150. // This should release the last reference and destroy the internal collider
  151. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  152. mInternal = nullptr;
  153. }
  154. void CCollider::updateParentRigidbody()
  155. {
  156. if (mIsTrigger)
  157. {
  158. setRigidbody(HRigidbody());
  159. return;
  160. }
  161. HSceneObject currentSO = SO();
  162. while (currentSO != nullptr)
  163. {
  164. HRigidbody parent = currentSO->getComponent<CRigidbody>();
  165. if (parent != nullptr)
  166. {
  167. if(currentSO->getActive() && isValidParent(parent))
  168. setRigidbody(parent);
  169. else
  170. setRigidbody(HRigidbody());
  171. return;
  172. }
  173. currentSO = currentSO->getParent();
  174. }
  175. // Not found
  176. setRigidbody(HRigidbody());
  177. }
  178. void CCollider::updateTransform()
  179. {
  180. if (mParent != nullptr)
  181. {
  182. Vector3 parentPos = mParent->SO()->getWorldPosition();
  183. Quaternion parentRot = mParent->SO()->getWorldRotation();
  184. Vector3 myPos = SO()->getWorldPosition();
  185. Quaternion myRot = SO()->getWorldRotation();
  186. Vector3 scale = mParent->SO()->getWorldScale();
  187. Vector3 invScale = scale;
  188. if (invScale.x != 0) invScale.x = 1.0f / invScale.x;
  189. if (invScale.y != 0) invScale.y = 1.0f / invScale.y;
  190. if (invScale.z != 0) invScale.z = 1.0f / invScale.z;
  191. Quaternion invRotation = parentRot.inverse();
  192. Vector3 relativePos = invRotation.rotate(myPos - parentPos) * invScale;
  193. Quaternion relativeRot = invRotation * myRot;
  194. relativePos = relativePos + myRot.rotate(mLocalPosition * scale);
  195. relativeRot = relativeRot * mLocalRotation;
  196. mInternal->setTransform(relativePos, relativeRot);
  197. mParent->_updateMassDistribution();
  198. }
  199. else
  200. {
  201. Vector3 myScale = SO()->getWorldScale();
  202. Quaternion myRot = SO()->getWorldRotation();
  203. Vector3 myPos = SO()->getWorldPosition() + myRot.rotate(mLocalPosition * myScale);
  204. myRot = myRot * mLocalRotation;
  205. mInternal->setTransform(myPos, myRot);
  206. }
  207. }
  208. void CCollider::triggerOnCollisionBegin(const CollisionData& data)
  209. {
  210. // Const-cast and modify is okay because we're the only object receiving this event
  211. CollisionData& hit = const_cast<CollisionData&>(data);
  212. hit.collider[0] = mThisHandle;
  213. if(hit.collidersRaw[1] != nullptr)
  214. {
  215. CCollider* other = (CCollider*)hit.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  216. hit.collider[1] = other->getHandle();
  217. }
  218. onCollisionBegin(hit);
  219. }
  220. void CCollider::triggerOnCollisionStay(const CollisionData& data)
  221. {
  222. // Const-cast and modify is okay because we're the only object receiving this event
  223. CollisionData& hit = const_cast<CollisionData&>(data);
  224. hit.collider[0] = mThisHandle;
  225. if (hit.collidersRaw[1] != nullptr)
  226. {
  227. CCollider* other = (CCollider*)hit.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  228. hit.collider[1] = other->getHandle();
  229. }
  230. onCollisionStay(hit);
  231. }
  232. void CCollider::triggerOnCollisionEnd(const CollisionData& data)
  233. {
  234. // Const-cast and modify is okay because we're the only object receiving this event
  235. CollisionData& hit = const_cast<CollisionData&>(data);
  236. hit.collider[0] = mThisHandle;
  237. if (hit.collidersRaw[1] != nullptr)
  238. {
  239. CCollider* other = (CCollider*)hit.collidersRaw[1]->_getOwner(PhysicsOwnerType::Component);
  240. hit.collider[1] = other->getHandle();
  241. }
  242. onCollisionEnd(hit);
  243. }
  244. RTTITypeBase* CCollider::getRTTIStatic()
  245. {
  246. return CColliderRTTI::instance();
  247. }
  248. RTTITypeBase* CCollider::getRTTI() const
  249. {
  250. return CCollider::getRTTIStatic();
  251. }
  252. }