2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
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:
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.
16 #ifndef BT_KINEMATIC_CHARACTER_CONTROLLER_H
17 #define BT_KINEMATIC_CHARACTER_CONTROLLER_H
19 #include "LinearMath/btVector3.h"
21 #include "btCharacterControllerInterface.h"
23 #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
25 class btCollisionShape;
28 class btCollisionWorld;
29 class btCollisionDispatcher;
30 class btPairCachingGhostObject;
32 ///btKinematicCharacterController is an object that supports a sliding motion in a world.
33 ///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.
34 ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user.
35 ATTRIBUTE_ALIGNED16(class)
36 btKinematicCharacterController : public btCharacterControllerInterface
39 btScalar m_halfHeight;
41 btPairCachingGhostObject* m_ghostObject;
42 btConvexShape* m_convexShape; //is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast
44 btScalar m_maxPenetrationDepth;
45 btScalar m_verticalVelocity;
46 btScalar m_verticalOffset;
49 btScalar m_SetjumpSpeed;
50 btScalar m_maxJumpHeight;
51 btScalar m_maxSlopeRadians; // Slope angle that is set (used for returning the exact value)
52 btScalar m_maxSlopeCosine; // Cosine equivalent of m_maxSlopeRadians (calculated once when set, for optimization)
57 btScalar m_stepHeight;
59 btScalar m_addedMargin; //@todo: remove this and fix the code
61 ///this is the desired walk direction, set by the user
62 btVector3 m_walkDirection;
63 btVector3 m_normalizedDirection;
66 btVector3 m_jumpPosition;
68 //some internal variables
69 btVector3 m_currentPosition;
70 btScalar m_currentStepOffset;
71 btVector3 m_targetPosition;
73 btQuaternion m_currentOrientation;
74 btQuaternion m_targetOrientation;
76 ///keep track of the contact manifolds
77 btManifoldArray m_manifoldArray;
79 bool m_touchingContact;
80 btVector3 m_touchingNormal;
82 btScalar m_linearDamping;
83 btScalar m_angularDamping;
87 bool m_useGhostObjectSweepTest;
88 bool m_useWalkDirection;
89 btScalar m_velocityTimeInterval;
93 static btVector3* getUpAxisDirections();
98 btVector3 computeReflectionDirection(const btVector3& direction, const btVector3& normal);
99 btVector3 parallelComponent(const btVector3& direction, const btVector3& normal);
100 btVector3 perpindicularComponent(const btVector3& direction, const btVector3& normal);
102 bool recoverFromPenetration(btCollisionWorld * collisionWorld);
103 void stepUp(btCollisionWorld * collisionWorld);
104 void updateTargetPositionBasedOnCollision(const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
105 void stepForwardAndStrafe(btCollisionWorld * collisionWorld, const btVector3& walkMove);
106 void stepDown(btCollisionWorld * collisionWorld, btScalar dt);
108 virtual bool needsCollision(const btCollisionObject* body0, const btCollisionObject* body1);
110 void setUpVector(const btVector3& up);
112 btQuaternion getRotation(btVector3 & v0, btVector3 & v1) const;
115 BT_DECLARE_ALIGNED_ALLOCATOR();
117 btKinematicCharacterController(btPairCachingGhostObject * ghostObject, btConvexShape * convexShape, btScalar stepHeight, const btVector3& up = btVector3(1.0, 0.0, 0.0));
118 ~btKinematicCharacterController();
120 ///btActionInterface interface
121 virtual void updateAction(btCollisionWorld * collisionWorld, btScalar deltaTime)
123 preStep(collisionWorld);
124 playerStep(collisionWorld, deltaTime);
127 ///btActionInterface interface
128 void debugDraw(btIDebugDraw * debugDrawer);
130 void setUp(const btVector3& up);
132 const btVector3& getUp() { return m_up; }
134 /// This should probably be called setPositionIncrementPerSimulatorStep.
135 /// This is neither a direction nor a velocity, but the amount to
136 /// increment the position each simulation iteration, regardless
138 /// This call will reset any velocity set by setVelocityForTimeInterval().
139 virtual void setWalkDirection(const btVector3& walkDirection);
141 /// Caller provides a velocity with which the character should move for
142 /// the given time period. After the time period, velocity is reset
144 /// This call will reset any walk direction set by setWalkDirection().
145 /// Negative time intervals will result in no motion.
146 virtual void setVelocityForTimeInterval(const btVector3& velocity,
147 btScalar timeInterval);
149 virtual void setAngularVelocity(const btVector3& velocity);
150 virtual const btVector3& getAngularVelocity() const;
152 virtual void setLinearVelocity(const btVector3& velocity);
153 virtual btVector3 getLinearVelocity() const;
155 void setLinearDamping(btScalar d) { m_linearDamping = btClamped(d, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0)); }
156 btScalar getLinearDamping() const { return m_linearDamping; }
157 void setAngularDamping(btScalar d) { m_angularDamping = btClamped(d, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0)); }
158 btScalar getAngularDamping() const { return m_angularDamping; }
160 void reset(btCollisionWorld * collisionWorld);
161 void warp(const btVector3& origin);
163 void preStep(btCollisionWorld * collisionWorld);
164 void playerStep(btCollisionWorld * collisionWorld, btScalar dt);
166 void setStepHeight(btScalar h);
167 btScalar getStepHeight() const { return m_stepHeight; }
168 void setFallSpeed(btScalar fallSpeed);
169 btScalar getFallSpeed() const { return m_fallSpeed; }
170 void setJumpSpeed(btScalar jumpSpeed);
171 btScalar getJumpSpeed() const { return m_jumpSpeed; }
172 void setMaxJumpHeight(btScalar maxJumpHeight);
173 bool canJump() const;
175 void jump(const btVector3& v = btVector3(0, 0, 0));
177 void applyImpulse(const btVector3& v) { jump(v); }
179 void setGravity(const btVector3& gravity);
180 btVector3 getGravity() const;
182 /// The max slope determines the maximum angle that the controller can walk up.
183 /// The slope angle is measured in radians.
184 void setMaxSlope(btScalar slopeRadians);
185 btScalar getMaxSlope() const;
187 void setMaxPenetrationDepth(btScalar d);
188 btScalar getMaxPenetrationDepth() const;
190 btPairCachingGhostObject* getGhostObject();
191 void setUseGhostSweepTest(bool useGhostObjectSweepTest)
193 m_useGhostObjectSweepTest = useGhostObjectSweepTest;
196 bool onGround() const;
197 void setUpInterpolate(bool value);
200 #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H