BsCharacterController.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "Physics/BsPhysicsCommon.h"
  6. #include "Math/BsVector3.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Physics
  10. * @{
  11. */
  12. /**
  13. * Controls climbing behaviour for a capsule character controller. Normally the character controller will not
  14. * automatically climb when heights are greater than the assigned step offset. However due to the shape of the capsule
  15. * it might automatically climb over slightly larger heights than assigned step offsets.
  16. */
  17. enum class BS_SCRIPT_EXPORT(m:Physics) CharacterClimbingMode
  18. {
  19. Normal, /**< Normal behaviour. Capsule character controller will be able to auto-step even above the step offset. */
  20. Constrained /**< The system will attempt to limit auto-step to the provided step offset and no higher. */
  21. };
  22. /** Controls behaviour when a character controller reaches a slope thats larger than its slope offset. */
  23. enum class BS_SCRIPT_EXPORT(m:Physics) CharacterNonWalkableMode
  24. {
  25. Prevent, /**< Character will be prevented from going further, but will be allowed to move laterally. */
  26. PreventAndSlide /**< Character will be prevented from going further, but also slide down the slope. */
  27. };
  28. /** Reports in which directions is the character colliding with other objects. */
  29. enum class BS_SCRIPT_EXPORT(m:Physics) CharacterCollisionFlag
  30. {
  31. Sides = 0x1, /**< Character is colliding with its sides. */
  32. Up = 0x2, /**< Character is colliding with the ceiling. */
  33. Down = 0x4 /**< Character is colliding with the ground. */
  34. };
  35. /** @copydoc CharacterCollisionFlag */
  36. typedef Flags<CharacterCollisionFlag> CharacterCollisionFlags;
  37. BS_FLAGS_OPERATORS(CharacterCollisionFlag)
  38. struct CHAR_CONTROLLER_DESC;
  39. struct ControllerColliderCollision;
  40. struct ControllerControllerCollision;
  41. /**
  42. * Special physics controller meant to be used for game characters. Uses the "slide-and-collide" physics instead of
  43. * of the standard physics model to handle various issues with manually moving kinematic objects. Uses a capsule to
  44. * represent the character's bounds.
  45. */
  46. class BS_CORE_EXPORT CharacterController
  47. {
  48. public:
  49. CharacterController(const CHAR_CONTROLLER_DESC& desc) { }
  50. virtual ~CharacterController() { }
  51. /**
  52. * Moves the controller in the specified direction by the specified amount, while interacting with surrounding
  53. * geometry. Returns flags signaling where collision occurred after the movement.
  54. *
  55. * Does not account for gravity, you must apply it manually.
  56. */
  57. virtual CharacterCollisionFlags move(const Vector3& displacement) = 0;
  58. /** Returns position of the center of the controller. */
  59. virtual Vector3 getPosition() const = 0;
  60. /**
  61. * Sets position of the center of the controller. This will teleport the character to the location. Use move()
  62. * for movement that includes physics.
  63. */
  64. virtual void setPosition(const Vector3& position) = 0;
  65. /** @copydoc setFootPosition() */
  66. virtual Vector3 getFootPosition() const = 0;
  67. /**
  68. * Determines the position of the bottom of the controller. Position takes contact offset into account. Changing
  69. * this will teleport the character to the location. Use move() for movement that includes physics.
  70. */
  71. virtual void setFootPosition(const Vector3& position) = 0;
  72. /** @copydoc setRadius() */
  73. virtual float getRadius() const = 0;
  74. /** Determines the radius of the controller capsule. */
  75. virtual void setRadius(float radius) = 0;
  76. /** @copydoc setHeight() */
  77. virtual float getHeight() const = 0;
  78. /** Determines the height between the centers of the two spheres of the controller capsule. */
  79. virtual void setHeight(float height) = 0;
  80. /** @copydoc setUp() */
  81. virtual Vector3 getUp() const = 0;
  82. /** Determines the up direction of capsule. Determines capsule orientation. */
  83. virtual void setUp(const Vector3& up) = 0;
  84. /** @copydoc CHAR_CONTROLLER_DESC::climbingMode */
  85. virtual CharacterClimbingMode getClimbingMode() const = 0;
  86. /** @copydoc CHAR_CONTROLLER_DESC::climbingMode */
  87. virtual void setClimbingMode(CharacterClimbingMode mode) = 0;
  88. /** @copydoc CHAR_CONTROLLER_DESC::nonWalkableMode */
  89. virtual CharacterNonWalkableMode getNonWalkableMode() const = 0;
  90. /** @copydoc CHAR_CONTROLLER_DESC::nonWalkableMode */
  91. virtual void setNonWalkableMode(CharacterNonWalkableMode mode) = 0;
  92. /** @copydoc CHAR_CONTROLLER_DESC::minMoveDistance */
  93. virtual float getMinMoveDistance() const = 0;
  94. /** @copydoc CHAR_CONTROLLER_DESC::minMoveDistance */
  95. virtual void setMinMoveDistance(float value) = 0;
  96. /** @copydoc CHAR_CONTROLLER_DESC::contactOffset */
  97. virtual float getContactOffset() const = 0;
  98. /** @copydoc CHAR_CONTROLLER_DESC::contactOffset */
  99. virtual void setContactOffset(float value) = 0;
  100. /** @copydoc CHAR_CONTROLLER_DESC::stepOffset */
  101. virtual float getStepOffset() const = 0;
  102. /** @copydoc CHAR_CONTROLLER_DESC::stepOffset */
  103. virtual void setStepOffset(float value) = 0;
  104. /** @copydoc CHAR_CONTROLLER_DESC::slopeLimit */
  105. virtual Radian getSlopeLimit() const = 0;
  106. /** @copydoc CHAR_CONTROLLER_DESC::slopeLimit */
  107. virtual void setSlopeLimit(Radian value) = 0;
  108. /** Determines the layer that controls what can the controller collide with. */
  109. virtual void setLayer(UINT64 layer) { mLayer = layer; }
  110. /** @copydoc setLayer() */
  111. virtual UINT64 getLayer() const { return mLayer; }
  112. /** Creates a new character controller. */
  113. static SPtr<CharacterController> create(const CHAR_CONTROLLER_DESC& desc);
  114. /** Triggered when the controller hits a collider. */
  115. Event<void(const ControllerColliderCollision&)> onColliderHit;
  116. /** Triggered when the controller hits another character controller. */
  117. Event<void(const ControllerControllerCollision&)> onControllerHit;
  118. /** @name Internal
  119. * @{
  120. */
  121. /**
  122. * Sets the object that owns this physics object, if any. Used for high level systems so they can easily map their
  123. * high level physics objects from the low level ones returned by various queries and events.
  124. */
  125. void _setOwner(PhysicsOwnerType type, void* owner) { mOwner.type = type; mOwner.ownerData = owner; }
  126. /**
  127. * Gets the object that owns this physics object, if any. Used for high level systems so they can easily map their
  128. * high level physics objects from the low level ones returned by various queries and events.
  129. */
  130. void* _getOwner(PhysicsOwnerType type) const { return mOwner.type == type ? mOwner.ownerData : nullptr; }
  131. /** @} */
  132. private:
  133. PhysicsObjectOwner mOwner;
  134. UINT64 mLayer = 1;
  135. };
  136. /** Contains all the information required for initializing a character controller. */
  137. struct CHAR_CONTROLLER_DESC
  138. {
  139. /** Center of the controller capsule */
  140. Vector3 position = Vector3::ZERO;
  141. /**
  142. * Contact offset specifies a skin around the object within which contacts will be generated. It should be a small
  143. * positive non-zero value.
  144. */
  145. float contactOffset = 0.1f;
  146. /**
  147. * Controls which obstacles will the character be able to automatically step over without being stopped. This is the
  148. * height of the maximum obstacle that will be stepped over (with exceptions, see climbingMode).
  149. */
  150. float stepOffset = 0.5f;
  151. /**
  152. * Controls which slopes should the character consider too steep and won't be able to move over. See
  153. * nonWalkableMode for more information.
  154. */
  155. Radian slopeLimit = Degree(45.0f);
  156. /**
  157. * Represents minimum distance that the character will move during a call to move(). This is used to stop the
  158. * recursive motion algorithm when the remaining distance is too small.
  159. */
  160. float minMoveDistance = 0.0f;
  161. /** Height between the centers of the two spheres of the controller capsule. */
  162. float height = 0.0f;
  163. /** Radius of the controller capsule. */
  164. float radius = 1.0f;
  165. /** Up direction of controller capsule. Determines capsule orientation. */
  166. Vector3 up = Vector3::UNIT_Y;
  167. /**
  168. * Controls what happens when character encounters a height higher than its step offset.
  169. *
  170. * @see CharacterClimbingMode
  171. */
  172. CharacterClimbingMode climbingMode = CharacterClimbingMode::Normal;
  173. /**
  174. * Controls what happens when character encounters a slope higher than its slope offset.
  175. *
  176. * @see CharacterNonWalkableMode
  177. */
  178. CharacterNonWalkableMode nonWalkableMode = CharacterNonWalkableMode::Prevent;
  179. };
  180. /** Contains data about a collision of a character controller and another object. */
  181. struct BS_SCRIPT_EXPORT(m:Physics,pl:true) ControllerCollision
  182. {
  183. Vector3 position; /**< Contact position. */
  184. Vector3 normal; /**< Contact normal. */
  185. Vector3 motionDir; /**< Direction of motion after the hit. */
  186. float motionAmount; /**< Magnitude of motion after the hit. */
  187. };
  188. /** Contains data about a collision of a character controller and a collider. */
  189. struct BS_SCRIPT_EXPORT(m:Physics,pl:true) ControllerColliderCollision : ControllerCollision
  190. {
  191. /**
  192. * Component of the controller that was touched. Can be null if the controller has no component parent, in which
  193. * case check #colliderRaw.
  194. */
  195. HCollider collider;
  196. BS_SCRIPT_EXPORT(ex:true)
  197. Collider* colliderRaw; /**< Collider that was touched. */
  198. UINT32 triangleIndex; /**< Touched triangle index for mesh colliders. */
  199. };
  200. /** Contains data about a collision between two character controllers. */
  201. struct BS_SCRIPT_EXPORT(m:Physics,pl:true) ControllerControllerCollision : ControllerCollision
  202. {
  203. /**
  204. * Component of the controller that was touched. Can be null if the controller has no component parent, in which
  205. * case check #controllerRaw.
  206. */
  207. HCharacterController controller;
  208. BS_SCRIPT_EXPORT(ex:true)
  209. CharacterController* controllerRaw; /**< Controller that was touched. */
  210. };
  211. /** @} */
  212. }