BsCollider.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "BsPhysicsCommon.h"
  6. #include "BsVector3.h"
  7. #include "BsQuaternion.h"
  8. namespace BansheeEngine
  9. {
  10. /** @addtogroup Physics
  11. * @{
  12. */
  13. /**
  14. * Collider represents physics geometry that can be in multiple states:
  15. * - Default: Static geometry that physics objects can collide with.
  16. * - Trigger: Static geometry that can't be collided with but will report touch events.
  17. * - Dynamic: Dynamic geometry that is a part of a Rigidbody. A set of colliders defines the shape of the parent
  18. * rigidbody.
  19. */
  20. class BS_CORE_EXPORT Collider
  21. {
  22. public:
  23. virtual ~Collider() { }
  24. /** @copydoc FCollider::getPosition */
  25. inline Vector3 getPosition() const;
  26. /** @copydoc FCollider::getRotation */
  27. inline Quaternion getRotation() const;
  28. /** @copydoc FCollider::setTransform */
  29. inline void setTransform(const Vector3& pos, const Quaternion& rot);
  30. /** @copydoc FCollider::setScale */
  31. virtual void setScale(const Vector3& scale);
  32. /** @copydoc FCollider::getScale */
  33. inline Vector3 getScale() const;
  34. /** @copydoc FCollider::setIsTrigger */
  35. inline void setIsTrigger(bool value);
  36. /** @copydoc FCollider::getIsTrigger */
  37. inline bool getIsTrigger() const;
  38. /** @copydoc FCollider::setRigidbody */
  39. inline void setRigidbody(Rigidbody* value);
  40. /** Returns the rigidbody this collider is attached to, if any. */
  41. Rigidbody* getRigidbody() const { return mRigidbody; }
  42. /** @copydoc FCollider::setMass */
  43. inline void setMass(float mass);
  44. /** @copydoc FCollider::getMass */
  45. inline float getMass() const;
  46. /** @copydoc FCollider::setMaterial */
  47. inline void setMaterial(const HPhysicsMaterial& material);
  48. /** @copydoc FCollider::getMaterial */
  49. inline HPhysicsMaterial getMaterial() const;
  50. /** @copydoc FCollider::setContactOffset */
  51. inline void setContactOffset(float value);
  52. /** @copydoc FCollider::getContactOffset */
  53. inline float getContactOffset();
  54. /** @copydoc FCollider::setRestOffset */
  55. inline void setRestOffset(float value);
  56. /** @copydoc FCollider::getRestOffset */
  57. inline float getRestOffset();
  58. /** @copydoc FCollider::setLayer */
  59. inline void setLayer(UINT64 layer);
  60. /** @copydoc FCollider::getLayer */
  61. inline UINT64 getLayer() const;
  62. /** Sets a value that determines which (if any) collision events are reported. */
  63. inline void setCollisionReportMode(CollisionReportMode mode);
  64. /** Gets a value that determines which (if any) collision events are reported. */
  65. inline CollisionReportMode getCollisionReportMode() const;
  66. /**
  67. * Checks does the ray hit this collider.
  68. *
  69. * @param[in] ray Ray to check.
  70. * @param[out] hit Information about the hit. Valid only if the method returns true.
  71. * @param[in] maxDist Maximum distance from the ray origin to search for hits.
  72. * @return True if the ray has hit the collider.
  73. */
  74. inline bool rayCast(const Ray& ray, PhysicsQueryHit& hit, float maxDist = FLT_MAX) const;
  75. /**
  76. * Checks does the ray hit this collider.
  77. *
  78. * @param[in] origin Origin of the ray to check.
  79. * @param[in] unitDir Unit direction of the ray to check.
  80. * @param[out] hit Information about the hit. Valid only if the method returns true.
  81. * @param[in] maxDist Maximum distance from the ray origin to search for hits.
  82. * @return True if the ray has hit the collider.
  83. */
  84. inline bool rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
  85. float maxDist = FLT_MAX) const;
  86. /**
  87. * Triggered when some object starts interacting with the collider. Only triggered if proper collision report mode
  88. * is turned on.
  89. */
  90. Event<void(const CollisionData&)> onCollisionBegin;
  91. /**
  92. * Triggered for every frame that an object remains interacting with a collider. Only triggered if proper collision
  93. * report mode is turned on.
  94. */
  95. Event<void(const CollisionData&)> onCollisionStay;
  96. /**
  97. * Triggered when some object stops interacting with the collider. Only triggered if proper collision report mode
  98. * is turned on.
  99. */
  100. Event<void(const CollisionData&)> onCollisionEnd;
  101. /** @name Internal
  102. * @{
  103. */
  104. /** Returns the object containing common collider code. */
  105. FCollider* _getInternal() const { return mInternal; }
  106. /**
  107. * Sets the object that owns this physics object, if any. Used for high level systems so they can easily map their
  108. * high level physics objects from the low level ones returned by various queries and events.
  109. */
  110. void _setOwner(PhysicsOwnerType type, void* owner) { mOwner.type = type; mOwner.ownerData = owner; }
  111. /**
  112. * Gets the object that owns this physics object, if any. Used for high level systems so they can easily map their
  113. * high level physics objects from the low level ones returned by various queries and events.
  114. */
  115. void* _getOwner(PhysicsOwnerType type) const { return mOwner.type == type ? mOwner.ownerData : nullptr; }
  116. /** @} */
  117. protected:
  118. FCollider* mInternal = nullptr;
  119. PhysicsObjectOwner mOwner;
  120. Rigidbody* mRigidbody = nullptr;
  121. Vector3 mScale = Vector3::ONE;
  122. };
  123. /** @} */
  124. }