BsCCollider.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsCollider.h"
  6. #include "BsComponent.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Components-Core
  10. * @{
  11. */
  12. /**
  13. * @copydoc Collider
  14. *
  15. * Wraps Collider as a Component.
  16. */
  17. class BS_CORE_EXPORT CCollider : public Component
  18. {
  19. public:
  20. CCollider(const HSceneObject& parent);
  21. virtual ~CCollider() {}
  22. /** @copydoc Collider::setIsTrigger */
  23. inline void setIsTrigger(bool value);
  24. /** @copydoc Collider::getIsTrigger */
  25. bool getIsTrigger() const { return mIsTrigger; }
  26. /** @copydoc Collider::setMass */
  27. inline void setMass(float mass);
  28. /** @copydoc Collider::getMass */
  29. float getMass() const { return mMass; }
  30. /** @copydoc Collider::setMaterial */
  31. inline void setMaterial(const HPhysicsMaterial& material);
  32. /** @copydoc Collider::getMaterial */
  33. HPhysicsMaterial getMaterial() const { return mMaterial; }
  34. /** @copydoc Collider::setContactOffset */
  35. inline void setContactOffset(float value);
  36. /** @copydoc Collider::getContactOffset */
  37. float getContactOffset() const { return mContactOffset; }
  38. /** @copydoc Collider::setRestOffset */
  39. inline void setRestOffset(float value);
  40. /** @copydoc Collider::getRestOffset */
  41. float getRestOffset() const { return mRestOffset; }
  42. /** @copydoc Collider::setLayer */
  43. inline void setLayer(UINT64 layer);
  44. /** @copydoc Collider::getLayer */
  45. UINT64 getLayer() const { return mLayer; }
  46. /** @copydoc Collider::setCollisionReportMode */
  47. inline void setCollisionReportMode(CollisionReportMode mode);
  48. /** @copydoc Collider::getCollisionReportMode */
  49. CollisionReportMode getCollisionReportMode() const { return mCollisionReportMode; }
  50. /** @copydoc Collider::getRigidbody */
  51. HRigidbody getRigidbody() const { return mParent; }
  52. /** @copydoc Collider::rayCast(const Ray&, PhysicsQueryHit&, float) */
  53. inline bool rayCast(const Ray& ray, PhysicsQueryHit& hit, float maxDist = FLT_MAX) const;
  54. /** @copydoc Collider::rayCast(const Vector3&, const Vector3&, PhysicsQueryHit&, float) */
  55. inline bool rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
  56. float maxDist = FLT_MAX) const;
  57. /** @copydoc Collider::onCollisionBegin */
  58. Event<void(const CollisionData&)> onCollisionBegin;
  59. /** @copydoc Collider::onCollisionStay */
  60. Event<void(const CollisionData&)> onCollisionStay;
  61. /** @copydoc Collider::onCollisionEnd */
  62. Event<void(const CollisionData&)> onCollisionEnd;
  63. /** @cond INTERNAL */
  64. /** Returns the Collider implementation wrapped by this component. */
  65. Collider* _getInternal() const { return mInternal.get(); }
  66. /** @endcond */
  67. /************************************************************************/
  68. /* COMPONENT OVERRIDES */
  69. /************************************************************************/
  70. protected:
  71. friend class SceneObject;
  72. friend class CRigidbody;
  73. /** @copydoc Component::onInitialized() */
  74. void onInitialized() override;
  75. /** @copydoc Component::onDestroyed() */
  76. void onDestroyed() override;
  77. /** @copydoc Component::onDisabled() */
  78. void onDisabled() override;
  79. /** @copydoc Component::onEnabled() */
  80. void onEnabled() override;
  81. /** @copydoc Component::onTransformChanged() */
  82. void onTransformChanged(TransformChangedFlags flags) override;
  83. protected:
  84. /** Creates the internal representation of the Collider for use by the component. */
  85. virtual SPtr<Collider> createInternal() = 0;
  86. /** Creates the internal representation of the Collider and restores the values saved by the Component. */
  87. virtual void restoreInternal();
  88. /** Destroys the internal collider representation. */
  89. void destroyInternal();
  90. /**
  91. * Checks is the provided rigidbody a valid parent for this collider.
  92. *
  93. * @note This is required because certain colliders are limited in how they can be used. */
  94. virtual bool isValidParent(const HRigidbody& parent) const { return true; }
  95. /**
  96. * Changes the rigidbody parent of the collider. Meant to be called from the Rigidbody itself.
  97. *
  98. * @param rigidbody New rigidbody to assign as the parent to the collider.
  99. * @param internal If true the rigidbody will just be changed internally, but parent rigidbody will not be
  100. * notified.
  101. */
  102. void setRigidbody(const HRigidbody& rigidbody, bool internal = false);
  103. /**
  104. * Updates the transform of the internal Collider representation from the transform of the component's scene object.
  105. */
  106. void updateTransform();
  107. /** Applies the collision report mode to the internal collider depending on the current state. */
  108. void updateCollisionReportMode();
  109. /** Searches the parent scene object hierarchy to find a parent Rigidbody component. */
  110. void updateParentRigidbody();
  111. /** Triggered when the internal collider begins touching another object. */
  112. void triggerOnCollisionBegin(const CollisionData& data);
  113. /** Triggered when the internal collider continues touching another object. */
  114. void triggerOnCollisionStay(const CollisionData& data);
  115. /** Triggered when the internal collider ends touching another object. */
  116. void triggerOnCollisionEnd(const CollisionData& data);
  117. SPtr<Collider> mInternal;
  118. UINT64 mLayer = 1;
  119. CollisionReportMode mCollisionReportMode = CollisionReportMode::None;
  120. float mRestOffset = 0.0f;
  121. float mContactOffset = 0.02f;
  122. HPhysicsMaterial mMaterial;
  123. float mMass = 1.0f;
  124. bool mIsTrigger = false;
  125. Vector3 mLocalPosition;
  126. Quaternion mLocalRotation;
  127. HRigidbody mParent;
  128. /************************************************************************/
  129. /* RTTI */
  130. /************************************************************************/
  131. public:
  132. friend class CColliderRTTI;
  133. static RTTITypeBase* getRTTIStatic();
  134. RTTITypeBase* getRTTI() const override;
  135. protected:
  136. CCollider() {} // Serialization only
  137. };
  138. /** @} */
  139. }