| 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; |
|---|
| 65 | |
|---|
| 66 | btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal); |
|---|
| 67 | btVector3 parallelComponent (const btVector3& direction, const btVector3& normal); |
|---|
| 68 | btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal); |
|---|
| 69 | |
|---|
| 70 | bool recoverFromPenetration (btCollisionWorld* collisionWorld); |
|---|
| 71 | void stepUp (btCollisionWorld* collisionWorld); |
|---|
| 72 | void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0)); |
|---|
| 73 | void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove); |
|---|
| 74 | void stepDown (btCollisionWorld* collisionWorld, btScalar dt); |
|---|
| 75 | public: |
|---|
| 76 | btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight); |
|---|
| 77 | ~btKinematicCharacterController (); |
|---|
| 78 | |
|---|
| 79 | virtual void setWalkDirection(const btVector3& walkDirection) |
|---|
| 80 | { |
|---|
| 81 | m_walkDirection = walkDirection; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void reset (); |
|---|
| 85 | void warp (const btVector3& origin); |
|---|
| 86 | |
|---|
| 87 | void preStep ( btCollisionWorld* collisionWorld); |
|---|
| 88 | void playerStep (btCollisionWorld* collisionWorld, btScalar dt); |
|---|
| 89 | |
|---|
| 90 | void setFallSpeed (btScalar fallSpeed); |
|---|
| 91 | void setJumpSpeed (btScalar jumpSpeed); |
|---|
| 92 | void setMaxJumpHeight (btScalar maxJumpHeight); |
|---|
| 93 | bool canJump () const; |
|---|
| 94 | void jump (); |
|---|
| 95 | |
|---|
| 96 | btPairCachingGhostObject* getGhostObject(); |
|---|
| 97 | void setUseGhostSweepTest(bool useGhostObjectSweepTest) |
|---|
| 98 | { |
|---|
| 99 | m_useGhostObjectSweepTest = useGhostObjectSweepTest; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | bool onGround () const; |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | #endif // KINEMATIC_CHARACTER_CONTROLLER_H |
|---|