您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

286 行
12 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. //
  12. // The BulletCharacterController class
  13. // ------------------
  14. // This class is a equivalent of btKinematicCharacterController, but more useful for Lol.
  15. //
  16. #if !defined __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__
  17. #define __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__
  18. #include <lol/engine.h>
  19. #include "easyphysics.h"
  20. //#include "BulletDynamics\Character\btCharacterControllerInterface.h"
  21. #define USE_LOL_CTRLR_CHARAC
  22. namespace lol
  23. {
  24. namespace phys
  25. {
  26. #ifdef USE_LOL_CTRLR_CHARAC
  27. //SweepCallback used for Swweep Tests.
  28. class ClosestNotMeConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback
  29. {
  30. public:
  31. ClosestNotMeConvexResultCallback(btCollisionObject* NewMe, const vec3& NewUp, float MinSlopeDot) :
  32. btCollisionWorld::ClosestConvexResultCallback(LOL2BTU_VEC3(vec3(.0f)), LOL2BTU_VEC3(vec3(.0f))),
  33. m_me(NewMe),
  34. m_up(NewUp),
  35. m_min_slope_dot(MinSlopeDot) { }
  36. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult& ConvexResult, bool NormalInWorld)
  37. {
  38. //We hit ourselves, FAIL
  39. if (ConvexResult.m_hitCollisionObject == m_me)
  40. return btScalar(1.f);
  41. vec3 WorldHitNomal(.0f);
  42. if (NormalInWorld)
  43. WorldHitNomal = BT2LOL_VEC3(ConvexResult.m_hitNormalLocal);
  44. else //need to transform Normal into worldspace
  45. {
  46. btVector3 TmpWorldHitNormal = ConvexResult.m_hitCollisionObject->getWorldTransform().getBasis() * ConvexResult.m_hitNormalLocal;
  47. WorldHitNomal = BT2LOL_VEC3(TmpWorldHitNormal);
  48. }
  49. float DotUp = dot(m_up, WorldHitNomal);
  50. //We hit below the accepted slope_dot, FAIL
  51. if (DotUp < m_min_slope_dot)
  52. return btScalar(1.f);
  53. //Continue to next.
  54. return ClosestConvexResultCallback::addSingleResult(ConvexResult, NormalInWorld);
  55. }
  56. protected:
  57. btCollisionObject* m_me;
  58. const vec3 m_up;
  59. float m_min_slope_dot;
  60. };
  61. ///BulletKinematicCharacterController is an object that supports a sliding motion in a world.
  62. ///It uses a ghost object and convex sweep test to test for upcoming collisions. This is combined with discrete collision detection to recover from penetrations.
  63. ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user.
  64. class BulletKinematicCharacterController : public btActionInterface
  65. {
  66. public:
  67. BulletKinematicCharacterController(btPairCachingGhostObject* NewGhostObject, btConvexShape* NewConvexShape, float NewStepHeight, int NewUpAxis=1)
  68. {
  69. m_convex_shape = NewConvexShape;
  70. m_i_up_axis = NewUpAxis;
  71. m_ghost_object = NewGhostObject;
  72. m_step_height = NewStepHeight;
  73. m_added_margin = 0.02f;
  74. m_walk_direction = vec3(.0f, .0f, .0f);
  75. m_do_gobject_sweep_test = true;
  76. m_turn_angle = .0f;
  77. m_use_walk_direction = false; // Should remove walk direction, this doesn't work correctly.
  78. m_velocity_time_interval = .0f;
  79. m_vertical_velocity = .0f;
  80. m_vertical_offset = .0f;
  81. m_f_gravity = 9.8f * 3.f; // 3G acceleration.
  82. m_fall_speed = 55.f; // Terminal velocity of a sky diver in m/s.
  83. m_jump_speed = 10.f; // ?
  84. m_was_on_ground = false;
  85. m_was_jumping = false;
  86. SetMaxSlope(45.f);
  87. }
  88. ~BulletKinematicCharacterController() { }
  89. protected:
  90. static vec3* GetUpAxisDirections()
  91. {
  92. static vec3 sUpAxisDirection[3] = { vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f) };
  93. return sUpAxisDirection;
  94. }
  95. //--------------------------
  96. //CONVENIENCE FUNCTIONS
  97. //--
  98. //Returns the reflection Direction of a ray going 'Direction' hitting a surface with Normal 'Normal' from: http://www-cs-students.stanford.edu/~adityagp/final/node3.html
  99. vec3 GetReflectedDir(const vec3& Direction, const vec3& Normal)
  100. {
  101. return Direction - (2.f * dot(Direction, Normal) * Normal);
  102. }
  103. //Returns the portion of 'direction' that is parallel to 'normal'
  104. vec3 ProjectDirOnNorm(const vec3& Direction, const vec3& Normal)
  105. {
  106. return Normal * dot(Direction, Normal);
  107. }
  108. //Returns the portion of 'Direction' that is perpindicular to 'Normal'
  109. vec3 ProjectDirOnNormPerpindicular(const vec3& Direction, const vec3& Normal)
  110. {
  111. return Direction - ProjectDirOnNorm(Direction, Normal);
  112. }
  113. //Returns Ghost Object. -duh-
  114. btPairCachingGhostObject* GetGhostObject()
  115. {
  116. return m_ghost_object;
  117. }
  118. //"Real" war functions
  119. bool RecoverFromPenetration(btCollisionWorld* CollisionWorld);
  120. void UpdateTargetOnHit(const vec3& hit_normal, float TangentMag = .0f, float NormalMag = 1.f);
  121. void DoMove(btCollisionWorld* CollisionWorld, const vec3& MoveStep, float DeltaTime);
  122. public:
  123. ///btActionInterface interface : KEEP IN camelCase
  124. virtual void updateAction(btCollisionWorld* CollisionWorld, float deltaTime)
  125. {
  126. PreStep(CollisionWorld);
  127. PlayerStep(CollisionWorld, deltaTime);
  128. }
  129. //not in the interface, but called above
  130. void PreStep(btCollisionWorld* CollisionWorld);
  131. void PlayerStep(btCollisionWorld* CollisionWorld, float DeltaTime);
  132. ///btActionInterface interface : KEEP IN camelCase
  133. void debugDraw(btIDebugDraw* debugDrawer) { }
  134. void SetUpAxis(int NewAxis)
  135. {
  136. if (NewAxis < 0)
  137. NewAxis = 0;
  138. if (NewAxis > 2)
  139. NewAxis = 2;
  140. m_i_up_axis = NewAxis;
  141. }
  142. //!!!!!! SHOULD DITCH THAT !!!!!!
  143. //This should probably be called setPositionIncrementPerSimulatorStep.
  144. //This is neither a Direction nor a velocity, but the amount to
  145. //increment the position each simulation iteration, regardless
  146. //of DeltaTime.
  147. //This call will Reset any velocity set by SetVelocityForTimeInterval().
  148. virtual void SetWalkDirection(const vec3& walkDirection)
  149. {
  150. m_use_walk_direction = true;
  151. m_walk_direction = walkDirection;
  152. m_normalized_direction = normalize(m_walk_direction);
  153. }
  154. //Caller provides a velocity with which the character should MoveStep for
  155. //the given time period. After the time period, velocity is Reset
  156. //to zero.
  157. //This call will Reset any walk Direction set by SetWalkDirection().
  158. //Negative time intervals will result in no motion.
  159. virtual void SetVelocityForTimeInterval(const vec3& velocity, float timeInterval)
  160. {
  161. m_use_walk_direction = false;
  162. m_walk_direction = velocity;
  163. m_normalized_direction = normalize(m_walk_direction);
  164. m_velocity_time_interval = timeInterval;
  165. }
  166. //Usefulness ?
  167. void Reset() { }
  168. void Warp(const vec3& NewOrigin)
  169. {
  170. btTransform NewTransform;
  171. NewTransform.setIdentity();
  172. NewTransform.setOrigin(LOL2BTU_VEC3(NewOrigin));
  173. m_ghost_object->setWorldTransform(NewTransform);
  174. }
  175. //External Setup
  176. //--
  177. void SetFallSpeed(float NewFallSpeed) { m_fall_speed = NewFallSpeed; }
  178. void SetJumpSpeed(float NewJumpSpeed) { m_jump_speed = NewJumpSpeed; }
  179. void SetMaxJumpHeight(float NewMaxJumpHeight) { m_max_jump_height = NewMaxJumpHeight; }
  180. //Jump logic will go in EasyCC
  181. bool CanJump() const { return OnGround(); }
  182. void Jump();
  183. //NewGravity functions
  184. void SetGravity(float NewGravity) { m_f_gravity = NewGravity; }
  185. float GetGravity() const { return m_f_gravity; }
  186. //The max slope determines the maximum angle that the controller can walk up.
  187. //The slope angle is measured in radians.
  188. void SetMaxSlope(float NewSlopeRadians) { m_max_slope_radians = NewSlopeRadians; m_max_slope_cosine = lol::cos(NewSlopeRadians); }
  189. float GetMaxSlope() const { return m_max_slope_radians; }
  190. void SetUseGhostSweepTest(bool UseGObjectSweepTest) { m_do_gobject_sweep_test = UseGObjectSweepTest; }
  191. bool OnGround() const { return m_vertical_velocity == .0f && m_vertical_offset == .0f; }
  192. private:
  193. btPairCachingGhostObject* m_ghost_object;
  194. btConvexShape* m_convex_shape; //is also in m_ghost_object, but it needs to be convex, so we store it here to avoid upcast
  195. //keep track of the contact manifolds
  196. btManifoldArray m_manifold_array;
  197. float m_half_height;
  198. float m_velocity_time_interval;
  199. float m_vertical_velocity;
  200. float m_vertical_offset;
  201. float m_fall_speed;
  202. float m_jump_speed;
  203. float m_max_jump_height;
  204. float m_max_slope_radians; // Slope angle that is set (used for returning the exact value)
  205. float m_max_slope_cosine; // Cosine equivalent of m_max_slope_radians (calculated once when set, for optimization)
  206. float m_f_gravity;
  207. float m_turn_angle;
  208. float m_step_height;
  209. float m_added_margin;//@todo: remove this and fix the code
  210. ///this is the desired walk Direction, set by the user
  211. vec3 m_walk_direction;
  212. vec3 m_normalized_direction;
  213. //some internal variables
  214. vec3 m_current_position;
  215. float m_current_step_offset;
  216. vec3 m_target_position;
  217. vec3 m_touching_normal;
  218. bool m_touching_contact;
  219. bool m_was_on_ground;
  220. bool m_was_jumping;
  221. bool m_do_gobject_sweep_test;
  222. bool m_use_walk_direction;
  223. int m_i_up_axis;
  224. //---------------------------------------------------------------------
  225. //NEW INTERNAL VARS
  226. //---------------------------------------------------------------------
  227. //Gravity in vec3
  228. vec3 m_gravity;
  229. //Current Velocity
  230. vec3 m_velocity;
  231. };
  232. #endif // USE_LOL_CTRLR_CHARAC
  233. } /* namespace phys */
  234. } /* namespace lol */
  235. #endif /* __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__ */