BsCCollider.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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) const */
  53. inline bool rayCast(const Ray& ray, PhysicsQueryHit& hit, float maxDist = FLT_MAX) const;
  54. /** @copydoc Collider::rayCast(const Vector3&, const Vector3&, PhysicsQueryHit&, float) const */
  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. /** @name Internal
  64. * @{
  65. */
  66. /** Returns the Collider implementation wrapped by this component. */
  67. Collider* _getInternal() const { return mInternal.get(); }
  68. /** @} */
  69. /************************************************************************/
  70. /* COMPONENT OVERRIDES */
  71. /************************************************************************/
  72. protected:
  73. friend class SceneObject;
  74. friend class CRigidbody;
  75. /** @copydoc Component::onInitialized() */
  76. void onInitialized() override;
  77. /** @copydoc Component::onDestroyed() */
  78. void onDestroyed() override;
  79. /** @copydoc Component::onDisabled() */
  80. void onDisabled() override;
  81. /** @copydoc Component::onEnabled() */
  82. void onEnabled() override;
  83. /** @copydoc Component::onTransformChanged() */
  84. void onTransformChanged(TransformChangedFlags flags) override;
  85. protected:
  86. using Component::destroyInternal;
  87. /** Creates the internal representation of the Collider for use by the component. */
  88. virtual SPtr<Collider> createInternal() = 0;
  89. /** Creates the internal representation of the Collider and restores the values saved by the Component. */
  90. virtual void restoreInternal();
  91. /** Destroys the internal collider representation. */
  92. void destroyInternal();
  93. /**
  94. * Checks is the provided rigidbody a valid parent for this collider.
  95. *
  96. * @note This is required because certain colliders are limited in how they can be used. */
  97. virtual bool isValidParent(const HRigidbody& parent) const { return true; }
  98. /**
  99. * Changes the rigidbody parent of the collider. Meant to be called from the Rigidbody itself.
  100. *
  101. * @param[in] rigidbody New rigidbody to assign as the parent to the collider.
  102. * @param[in] internal If true the rigidbody will just be changed internally, but parent rigidbody will not be
  103. * notified.
  104. */
  105. void setRigidbody(const HRigidbody& rigidbody, bool internal = false);
  106. /**
  107. * Updates the transform of the internal Collider representation from the transform of the component's scene object.
  108. */
  109. void updateTransform();
  110. /** Applies the collision report mode to the internal collider depending on the current state. */
  111. void updateCollisionReportMode();
  112. /** Searches the parent scene object hierarchy to find a parent Rigidbody component. */
  113. void updateParentRigidbody();
  114. /** Triggered when the internal collider begins touching another object. */
  115. void triggerOnCollisionBegin(const CollisionData& data);
  116. /** Triggered when the internal collider continues touching another object. */
  117. void triggerOnCollisionStay(const CollisionData& data);
  118. /** Triggered when the internal collider ends touching another object. */
  119. void triggerOnCollisionEnd(const CollisionData& data);
  120. SPtr<Collider> mInternal;
  121. UINT64 mLayer = 1;
  122. CollisionReportMode mCollisionReportMode = CollisionReportMode::None;
  123. float mRestOffset = 0.0f;
  124. float mContactOffset = 0.02f;
  125. HPhysicsMaterial mMaterial;
  126. float mMass = 1.0f;
  127. bool mIsTrigger = false;
  128. Vector3 mLocalPosition = Vector3::ZERO;
  129. Quaternion mLocalRotation = Quaternion::IDENTITY;
  130. HRigidbody mParent;
  131. /************************************************************************/
  132. /* RTTI */
  133. /************************************************************************/
  134. public:
  135. friend class CColliderRTTI;
  136. static RTTITypeBase* getRTTIStatic();
  137. RTTITypeBase* getRTTI() const override;
  138. protected:
  139. CCollider() {} // Serialization only
  140. };
  141. /** @} */
  142. }