BsCollider.h 5.5 KB

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