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 ATTRIBUTE_ALIGNED16(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         bool  m_interpolateUp;
00085         bool  full_drop;
00086 
00087         btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal);
00088         btVector3 parallelComponent (const btVector3& direction, const btVector3& normal);
00089         btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal);
00090 
00091         bool recoverFromPenetration ( btCollisionWorld* collisionWorld);
00092         void stepUp (btCollisionWorld* collisionWorld);
00093         void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
00094         void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove);
00095         void stepDown (btCollisionWorld* collisionWorld, btScalar dt);
00096 public:
00097 
00098         BT_DECLARE_ALIGNED_ALLOCATOR();
00099 
00100         btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1);
00101         ~btKinematicCharacterController ();
00102         
00103 
00105         virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
00106         {
00107                 preStep ( collisionWorld);
00108                 playerStep (collisionWorld, deltaTime);
00109         }
00110         
00112         void    debugDraw(btIDebugDraw* debugDrawer);
00113 
00114         void setUpAxis (int axis)
00115         {
00116                 if (axis < 0)
00117                         axis = 0;
00118                 if (axis > 2)
00119                         axis = 2;
00120                 m_upAxis = axis;
00121         }
00122 
00128         virtual void    setWalkDirection(const btVector3& walkDirection);
00129 
00135         virtual void setVelocityForTimeInterval(const btVector3& velocity,
00136                                 btScalar timeInterval);
00137 
00138         void reset ( btCollisionWorld* collisionWorld );
00139         void warp (const btVector3& origin);
00140 
00141         void preStep (  btCollisionWorld* collisionWorld);
00142         void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
00143 
00144         void setFallSpeed (btScalar fallSpeed);
00145         void setJumpSpeed (btScalar jumpSpeed);
00146         void setMaxJumpHeight (btScalar maxJumpHeight);
00147         bool canJump () const;
00148 
00149         void jump ();
00150 
00151         void setGravity(btScalar gravity);
00152         btScalar getGravity() const;
00153 
00156         void setMaxSlope(btScalar slopeRadians);
00157         btScalar getMaxSlope() const;
00158 
00159         btPairCachingGhostObject* getGhostObject();
00160         void    setUseGhostSweepTest(bool useGhostObjectSweepTest)
00161         {
00162                 m_useGhostObjectSweepTest = useGhostObjectSweepTest;
00163         }
00164 
00165         bool onGround () const;
00166         void setUpInterpolate (bool value);
00167 };
00168 
00169 #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H