| [2430] | 1 | /* | 
|---|
|  | 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
|  | 3 | Copyright (c) 2003-2008 Erwin Coumans  http://bulletphysics.com | 
|---|
|  | 4 |  | 
|---|
|  | 5 | This software is provided 'as-is', without any express or implied warranty. | 
|---|
|  | 6 | In no event will the authors be held liable for any damages arising from the use of this software. | 
|---|
|  | 7 | Permission is granted to anyone to use this software for any purpose, | 
|---|
|  | 8 | including commercial applications, and to alter it and redistribute it freely, | 
|---|
|  | 9 | subject to the following restrictions: | 
|---|
|  | 10 |  | 
|---|
|  | 11 | 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. | 
|---|
|  | 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | 
|---|
|  | 13 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
|  | 14 | */ | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #ifndef KINEMATIC_CHARACTER_CONTROLLER_H | 
|---|
|  | 17 | #define KINEMATIC_CHARACTER_CONTROLLER_H | 
|---|
|  | 18 |  | 
|---|
|  | 19 | #include "LinearMath/btVector3.h" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | #include "btCharacterControllerInterface.h" | 
|---|
|  | 22 |  | 
|---|
|  | 23 | class btCollisionShape; | 
|---|
|  | 24 | class btRigidBody; | 
|---|
|  | 25 | class btCollisionWorld; | 
|---|
|  | 26 | class btCollisionDispatcher; | 
|---|
|  | 27 | class btPairCachingGhostObject; | 
|---|
|  | 28 |  | 
|---|
|  | 29 | ///btKinematicCharacterController is an object that supports a sliding motion in a world. | 
|---|
|  | 30 | ///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. | 
|---|
|  | 31 | ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user. | 
|---|
|  | 32 | class btKinematicCharacterController : public btCharacterControllerInterface | 
|---|
|  | 33 | { | 
|---|
|  | 34 | protected: | 
|---|
|  | 35 | btScalar m_halfHeight; | 
|---|
|  | 36 |  | 
|---|
|  | 37 | btPairCachingGhostObject* m_ghostObject; | 
|---|
|  | 38 | btConvexShape*  m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast | 
|---|
|  | 39 |  | 
|---|
|  | 40 | btScalar m_fallSpeed; | 
|---|
|  | 41 | btScalar m_jumpSpeed; | 
|---|
|  | 42 | btScalar m_maxJumpHeight; | 
|---|
|  | 43 |  | 
|---|
|  | 44 | btScalar m_turnAngle; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | btScalar m_stepHeight; | 
|---|
|  | 47 |  | 
|---|
|  | 48 | btScalar        m_addedMargin;//@todo: remove this and fix the code | 
|---|
|  | 49 |  | 
|---|
|  | 50 | ///this is the desired walk direction, set by the user | 
|---|
|  | 51 | btVector3       m_walkDirection; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | //some internal variables | 
|---|
|  | 54 | btVector3 m_currentPosition; | 
|---|
|  | 55 | btScalar  m_currentStepOffset; | 
|---|
|  | 56 | btVector3 m_targetPosition; | 
|---|
|  | 57 |  | 
|---|
|  | 58 | ///keep track of the contact manifolds | 
|---|
|  | 59 | btManifoldArray m_manifoldArray; | 
|---|
|  | 60 |  | 
|---|
|  | 61 | bool m_touchingContact; | 
|---|
|  | 62 | btVector3 m_touchingNormal; | 
|---|
|  | 63 |  | 
|---|
|  | 64 | bool    m_useGhostObjectSweepTest; | 
|---|
| [2882] | 65 |  | 
|---|
|  | 66 | int m_upAxis; | 
|---|
| [2430] | 67 |  | 
|---|
|  | 68 | btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal); | 
|---|
|  | 69 | btVector3 parallelComponent (const btVector3& direction, const btVector3& normal); | 
|---|
|  | 70 | btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal); | 
|---|
|  | 71 |  | 
|---|
| [2882] | 72 | bool recoverFromPenetration ( btCollisionWorld* collisionWorld); | 
|---|
| [2430] | 73 | void stepUp (btCollisionWorld* collisionWorld); | 
|---|
|  | 74 | void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0)); | 
|---|
|  | 75 | void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove); | 
|---|
|  | 76 | void stepDown (btCollisionWorld* collisionWorld, btScalar dt); | 
|---|
|  | 77 | public: | 
|---|
| [2882] | 78 | btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1); | 
|---|
| [2430] | 79 | ~btKinematicCharacterController (); | 
|---|
|  | 80 |  | 
|---|
| [2882] | 81 |  | 
|---|
|  | 82 | ///btActionInterface interface | 
|---|
|  | 83 | virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime) | 
|---|
|  | 84 | { | 
|---|
|  | 85 | preStep ( collisionWorld); | 
|---|
|  | 86 | playerStep (collisionWorld, deltaTime); | 
|---|
|  | 87 | } | 
|---|
|  | 88 |  | 
|---|
|  | 89 | ///btActionInterface interface | 
|---|
|  | 90 | void    debugDraw(btIDebugDraw* debugDrawer); | 
|---|
|  | 91 |  | 
|---|
|  | 92 | void setUpAxis (int axis) | 
|---|
|  | 93 | { | 
|---|
|  | 94 | if (axis < 0) | 
|---|
|  | 95 | axis = 0; | 
|---|
|  | 96 | if (axis > 2) | 
|---|
|  | 97 | axis = 2; | 
|---|
|  | 98 | m_upAxis = axis; | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
| [2430] | 101 | virtual void    setWalkDirection(const btVector3& walkDirection) | 
|---|
|  | 102 | { | 
|---|
|  | 103 | m_walkDirection = walkDirection; | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
|  | 106 | void reset (); | 
|---|
|  | 107 | void warp (const btVector3& origin); | 
|---|
|  | 108 |  | 
|---|
| [2882] | 109 | void preStep (  btCollisionWorld* collisionWorld); | 
|---|
|  | 110 | void playerStep ( btCollisionWorld* collisionWorld, btScalar dt); | 
|---|
| [2430] | 111 |  | 
|---|
|  | 112 | void setFallSpeed (btScalar fallSpeed); | 
|---|
|  | 113 | void setJumpSpeed (btScalar jumpSpeed); | 
|---|
|  | 114 | void setMaxJumpHeight (btScalar maxJumpHeight); | 
|---|
|  | 115 | bool canJump () const; | 
|---|
|  | 116 | void jump (); | 
|---|
|  | 117 |  | 
|---|
|  | 118 | btPairCachingGhostObject* getGhostObject(); | 
|---|
|  | 119 | void    setUseGhostSweepTest(bool useGhostObjectSweepTest) | 
|---|
|  | 120 | { | 
|---|
|  | 121 | m_useGhostObjectSweepTest = useGhostObjectSweepTest; | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
|  | 124 | bool onGround () const; | 
|---|
|  | 125 | }; | 
|---|
|  | 126 |  | 
|---|
|  | 127 | #endif // KINEMATIC_CHARACTER_CONTROLLER_H | 
|---|