BsCharacterController.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. namespace BansheeEngine
  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 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 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 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. /** Returns position of the bottom of the controller. Position takes contact offset into account. */
  66. virtual Vector3 getFootPosition() const = 0;
  67. /**
  68. * Sets position of the bottom of the controller. Position takes contact offset into account. This will teleport the
  69. * character to the location. Use move() for movement that includes physics.
  70. */
  71. virtual void setFootPosition(const Vector3& position) = 0;
  72. /** Returns the radius of the controller capsule. */
  73. virtual float getRadius() const = 0;
  74. /** Sets the radius of the controller capsule. */
  75. virtual void setRadius(float radius) = 0;
  76. /** Returns the height between the centers of the two spheres of the controller capsule. */
  77. virtual float getHeight() const = 0;
  78. /** Sets the height between the centers of the two spheres of the controller capsule. */
  79. virtual void setHeight(float height) = 0;
  80. /** Returns the up direction of capsule. Determines capsule orientation. */
  81. virtual Vector3 getUp() const = 0;
  82. /** Sets the up direction of capsule. Determines capsule orientation. */
  83. virtual void setUp(const Vector3& up) = 0;
  84. /**
  85. * Returns climbing mode.
  86. *
  87. * @copydoc CHAR_CONTROLLER_DESC::climbingMode
  88. */
  89. virtual CharacterClimbingMode getClimbingMode() const = 0;
  90. /**
  91. * Sets climbing mode.
  92. *
  93. * @copydoc CHAR_CONTROLLER_DESC::climbingMode
  94. */
  95. virtual void setClimbingMode(CharacterClimbingMode mode) = 0;
  96. /**
  97. * Returns non-walkable mode.
  98. *
  99. * @copydoc CHAR_CONTROLLER_DESC::nonWalkableMode
  100. */
  101. virtual CharacterNonWalkableMode getNonWalkableMode() const = 0;
  102. /**
  103. * Sets non-walkable mode.
  104. *
  105. * @copydoc CHAR_CONTROLLER_DESC::nonWalkableMode
  106. */
  107. virtual void setNonWalkableMode(CharacterNonWalkableMode mode) = 0;
  108. /**
  109. * Returns minimum move distance.
  110. *
  111. * @copydoc CHAR_CONTROLLER_DESC::minMoveDistance
  112. */
  113. virtual float getMinMoveDistance() const = 0;
  114. /**
  115. * Sets minimum move distance.
  116. *
  117. * @copydoc CHAR_CONTROLLER_DESC::minMoveDistance
  118. */
  119. virtual void setMinMoveDistance(float value) = 0;
  120. /**
  121. * Returns the contact offset.
  122. *
  123. * @copydoc CHAR_CONTROLLER_DESC::contactOffset
  124. */
  125. virtual float getContactOffset() const = 0;
  126. /**
  127. * Sets the contact offset.
  128. *
  129. * @copydoc CHAR_CONTROLLER_DESC::contactOffset
  130. */
  131. virtual void setContactOffset(float value) = 0;
  132. /**
  133. * Returns the step offset.
  134. *
  135. * @copydoc CHAR_CONTROLLER_DESC::stepOffset
  136. */
  137. virtual float getStepOffset() const = 0;
  138. /**
  139. * Sets the step offset.
  140. *
  141. * @copydoc CHAR_CONTROLLER_DESC::stepOffset
  142. */
  143. virtual void setStepOffset(float value) = 0;
  144. /**
  145. * Returns the slope angle.
  146. *
  147. * @copydoc CHAR_CONTROLLER_DESC::slopeLimit
  148. */
  149. virtual Radian getSlopeLimit() const = 0;
  150. /**
  151. * Sets the slope angle.
  152. *
  153. * @copydoc CHAR_CONTROLLER_DESC::slopeLimit
  154. */
  155. virtual void setSlopeLimit(Radian value) = 0;
  156. /** Sets the layer that controls what can the controller collide with. */
  157. virtual void setLayer(UINT64 layer) { mLayer = layer; }
  158. /** Gets the layer that controls what can the controller collide with. */
  159. virtual UINT64 getLayer() const { return mLayer; }
  160. /** Creates a new character controller. */
  161. static SPtr<CharacterController> create(const CHAR_CONTROLLER_DESC& desc);
  162. /** Triggered when the controller hits a collider. */
  163. Event<void(const ControllerColliderCollision&)> onColliderHit;
  164. /** Triggered when the controller hits another character controller. */
  165. Event<void(const ControllerControllerCollision&)> onControllerHit;
  166. /** @name Internal
  167. * @{
  168. */
  169. /**
  170. * Sets the object that owns this physics object, if any. Used for high level systems so they can easily map their
  171. * high level physics objects from the low level ones returned by various queries and events.
  172. */
  173. void _setOwner(PhysicsOwnerType type, void* owner) { mOwner.type = type; mOwner.ownerData = owner; }
  174. /**
  175. * Gets the object that owns this physics object, if any. Used for high level systems so they can easily map their
  176. * high level physics objects from the low level ones returned by various queries and events.
  177. */
  178. void* _getOwner(PhysicsOwnerType type) const { return mOwner.type == type ? mOwner.ownerData : nullptr; }
  179. /** @} */
  180. private:
  181. PhysicsObjectOwner mOwner;
  182. UINT64 mLayer = 1;
  183. };
  184. /** Contains all the information required for initializing a character controller. */
  185. struct CHAR_CONTROLLER_DESC
  186. {
  187. /** Center of the controller capsule */
  188. Vector3 position = Vector3::ZERO;
  189. /**
  190. * Contact offset specifies a skin around the object within which contacts will be generated. It should be a small
  191. * positive non-zero value.
  192. */
  193. float contactOffset = 0.1f;
  194. /**
  195. * Controls which obstacles will the character be able to automatically step over without being stopped. This is the
  196. * height of the maximum obstacle that will be stepped over (with exceptions, see climbingMode).
  197. */
  198. float stepOffset = 0.5f;
  199. /**
  200. * Controls which slopes should the character consider too steep and won't be able to move over. See
  201. * nonWalkableMode for more information.
  202. */
  203. Radian slopeLimit = Degree(45.0f);
  204. /**
  205. * Represents minimum distance that the character will move during a call to move(). This is used to stop the
  206. * recursive motion algorithm when the remaining distance is too small.
  207. */
  208. float minMoveDistance = 0.0f;
  209. /** Height between the centers of the two spheres of the controller capsule. */
  210. float height = 0.0f;
  211. /** Radius of the controller capsule. */
  212. float radius = 1.0f;
  213. /** Up direction of controller capsule. Determines capsule orientation. */
  214. Vector3 up = Vector3::UNIT_Y;
  215. /**
  216. * Controls what happens when character encounters a height higher than its step offset.
  217. *
  218. * @see CharacterClimbingMode
  219. */
  220. CharacterClimbingMode climbingMode = CharacterClimbingMode::Normal;
  221. /**
  222. * Controls what happens when character encounters a slope higher than its slope offset.
  223. *
  224. * @see CharacterNonWalkableMode
  225. */
  226. CharacterNonWalkableMode nonWalkableMode = CharacterNonWalkableMode::Prevent;
  227. };
  228. /** Contains data about a collision of a character controller and another object. */
  229. struct ControllerCollision
  230. {
  231. Vector3 position; /**< Contact position. */
  232. Vector3 normal; /**< Contact normal. */
  233. Vector3 motionDir; /**< Direction of motion after the hit. */
  234. float motionAmount; /**< Magnitude of motion after the hit. */
  235. };
  236. /** Contains data about a collision of a character controller and a collider. */
  237. struct ControllerColliderCollision : ControllerCollision
  238. {
  239. /**
  240. * Component of the controller that was touched. Can be null if the controller has no component parent, in which
  241. * case check #colliderRaw.
  242. */
  243. HCollider collider;
  244. Collider* colliderRaw; /**< Collider that was touched. */
  245. UINT32 triangleIndex; /**< Touched triangle index for mesh colliders. */
  246. };
  247. /** Contains data about a collision between two character controllers. */
  248. struct ControllerControllerCollision : ControllerCollision
  249. {
  250. /**
  251. * Component of the controller that was touched. Can be null if the controller has no component parent, in which
  252. * case check #controllerRaw.
  253. */
  254. HCharacterController controller;
  255. CharacterController* controllerRaw; /**< Controller that was touched. */
  256. };
  257. /** @} */
  258. }