OpenMW
libs/openengine/bullet/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 #ifndef KINEMATIC_CHARACTER_CONTROLLER_H
00017 #define KINEMATIC_CHARACTER_CONTROLLER_H
00018 
00019 #include "LinearMath/btVector3.h"
00020 #include "LinearMath/btQuickprof.h"
00021 
00022 #include "BulletDynamics/Character/btCharacterControllerInterface.h"
00023 
00024 #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
00025 
00026 
00027 class btCollisionShape;
00028 class btRigidBody;
00029 class btCollisionWorld;
00030 class btCollisionDispatcher;
00031 class btPairCachingGhostObject;
00032 
00036 class btKinematicCharacterController : public btCharacterControllerInterface
00037 {
00038 public:
00039   enum UpAxis
00040   {
00041     X_AXIS = 0,
00042     Y_AXIS = 1,
00043     Z_AXIS = 2
00044   };
00045 
00046 private:
00047     btPairCachingGhostObject* externalGhostObject;  // use this for querying collisions for sliding and move
00048     btPairCachingGhostObject* internalGhostObject;  // and this for recoreving from penetrations
00049 
00050     btScalar m_verticalVelocity;
00051     btScalar m_verticalOffset;
00052     btScalar m_fallSpeed;
00053     btScalar m_jumpSpeed;
00054     btScalar m_maxJumpHeight;
00055     btScalar m_maxSlopeRadians; // Slope angle that is set (used for returning the exact value)
00056     btScalar m_maxSlopeCosine;  // Cosine equivalent of m_maxSlopeRadians (calculated once when set, for optimization)
00057     btScalar m_gravity;
00058     btScalar m_recoveringFactor;
00059 
00060     btScalar m_stepHeight;
00061 
00063     btVector3   m_walkDirection;
00064 
00066     btManifoldArray m_manifoldArray;
00067 
00069     bool  m_wasJumping;
00070 
00071     bool    m_useGhostObjectSweepTest;
00072     bool    m_useWalkDirection;
00073     btScalar    m_velocityTimeInterval;
00074 
00075     UpAxis m_upAxis;
00076 
00077     static btVector3* getUpAxisDirections();
00078 
00079     bool recoverFromPenetration ( btCollisionWorld* collisionWorld );
00080 
00081     btVector3 stepUp( btCollisionWorld* collisionWorld, const btVector3& currentPosition, btScalar& currentStepOffset );
00082   btVector3 stepForwardAndStrafe( btCollisionWorld* collisionWorld, const btVector3& currentPosition, const btVector3& walkMove );
00083   btScalar addFallOffset( bool wasJumping, btScalar currentStepOffset, btScalar dt );
00084   btVector3 stepDown( btCollisionWorld* collisionWorld, const btVector3& currentPosition, btScalar currentStepOffset );
00085 
00086 public:
00093     btKinematicCharacterController( btPairCachingGhostObject* externalGhostObject,
00094                                   btPairCachingGhostObject* internalGhostObject,
00095                                   btScalar stepHeight,
00096                                   btScalar constantScale = btScalar( 1.0 ),
00097                                   btScalar gravity = btScalar( 9.8 ),
00098                                   btScalar fallVelocity = btScalar( 55.0 ),
00099                                   btScalar jumpVelocity = btScalar( 9.8 ),
00100                                   btScalar recoveringFactor = btScalar( 0.2 ) );
00101 
00102     ~btKinematicCharacterController ();
00103 
00104     void setVerticalVelocity(float z);
00105 
00107     virtual void updateAction( btCollisionWorld* collisionWorld, btScalar deltaTime )
00108     {
00109     preStep( collisionWorld );
00110         playerStep( collisionWorld, deltaTime );
00111     }
00112 
00114     void debugDraw( btIDebugDraw* debugDrawer );
00115 
00116   void setUpAxis( UpAxis axis )
00117     {
00118         m_upAxis = axis;
00119     }
00120 
00126     virtual void    setWalkDirection(const btVector3& walkDirection);
00127 
00133     virtual void setVelocityForTimeInterval(const btVector3& velocity,
00134                 btScalar timeInterval);
00135 
00136     void reset();
00137     void warp( const btVector3& origin );
00138 
00139     void preStep( btCollisionWorld* collisionWorld );
00140     void playerStep( btCollisionWorld* collisionWorld, btScalar dt );
00141 
00142     void setFallSpeed( btScalar fallSpeed );
00143     void setJumpSpeed( btScalar jumpSpeed );
00144     void setMaxJumpHeight( btScalar maxJumpHeight );
00145     bool canJump() const;
00146 
00147     void jump();
00148 
00149     void setGravity( btScalar gravity );
00150     btScalar getGravity() const;
00151 
00154     void setMaxSlope( btScalar slopeRadians );
00155     btScalar getMaxSlope() const;
00156 
00157     void setUseGhostSweepTest( bool useGhostObjectSweepTest )
00158     {
00159         m_useGhostObjectSweepTest = useGhostObjectSweepTest;
00160     }
00161 
00162     bool onGround() const;
00163 
00164     //if set to false, there will be no collision.
00165     bool mCollision;
00166 };
00167 
00168 #endif // KINEMATIC_CHARACTER_CONTROLLER_H