BsCharacterController.h 9.4 KB

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