BsCollider.h 5.5 KB

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