BsCCollider.cpp 8.1 KB

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