BsCCollider.cpp 8.0 KB

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