btKinematicCharacterController.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_KINEMATIC_CHARACTER_CONTROLLER_H
  14. #define BT_KINEMATIC_CHARACTER_CONTROLLER_H
  15. #include "LinearMath/btVector3.h"
  16. #include "btCharacterControllerInterface.h"
  17. #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
  18. class btCollisionShape;
  19. class btConvexShape;
  20. class btRigidBody;
  21. class btCollisionWorld;
  22. class btCollisionDispatcher;
  23. class btPairCachingGhostObject;
  24. ///btKinematicCharacterController is an object that supports a sliding motion in a world.
  25. ///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.
  26. ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user.
  27. ATTRIBUTE_ALIGNED16(class)
  28. btKinematicCharacterController : public btCharacterControllerInterface
  29. {
  30. protected:
  31. btScalar m_halfHeight;
  32. btPairCachingGhostObject* m_ghostObject;
  33. btConvexShape* m_convexShape; //is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast
  34. btScalar m_maxPenetrationDepth;
  35. btScalar m_verticalVelocity;
  36. btScalar m_verticalOffset;
  37. btScalar m_fallSpeed;
  38. btScalar m_jumpSpeed;
  39. btScalar m_SetjumpSpeed;
  40. btScalar m_maxJumpHeight;
  41. btScalar m_maxSlopeRadians; // Slope angle that is set (used for returning the exact value)
  42. btScalar m_maxSlopeCosine; // Cosine equivalent of m_maxSlopeRadians (calculated once when set, for optimization)
  43. btScalar m_gravity;
  44. btScalar m_turnAngle;
  45. btScalar m_stepHeight;
  46. btScalar m_addedMargin; //@todo: remove this and fix the code
  47. ///this is the desired walk direction, set by the user
  48. btVector3 m_walkDirection;
  49. btVector3 m_normalizedDirection;
  50. btVector3 m_AngVel;
  51. btVector3 m_jumpPosition;
  52. //some internal variables
  53. btVector3 m_currentPosition;
  54. btScalar m_currentStepOffset;
  55. btVector3 m_targetPosition;
  56. btQuaternion m_currentOrientation;
  57. btQuaternion m_targetOrientation;
  58. ///keep track of the contact manifolds
  59. btManifoldArray m_manifoldArray;
  60. bool m_touchingContact;
  61. btVector3 m_touchingNormal;
  62. btScalar m_linearDamping;
  63. btScalar m_angularDamping;
  64. bool m_wasOnGround;
  65. bool m_wasJumping;
  66. bool m_useGhostObjectSweepTest;
  67. bool m_useWalkDirection;
  68. btScalar m_velocityTimeInterval;
  69. btVector3 m_up;
  70. btVector3 m_jumpAxis;
  71. static btVector3* getUpAxisDirections();
  72. bool m_interpolateUp;
  73. bool full_drop;
  74. bool bounce_fix;
  75. btVector3 computeReflectionDirection(const btVector3& direction, const btVector3& normal);
  76. btVector3 parallelComponent(const btVector3& direction, const btVector3& normal);
  77. btVector3 perpindicularComponent(const btVector3& direction, const btVector3& normal);
  78. bool recoverFromPenetration(btCollisionWorld * collisionWorld);
  79. void stepUp(btCollisionWorld * collisionWorld);
  80. void updateTargetPositionBasedOnCollision(const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
  81. void stepForwardAndStrafe(btCollisionWorld * collisionWorld, const btVector3& walkMove);
  82. void stepDown(btCollisionWorld * collisionWorld, btScalar dt);
  83. virtual bool needsCollision(const btCollisionObject* body0, const btCollisionObject* body1);
  84. void setUpVector(const btVector3& up);
  85. btQuaternion getRotation(btVector3 & v0, btVector3 & v1) const;
  86. public:
  87. BT_DECLARE_ALIGNED_ALLOCATOR();
  88. btKinematicCharacterController(btPairCachingGhostObject * ghostObject, btConvexShape * convexShape, btScalar stepHeight, const btVector3& up = btVector3(1.0, 0.0, 0.0));
  89. ~btKinematicCharacterController();
  90. ///btActionInterface interface
  91. virtual void updateAction(btCollisionWorld * collisionWorld, btScalar deltaTime)
  92. {
  93. preStep(collisionWorld);
  94. playerStep(collisionWorld, deltaTime);
  95. }
  96. ///btActionInterface interface
  97. void debugDraw(btIDebugDraw * debugDrawer);
  98. void setUp(const btVector3& up);
  99. const btVector3& getUp() { return m_up; }
  100. /// This should probably be called setPositionIncrementPerSimulatorStep.
  101. /// This is neither a direction nor a velocity, but the amount to
  102. /// increment the position each simulation iteration, regardless
  103. /// of dt.
  104. /// This call will reset any velocity set by setVelocityForTimeInterval().
  105. virtual void setWalkDirection(const btVector3& walkDirection);
  106. /// Caller provides a velocity with which the character should move for
  107. /// the given time period. After the time period, velocity is reset
  108. /// to zero.
  109. /// This call will reset any walk direction set by setWalkDirection().
  110. /// Negative time intervals will result in no motion.
  111. virtual void setVelocityForTimeInterval(const btVector3& velocity,
  112. btScalar timeInterval);
  113. virtual void setAngularVelocity(const btVector3& velocity);
  114. virtual const btVector3& getAngularVelocity() const;
  115. virtual void setLinearVelocity(const btVector3& velocity);
  116. virtual btVector3 getLinearVelocity() const;
  117. void setLinearDamping(btScalar d) { m_linearDamping = btClamped(d, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0)); }
  118. btScalar getLinearDamping() const { return m_linearDamping; }
  119. void setAngularDamping(btScalar d) { m_angularDamping = btClamped(d, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0)); }
  120. btScalar getAngularDamping() const { return m_angularDamping; }
  121. void reset(btCollisionWorld * collisionWorld);
  122. void warp(const btVector3& origin);
  123. void preStep(btCollisionWorld * collisionWorld);
  124. void playerStep(btCollisionWorld * collisionWorld, btScalar dt);
  125. void setStepHeight(btScalar h);
  126. btScalar getStepHeight() const { return m_stepHeight; }
  127. void setFallSpeed(btScalar fallSpeed);
  128. btScalar getFallSpeed() const { return m_fallSpeed; }
  129. void setJumpSpeed(btScalar jumpSpeed);
  130. btScalar getJumpSpeed() const { return m_jumpSpeed; }
  131. void setMaxJumpHeight(btScalar maxJumpHeight);
  132. bool canJump() const;
  133. void jump(const btVector3& v = btVector3(0, 0, 0));
  134. void applyImpulse(const btVector3& v) { jump(v); }
  135. void setGravity(const btVector3& gravity);
  136. btVector3 getGravity() const;
  137. /// The max slope determines the maximum angle that the controller can walk up.
  138. /// The slope angle is measured in radians.
  139. void setMaxSlope(btScalar slopeRadians);
  140. btScalar getMaxSlope() const;
  141. void setMaxPenetrationDepth(btScalar d);
  142. btScalar getMaxPenetrationDepth() const;
  143. btPairCachingGhostObject* getGhostObject();
  144. void setUseGhostSweepTest(bool useGhostObjectSweepTest)
  145. {
  146. m_useGhostObjectSweepTest = useGhostObjectSweepTest;
  147. }
  148. bool onGround() const;
  149. void setUpInterpolate(bool value);
  150. };
  151. #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H