BsCCollider.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Components/BsCCollider.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Components/BsCRigidbody.h"
  6. #include "Physics/BsPhysics.h"
  7. #include "RTTI/BsCColliderRTTI.h"
  8. using namespace std::placeholders;
  9. namespace bs
  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. value = std::max(0.0f, std::max(value, getRestOffset()));
  50. mContactOffset = value;
  51. if (mInternal != nullptr)
  52. mInternal->setContactOffset(value);
  53. }
  54. void CCollider::setRestOffset(float value)
  55. {
  56. value = std::min(value, getContactOffset());
  57. mRestOffset = value;
  58. if (mInternal != nullptr)
  59. mInternal->setRestOffset(value);
  60. }
  61. void CCollider::setLayer(UINT64 layer)
  62. {
  63. mLayer = layer;
  64. if (mInternal != nullptr)
  65. mInternal->setLayer(layer);
  66. }
  67. void CCollider::setCollisionReportMode(CollisionReportMode mode)
  68. {
  69. mCollisionReportMode = mode;
  70. if (mInternal != nullptr)
  71. updateCollisionReportMode();
  72. }
  73. void CCollider::onInitialized()
  74. {
  75. }
  76. void CCollider::onDestroyed()
  77. {
  78. destroyInternal();
  79. }
  80. void CCollider::onDisabled()
  81. {
  82. destroyInternal();
  83. }
  84. void CCollider::onEnabled()
  85. {
  86. restoreInternal();
  87. }
  88. void CCollider::onTransformChanged(TransformChangedFlags flags)
  89. {
  90. if (!SO()->getActive())
  91. return;
  92. if ((flags & TCF_Parent) != 0)
  93. updateParentRigidbody();
  94. // Don't update the transform if it's due to Physics update since then we can guarantee it will remain at the same
  95. // relative transform to its parent
  96. if (gPhysics()._isUpdateInProgress())
  97. return;
  98. if ((flags & (TCF_Parent | TCF_Transform)) != 0)
  99. updateTransform();
  100. }
  101. void CCollider::setRigidbody(const HRigidbody& rigidbody, bool internal)
  102. {
  103. if (rigidbody == mParent)
  104. return;
  105. if (mInternal != nullptr && !internal)
  106. {
  107. if (mParent != nullptr)
  108. mParent->removeCollider(mThisHandle);
  109. Rigidbody* rigidBodyPtr = nullptr;
  110. if (rigidbody != nullptr)
  111. rigidBodyPtr = rigidbody->_getInternal();
  112. mInternal->setRigidbody(rigidBodyPtr);
  113. if (rigidbody != nullptr)
  114. rigidbody->addCollider(mThisHandle);
  115. }
  116. mParent = rigidbody;
  117. updateCollisionReportMode();
  118. }
  119. bool CCollider::rayCast(const Ray& ray, PhysicsQueryHit& hit, float maxDist) const
  120. {
  121. if (mInternal == nullptr)
  122. return false;
  123. return mInternal->rayCast(ray, hit, maxDist);
  124. }
  125. bool CCollider::rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
  126. float maxDist) const
  127. {
  128. if (mInternal == nullptr)
  129. return false;
  130. return mInternal->rayCast(origin, unitDir, hit, maxDist);
  131. }
  132. void CCollider::restoreInternal()
  133. {
  134. if (mInternal == nullptr)
  135. {
  136. mInternal = createInternal();
  137. mInternal->onCollisionBegin.connect(std::bind(&CCollider::triggerOnCollisionBegin, this, _1));
  138. mInternal->onCollisionStay.connect(std::bind(&CCollider::triggerOnCollisionStay, this, _1));
  139. mInternal->onCollisionEnd.connect(std::bind(&CCollider::triggerOnCollisionEnd, this, _1));
  140. }
  141. // Note: Merge into one call to avoid many virtual function calls
  142. mInternal->setIsTrigger(mIsTrigger);
  143. mInternal->setMass(mMass);
  144. mInternal->setMaterial(mMaterial);
  145. mInternal->setContactOffset(mContactOffset);
  146. mInternal->setRestOffset(mRestOffset);
  147. mInternal->setLayer(mLayer);
  148. updateParentRigidbody();
  149. updateTransform();
  150. updateCollisionReportMode();
  151. }
  152. void CCollider::destroyInternal()
  153. {
  154. if (mParent != nullptr)
  155. mParent->removeCollider(mThisHandle);
  156. mParent = nullptr;
  157. // This should release the last reference and destroy the internal collider
  158. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  159. mInternal = nullptr;
  160. }
  161. void CCollider::updateParentRigidbody()
  162. {
  163. if (mIsTrigger)
  164. {
  165. setRigidbody(HRigidbody());
  166. return;
  167. }
  168. HSceneObject currentSO = SO();
  169. while (currentSO != nullptr)
  170. {
  171. HRigidbody parent = currentSO->getComponent<CRigidbody>();
  172. if (parent != nullptr)
  173. {
  174. if(currentSO->getActive() && isValidParent(parent))
  175. setRigidbody(parent);
  176. else
  177. setRigidbody(HRigidbody());
  178. return;
  179. }
  180. currentSO = currentSO->getParent();
  181. }
  182. // Not found
  183. setRigidbody(HRigidbody());
  184. }
  185. void CCollider::updateTransform()
  186. {
  187. const Transform& tfrm = SO()->getTransform();
  188. Vector3 myScale = tfrm.getScale();
  189. if (mParent != nullptr)
  190. {
  191. const Transform& parentTfrm = mParent->SO()->getTransform();
  192. Vector3 parentPos = parentTfrm.getPosition();
  193. Quaternion parentRot = parentTfrm.getRotation();
  194. Vector3 myPos = tfrm.getPosition();
  195. Quaternion myRot = tfrm.getRotation();
  196. Vector3 scale = parentTfrm.getScale();
  197. Vector3 invScale = scale;
  198. if (invScale.x != 0) invScale.x = 1.0f / invScale.x;
  199. if (invScale.y != 0) invScale.y = 1.0f / invScale.y;
  200. if (invScale.z != 0) invScale.z = 1.0f / invScale.z;
  201. Quaternion invRotation = parentRot.inverse();
  202. Vector3 relativePos = invRotation.rotate(myPos - parentPos) * invScale;
  203. Quaternion relativeRot = invRotation * myRot;
  204. relativePos = relativePos + myRot.rotate(mLocalPosition * scale);
  205. relativeRot = relativeRot * mLocalRotation;
  206. mInternal->setTransform(relativePos, relativeRot);
  207. mParent->_updateMassDistribution();
  208. }
  209. else
  210. {
  211. Quaternion myRot = tfrm.getRotation();
  212. Vector3 myPos = tfrm.getPosition() + myRot.rotate(mLocalPosition * myScale);
  213. myRot = myRot * mLocalRotation;
  214. mInternal->setTransform(myPos, myRot);
  215. }
  216. mInternal->setScale(myScale);
  217. }
  218. void CCollider::updateCollisionReportMode()
  219. {
  220. CollisionReportMode mode = mCollisionReportMode;
  221. if (mParent != nullptr)
  222. mode = mParent->getCollisionReportMode();
  223. if(mInternal != nullptr)
  224. mInternal->setCollisionReportMode(mode);
  225. }
  226. void CCollider::triggerOnCollisionBegin(const CollisionDataRaw& data)
  227. {
  228. CollisionData hit;
  229. hit.contactPoints = std::move(data.contactPoints);
  230. hit.collider[0] = mThisHandle;
  231. if(data.colliders[1] != nullptr)
  232. {
  233. CCollider* other = (CCollider*)data.colliders[1]->_getOwner(PhysicsOwnerType::Component);
  234. hit.collider[1] = other->getHandle();
  235. }
  236. onCollisionBegin(hit);
  237. }
  238. void CCollider::triggerOnCollisionStay(const CollisionDataRaw& data)
  239. {
  240. CollisionData hit;
  241. hit.contactPoints = std::move(data.contactPoints);
  242. hit.collider[0] = mThisHandle;
  243. if (data.colliders[1] != nullptr)
  244. {
  245. CCollider* other = (CCollider*)data.colliders[1]->_getOwner(PhysicsOwnerType::Component);
  246. hit.collider[1] = other->getHandle();
  247. }
  248. onCollisionStay(hit);
  249. }
  250. void CCollider::triggerOnCollisionEnd(const CollisionDataRaw& data)
  251. {
  252. CollisionData hit;
  253. hit.contactPoints = std::move(data.contactPoints);
  254. hit.collider[0] = mThisHandle;
  255. if (data.colliders[1] != nullptr)
  256. {
  257. CCollider* other = (CCollider*)data.colliders[1]->_getOwner(PhysicsOwnerType::Component);
  258. hit.collider[1] = other->getHandle();
  259. }
  260. onCollisionEnd(hit);
  261. }
  262. RTTITypeBase* CCollider::getRTTIStatic()
  263. {
  264. return CColliderRTTI::instance();
  265. }
  266. RTTITypeBase* CCollider::getRTTI() const
  267. {
  268. return CCollider::getRTTIStatic();
  269. }
  270. }