Sirikata
libspace/plugins/physics/BulletCharacterController.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved.
00002 // Use of this source code is governed by a BSD-style license that can
00003 // be found in the LICENSE file.
00004 // This is originally from Bullet's src/BulletDynamics/Character/btCharacterController.h
00005 /*
00006   Bullet Continuous Collision Detection and Physics Library
00007   Copyright (c) 2003-2008 Erwin Coumans  http://bulletphysics.com
00008 
00009   This software is provided 'as-is', without any express or implied warranty.
00010   In no event will the authors be held liable for any damages arising from the use of this software.
00011   Permission is granted to anyone to use this software for any purpose,
00012   including commercial applications, and to alter it and redistribute it freely,
00013   subject to the following restrictions:
00014 
00015   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.
00016   2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00017   3. This notice may not be removed or altered from any source distribution.
00018 */
00019 
00020 
00021 #ifndef _SIRIKATA_BULLET_PHYSICS_CHARACTER_CONTROLLER_HPP_
00022 #define _SIRIKATA_BULLET_PHYSICS_CHARACTER_CONTROLLER_HPP_
00023 
00024 #include "LinearMath/btVector3.h"
00025 #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
00026 #include "BulletDynamics/Dynamics/btActionInterface.h"
00027 
00028 class btCollisionShape;
00029 class btRigidBody;
00030 class btCollisionWorld;
00031 class btCollisionDispatcher;
00032 class btPairCachingGhostObject;
00033 class btConvexShape;
00034 class btIDebugDraw;
00035 
00036 namespace Sirikata {
00037 
00041 class BulletCharacterController : public btActionInterface
00042 {
00043 protected:
00044 
00045     btScalar m_halfHeight;
00046 
00047     btPairCachingGhostObject* m_ghostObject;
00048     btConvexShape* m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast
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 
00059     btScalar m_turnAngle;
00060 
00061     btScalar m_stepHeight;
00062 
00063     btScalar m_addedMargin;//@todo: remove this and fix the code
00064 
00066     btVector3 m_walkDirection;
00067     btVector3 m_normalizedDirection;
00068 
00069     //some internal variables
00070     btVector3 m_currentPosition;
00071     btScalar  m_currentStepOffset;
00072     btVector3 m_targetPosition;
00073 
00075     btManifoldArray m_manifoldArray;
00076 
00077     bool m_touchingContact;
00078     btVector3 m_touchingNormal;
00079 
00080     bool  m_wasOnGround;
00081     bool  m_wasJumping;
00082     bool m_useGhostObjectSweepTest;
00083     bool m_useWalkDirection;
00084     btScalar m_velocityTimeInterval;
00085     int m_upAxis;
00086 
00087     static btVector3* getUpAxisDirections();
00088 
00089     btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal);
00090     btVector3 parallelComponent (const btVector3& direction, const btVector3& normal);
00091     btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal);
00092 
00093     bool recoverFromPenetration ( btCollisionWorld* collisionWorld);
00094     void stepUp (btCollisionWorld* collisionWorld);
00095     void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
00096     void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove);
00097     void stepDown (btCollisionWorld* collisionWorld, btScalar dt);
00098 public:
00099     BulletCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1);
00100     ~BulletCharacterController ();
00101 
00102 
00104     virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
00105     {
00106         preStep ( collisionWorld);
00107         playerStep (collisionWorld, deltaTime);
00108     }
00109 
00111     void debugDraw(btIDebugDraw* debugDrawer);
00112 
00113     void setUpAxis (int axis)
00114     {
00115         if (axis < 0)
00116             axis = 0;
00117         if (axis > 2)
00118             axis = 2;
00119         m_upAxis = axis;
00120     }
00121 
00127     virtual void setWalkDirection(const btVector3& walkDirection);
00128 
00134     virtual void setVelocityForTimeInterval(const btVector3& velocity,
00135         btScalar timeInterval);
00136 
00137     void reset ();
00138     void warp (const btVector3& origin);
00139 
00140     void preStep (  btCollisionWorld* collisionWorld);
00141     void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
00142 
00143     void setFallSpeed (btScalar fallSpeed);
00144     void setJumpSpeed (btScalar jumpSpeed);
00145     void setMaxJumpHeight (btScalar maxJumpHeight);
00146     bool canJump () const;
00147 
00148     void jump ();
00149 
00150     void setGravity(btScalar gravity);
00151     btScalar getGravity() const;
00152 
00155     void setMaxSlope(btScalar slopeRadians);
00156     btScalar getMaxSlope() const;
00157 
00158     btPairCachingGhostObject* getGhostObject();
00159     void setUseGhostSweepTest(bool useGhostObjectSweepTest)
00160     {
00161         m_useGhostObjectSweepTest = useGhostObjectSweepTest;
00162     }
00163 
00164     bool onGround () const;
00165 };
00166 
00167 } // namespace Sirikata
00168 
00169 #endif // _SIRIKATA_BULLET_PHYSICS_CHARACTER_CONTROLLER_HPP_