2
0

BsCharacterController.h 9.3 KB

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