Bullet Collision Detection & Physics Library

btKinematicCharacterController.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2008 Erwin Coumans  http://bulletphysics.com
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 
00017 #ifndef BT_KINEMATIC_CHARACTER_CONTROLLER_H
00018 #define BT_KINEMATIC_CHARACTER_CONTROLLER_H
00019 
00020 #include "LinearMath/btVector3.h"
00021 
00022 #include "btCharacterControllerInterface.h"
00023 
00024 #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
00025 
00026 
00027 class btCollisionShape;
00028 class btConvexShape;
00029 class btRigidBody;
00030 class btCollisionWorld;
00031 class btCollisionDispatcher;
00032 class btPairCachingGhostObject;
00033 
00037 class btKinematicCharacterController : public btCharacterControllerInterface
00038 {
00039 protected:
00040 
00041         btScalar m_halfHeight;
00042         
00043         btPairCachingGhostObject* m_ghostObject;
00044         btConvexShape*  m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast
00045         
00046         btScalar m_verticalVelocity;
00047         btScalar m_verticalOffset;
00048         btScalar m_fallSpeed;
00049         btScalar m_jumpSpeed;
00050         btScalar m_maxJumpHeight;
00051         btScalar m_maxSlopeRadians; // Slope angle that is set (used for returning the exact value)
00052         btScalar m_maxSlopeCosine;  // Cosine equivalent of m_maxSlopeRadians (calculated once when set, for optimization)
00053         btScalar m_gravity;
00054 
00055         btScalar m_turnAngle;
00056         
00057         btScalar m_stepHeight;
00058 
00059         btScalar        m_addedMargin;//@todo: remove this and fix the code
00060 
00062         btVector3       m_walkDirection;
00063         btVector3       m_normalizedDirection;
00064 
00065         //some internal variables
00066         btVector3 m_currentPosition;
00067         btScalar  m_currentStepOffset;
00068         btVector3 m_targetPosition;
00069 
00071         btManifoldArray m_manifoldArray;
00072 
00073         bool m_touchingContact;
00074         btVector3 m_touchingNormal;
00075 
00076         bool  m_wasOnGround;
00077         bool  m_wasJumping;
00078         bool    m_useGhostObjectSweepTest;
00079         bool    m_useWalkDirection;
00080         btScalar        m_velocityTimeInterval;
00081         int m_upAxis;
00082 
00083         static btVector3* getUpAxisDirections();
00084 
00085         btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal);
00086         btVector3 parallelComponent (const btVector3& direction, const btVector3& normal);
00087         btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal);
00088 
00089         bool recoverFromPenetration ( btCollisionWorld* collisionWorld);
00090         void stepUp (btCollisionWorld* collisionWorld);
00091         void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
00092         void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove);
00093         void stepDown (btCollisionWorld* collisionWorld, btScalar dt);
00094 public:
00095         btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1);
00096         ~btKinematicCharacterController ();
00097         
00098 
00100         virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
00101         {
00102                 preStep ( collisionWorld);
00103                 playerStep (collisionWorld, deltaTime);
00104         }
00105         
00107         void    debugDraw(btIDebugDraw* debugDrawer);
00108 
00109         void setUpAxis (int axis)
00110         {
00111                 if (axis < 0)
00112                         axis = 0;
00113                 if (axis > 2)
00114                         axis = 2;
00115                 m_upAxis = axis;
00116         }
00117 
00123         virtual void    setWalkDirection(const btVector3& walkDirection);
00124 
00130         virtual void setVelocityForTimeInterval(const btVector3& velocity,
00131                                 btScalar timeInterval);
00132 
00133         void reset ();
00134         void warp (const btVector3& origin);
00135 
00136         void preStep (  btCollisionWorld* collisionWorld);
00137         void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
00138 
00139         void setFallSpeed (btScalar fallSpeed);
00140         void setJumpSpeed (btScalar jumpSpeed);
00141         void setMaxJumpHeight (btScalar maxJumpHeight);
00142         bool canJump () const;
00143 
00144         void jump ();
00145 
00146         void setGravity(btScalar gravity);
00147         btScalar getGravity() const;
00148 
00151         void setMaxSlope(btScalar slopeRadians);
00152         btScalar getMaxSlope() const;
00153 
00154         btPairCachingGhostObject* getGhostObject();
00155         void    setUseGhostSweepTest(bool useGhostObjectSweepTest)
00156         {
00157                 m_useGhostObjectSweepTest = useGhostObjectSweepTest;
00158         }
00159 
00160         bool onGround () const;
00161 };
00162 
00163 #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H