BsCCollider.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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::getRigidbody */
  47. HRigidbody getRigidbody() const { return mParent; }
  48. /** @copydoc Collider::onCollisionBegin */
  49. Event<void(const CollisionData&)> onCollisionBegin;
  50. /** @copydoc Collider::onCollisionStay */
  51. Event<void(const CollisionData&)> onCollisionStay;
  52. /** @copydoc Collider::onCollisionEnd */
  53. Event<void(const CollisionData&)> onCollisionEnd;
  54. /** @cond INTERNAL */
  55. /** Returns the Collider implementation wrapped by this component. */
  56. SPtr<Collider> _getInternal() const { return mInternal; }
  57. /** @endcond */
  58. /************************************************************************/
  59. /* COMPONENT OVERRIDES */
  60. /************************************************************************/
  61. protected:
  62. friend class SceneObject;
  63. friend class CRigidbody;
  64. /** @copydoc Component::onInitialized() */
  65. void onInitialized() override;
  66. /** @copydoc Component::onDestroyed() */
  67. void onDestroyed() override;
  68. /** @copydoc Component::onDisabled() */
  69. void onDisabled() override;
  70. /** @copydoc Component::onEnabled() */
  71. void onEnabled() override;
  72. /** @copydoc Component::onTransformChanged() */
  73. void onTransformChanged(TransformChangedFlags flags) override;
  74. protected:
  75. /** Creates the internal representation of the Collider for use by the component. */
  76. virtual SPtr<Collider> createInternal() = 0;
  77. /** Creates the internal representation of the Collider and restores the values saved by the Component. */
  78. virtual void restoreInternal();
  79. /**
  80. * Checks is the provided rigidbody a valid parent for this collider.
  81. *
  82. * @note This is required because certain colliders are limited in how they can be used. */
  83. virtual bool isValidParent(const HRigidbody& parent) const { return true; }
  84. /**
  85. * Changes the rigidbody parent of the collider. Meant to be called from the Rigidbody itself.
  86. *
  87. * @param rigidbody New rigidbody to assign as the parent to the collider.
  88. * @param internal If true the rigidbody will just be changed internally, but parent rigidbody will not be
  89. * notified.
  90. */
  91. void setRigidbody(const HRigidbody& rigidbody, bool internal = false);
  92. /**
  93. * Updates the transform of the internal Collider representation from the transform of the component's Scene Object.
  94. */
  95. void updateTransform();
  96. /** Searches the parent scene object hierarchy to find a parent Rigidbody component. */
  97. void updateParentRigidbody();
  98. /** Triggered when the internal collider begins touching another object. */
  99. void triggerOnCollisionBegin(const CollisionData& data);
  100. /** Triggered when the internal collider continues touching another object. */
  101. void triggerOnCollisionStay(const CollisionData& data);
  102. /** Triggered when the internal collider ends touching another object. */
  103. void triggerOnCollisionEnd(const CollisionData& data);
  104. SPtr<Collider> mInternal;
  105. UINT64 mLayer = 1;
  106. float mRestOffset = 0.0f;
  107. float mContactOffset = 0.0f;
  108. HPhysicsMaterial mMaterial;
  109. float mMass = 0.0f;
  110. bool mIsTrigger = false;
  111. Vector3 mLocalPosition;
  112. Quaternion mLocalRotation;
  113. HRigidbody mParent;
  114. /************************************************************************/
  115. /* RTTI */
  116. /************************************************************************/
  117. public:
  118. friend class CColliderRTTI;
  119. static RTTITypeBase* getRTTIStatic();
  120. RTTITypeBase* getRTTI() const override;
  121. protected:
  122. CCollider() {} // Serialization only
  123. };
  124. /** @} */
  125. }