BsCCollider.h 5.3 KB

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