BsCCollider.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. Vector3 myScale = SO()->getWorldScale();
  188. if (mParent != nullptr)
  189. {
  190. Vector3 parentPos = mParent->SO()->getWorldPosition();
  191. Quaternion parentRot = mParent->SO()->getWorldRotation();
  192. Vector3 myPos = SO()->getWorldPosition();
  193. Quaternion myRot = SO()->getWorldRotation();
  194. Vector3 scale = mParent->SO()->getWorldScale();
  195. Vector3 invScale = scale;
  196. if (invScale.x != 0) invScale.x = 1.0f / invScale.x;
  197. if (invScale.y != 0) invScale.y = 1.0f / invScale.y;
  198. if (invScale.z != 0) invScale.z = 1.0f / invScale.z;
  199. Quaternion invRotation = parentRot.inverse();
  200. Vector3 relativePos = invRotation.rotate(myPos - parentPos) * invScale;
  201. Quaternion relativeRot = invRotation * myRot;
  202. relativePos = relativePos + myRot.rotate(mLocalPosition * scale);
  203. relativeRot = relativeRot * mLocalRotation;
  204. mInternal->setTransform(relativePos, relativeRot);
  205. mParent->_updateMassDistribution();
  206. }
  207. else
  208. {
  209. Quaternion myRot = SO()->getWorldRotation();
  210. Vector3 myPos = SO()->getWorldPosition() + myRot.rotate(mLocalPosition * myScale);
  211. myRot = myRot * mLocalRotation;
  212. mInternal->setTransform(myPos, myRot);
  213. }
  214. mInternal->setScale(myScale);
  215. }
  216. void CCollider::updateCollisionReportMode()
  217. {
  218. CollisionReportMode mode = mCollisionReportMode;
  219. if (mParent != nullptr)
  220. mode = mParent->getCollisionReportMode();
  221. if(mInternal != nullptr)
  222. mInternal->setCollisionReportMode(mode);
  223. }
  224. void CCollider::triggerOnCollisionBegin(const CollisionDataRaw& data)
  225. {
  226. CollisionData hit;
  227. hit.contactPoints = std::move(data.contactPoints);
  228. hit.collider[0] = mThisHandle;
  229. if(data.colliders[1] != nullptr)
  230. {
  231. CCollider* other = (CCollider*)data.colliders[1]->_getOwner(PhysicsOwnerType::Component);
  232. hit.collider[1] = other->getHandle();
  233. }
  234. onCollisionBegin(hit);
  235. }
  236. void CCollider::triggerOnCollisionStay(const CollisionDataRaw& data)
  237. {
  238. CollisionData hit;
  239. hit.contactPoints = std::move(data.contactPoints);
  240. hit.collider[0] = mThisHandle;
  241. if (data.colliders[1] != nullptr)
  242. {
  243. CCollider* other = (CCollider*)data.colliders[1]->_getOwner(PhysicsOwnerType::Component);
  244. hit.collider[1] = other->getHandle();
  245. }
  246. onCollisionStay(hit);
  247. }
  248. void CCollider::triggerOnCollisionEnd(const CollisionDataRaw& data)
  249. {
  250. CollisionData hit;
  251. hit.contactPoints = std::move(data.contactPoints);
  252. hit.collider[0] = mThisHandle;
  253. if (data.colliders[1] != nullptr)
  254. {
  255. CCollider* other = (CCollider*)data.colliders[1]->_getOwner(PhysicsOwnerType::Component);
  256. hit.collider[1] = other->getHandle();
  257. }
  258. onCollisionEnd(hit);
  259. }
  260. RTTITypeBase* CCollider::getRTTIStatic()
  261. {
  262. return CColliderRTTI::instance();
  263. }
  264. RTTITypeBase* CCollider::getRTTI() const
  265. {
  266. return CCollider::getRTTIStatic();
  267. }
  268. }