[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 | |
---|
[8284] | 16 | |
---|
[2430] | 17 | #ifndef KINEMATIC_CHARACTER_CONTROLLER_H |
---|
| 18 | #define KINEMATIC_CHARACTER_CONTROLLER_H |
---|
| 19 | |
---|
| 20 | #include "LinearMath/btVector3.h" |
---|
| 21 | |
---|
| 22 | #include "btCharacterControllerInterface.h" |
---|
| 23 | |
---|
[8284] | 24 | #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h" |
---|
| 25 | |
---|
| 26 | |
---|
[2430] | 27 | class btCollisionShape; |
---|
| 28 | class btRigidBody; |
---|
| 29 | class btCollisionWorld; |
---|
| 30 | class btCollisionDispatcher; |
---|
| 31 | class btPairCachingGhostObject; |
---|
| 32 | |
---|
| 33 | ///btKinematicCharacterController is an object that supports a sliding motion in a world. |
---|
| 34 | ///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. |
---|
| 35 | ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user. |
---|
| 36 | class btKinematicCharacterController : public btCharacterControllerInterface |
---|
| 37 | { |
---|
| 38 | protected: |
---|
[8284] | 39 | |
---|
[2430] | 40 | btScalar m_halfHeight; |
---|
| 41 | |
---|
| 42 | btPairCachingGhostObject* m_ghostObject; |
---|
| 43 | btConvexShape* m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast |
---|
| 44 | |
---|
[8284] | 45 | btScalar m_verticalVelocity; |
---|
| 46 | btScalar m_verticalOffset; |
---|
[2430] | 47 | btScalar m_fallSpeed; |
---|
| 48 | btScalar m_jumpSpeed; |
---|
| 49 | btScalar m_maxJumpHeight; |
---|
[8284] | 50 | btScalar m_maxSlopeRadians; // Slope angle that is set (used for returning the exact value) |
---|
| 51 | btScalar m_maxSlopeCosine; // Cosine equivalent of m_maxSlopeRadians (calculated once when set, for optimization) |
---|
| 52 | btScalar m_gravity; |
---|
[2430] | 53 | |
---|
| 54 | btScalar m_turnAngle; |
---|
| 55 | |
---|
| 56 | btScalar m_stepHeight; |
---|
| 57 | |
---|
| 58 | btScalar m_addedMargin;//@todo: remove this and fix the code |
---|
| 59 | |
---|
| 60 | ///this is the desired walk direction, set by the user |
---|
| 61 | btVector3 m_walkDirection; |
---|
[8284] | 62 | btVector3 m_normalizedDirection; |
---|
[2430] | 63 | |
---|
| 64 | //some internal variables |
---|
| 65 | btVector3 m_currentPosition; |
---|
| 66 | btScalar m_currentStepOffset; |
---|
| 67 | btVector3 m_targetPosition; |
---|
| 68 | |
---|
| 69 | ///keep track of the contact manifolds |
---|
| 70 | btManifoldArray m_manifoldArray; |
---|
| 71 | |
---|
| 72 | bool m_touchingContact; |
---|
| 73 | btVector3 m_touchingNormal; |
---|
| 74 | |
---|
[8284] | 75 | bool m_wasOnGround; |
---|
| 76 | bool m_wasJumping; |
---|
[2430] | 77 | bool m_useGhostObjectSweepTest; |
---|
[8284] | 78 | bool m_useWalkDirection; |
---|
| 79 | btScalar m_velocityTimeInterval; |
---|
| 80 | int m_upAxis; |
---|
[2882] | 81 | |
---|
[8284] | 82 | static btVector3* getUpAxisDirections(); |
---|
| 83 | |
---|
[2430] | 84 | btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal); |
---|
| 85 | btVector3 parallelComponent (const btVector3& direction, const btVector3& normal); |
---|
| 86 | btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal); |
---|
| 87 | |
---|
[2882] | 88 | bool recoverFromPenetration ( btCollisionWorld* collisionWorld); |
---|
[2430] | 89 | void stepUp (btCollisionWorld* collisionWorld); |
---|
| 90 | void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0)); |
---|
| 91 | void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove); |
---|
| 92 | void stepDown (btCollisionWorld* collisionWorld, btScalar dt); |
---|
| 93 | public: |
---|
[2882] | 94 | btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1); |
---|
[2430] | 95 | ~btKinematicCharacterController (); |
---|
| 96 | |
---|
[2882] | 97 | |
---|
| 98 | ///btActionInterface interface |
---|
| 99 | virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime) |
---|
| 100 | { |
---|
| 101 | preStep ( collisionWorld); |
---|
| 102 | playerStep (collisionWorld, deltaTime); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | ///btActionInterface interface |
---|
| 106 | void debugDraw(btIDebugDraw* debugDrawer); |
---|
| 107 | |
---|
| 108 | void setUpAxis (int axis) |
---|
| 109 | { |
---|
| 110 | if (axis < 0) |
---|
| 111 | axis = 0; |
---|
| 112 | if (axis > 2) |
---|
| 113 | axis = 2; |
---|
| 114 | m_upAxis = axis; |
---|
| 115 | } |
---|
| 116 | |
---|
[8284] | 117 | /// This should probably be called setPositionIncrementPerSimulatorStep. |
---|
| 118 | /// This is neither a direction nor a velocity, but the amount to |
---|
| 119 | /// increment the position each simulation iteration, regardless |
---|
| 120 | /// of dt. |
---|
| 121 | /// This call will reset any velocity set by setVelocityForTimeInterval(). |
---|
| 122 | virtual void setWalkDirection(const btVector3& walkDirection); |
---|
[2430] | 123 | |
---|
[8284] | 124 | /// Caller provides a velocity with which the character should move for |
---|
| 125 | /// the given time period. After the time period, velocity is reset |
---|
| 126 | /// to zero. |
---|
| 127 | /// This call will reset any walk direction set by setWalkDirection(). |
---|
| 128 | /// Negative time intervals will result in no motion. |
---|
| 129 | virtual void setVelocityForTimeInterval(const btVector3& velocity, |
---|
| 130 | btScalar timeInterval); |
---|
| 131 | |
---|
[2430] | 132 | void reset (); |
---|
| 133 | void warp (const btVector3& origin); |
---|
| 134 | |
---|
[2882] | 135 | void preStep ( btCollisionWorld* collisionWorld); |
---|
| 136 | void playerStep ( btCollisionWorld* collisionWorld, btScalar dt); |
---|
[2430] | 137 | |
---|
| 138 | void setFallSpeed (btScalar fallSpeed); |
---|
| 139 | void setJumpSpeed (btScalar jumpSpeed); |
---|
| 140 | void setMaxJumpHeight (btScalar maxJumpHeight); |
---|
| 141 | bool canJump () const; |
---|
[8284] | 142 | |
---|
[2430] | 143 | void jump (); |
---|
| 144 | |
---|
[8284] | 145 | void setGravity(btScalar gravity); |
---|
| 146 | btScalar getGravity() const; |
---|
| 147 | |
---|
| 148 | /// The max slope determines the maximum angle that the controller can walk up. |
---|
| 149 | /// The slope angle is measured in radians. |
---|
| 150 | void setMaxSlope(btScalar slopeRadians); |
---|
| 151 | btScalar getMaxSlope() const; |
---|
| 152 | |
---|
[2430] | 153 | btPairCachingGhostObject* getGhostObject(); |
---|
| 154 | void setUseGhostSweepTest(bool useGhostObjectSweepTest) |
---|
| 155 | { |
---|
| 156 | m_useGhostObjectSweepTest = useGhostObjectSweepTest; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | bool onGround () const; |
---|
| 160 | }; |
---|
| 161 | |
---|
| 162 | #endif // KINEMATIC_CHARACTER_CONTROLLER_H |
---|