Bullet 2.85 update
[Torque-3d.git] / Engine / lib / bullet / src / BulletDynamics / Character / btKinematicCharacterController.h
blob3d677e647e2003bcb9f9ed4e7ffdea6df182f922
1 /*
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.
17 #ifndef BT_KINEMATIC_CHARACTER_CONTROLLER_H
18 #define BT_KINEMATIC_CHARACTER_CONTROLLER_H
20 #include "LinearMath/btVector3.h"
22 #include "btCharacterControllerInterface.h"
24 #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
27 class btCollisionShape;
28 class btConvexShape;
29 class btRigidBody;
30 class btCollisionWorld;
31 class btCollisionDispatcher;
32 class btPairCachingGhostObject;
34 ///btKinematicCharacterController is an object that supports a sliding motion in a world.
35 ///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.
36 ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user.
37 ATTRIBUTE_ALIGNED16(class) btKinematicCharacterController : public btCharacterControllerInterface
39 protected:
41 btScalar m_halfHeight;
43 btPairCachingGhostObject* m_ghostObject;
44 btConvexShape* m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast
46 btScalar m_maxPenetrationDepth;
47 btScalar m_verticalVelocity;
48 btScalar m_verticalOffset;
49 btScalar m_fallSpeed;
50 btScalar m_jumpSpeed;
51 btScalar m_SetjumpSpeed;
52 btScalar m_maxJumpHeight;
53 btScalar m_maxSlopeRadians; // Slope angle that is set (used for returning the exact value)
54 btScalar m_maxSlopeCosine; // Cosine equivalent of m_maxSlopeRadians (calculated once when set, for optimization)
55 btScalar m_gravity;
57 btScalar m_turnAngle;
59 btScalar m_stepHeight;
61 btScalar m_addedMargin;//@todo: remove this and fix the code
63 ///this is the desired walk direction, set by the user
64 btVector3 m_walkDirection;
65 btVector3 m_normalizedDirection;
66 btVector3 m_AngVel;
68 btVector3 m_jumpPosition;
70 //some internal variables
71 btVector3 m_currentPosition;
72 btScalar m_currentStepOffset;
73 btVector3 m_targetPosition;
75 btQuaternion m_currentOrientation;
76 btQuaternion m_targetOrientation;
78 ///keep track of the contact manifolds
79 btManifoldArray m_manifoldArray;
81 bool m_touchingContact;
82 btVector3 m_touchingNormal;
84 btScalar m_linearDamping;
85 btScalar m_angularDamping;
87 bool m_wasOnGround;
88 bool m_wasJumping;
89 bool m_useGhostObjectSweepTest;
90 bool m_useWalkDirection;
91 btScalar m_velocityTimeInterval;
92 btVector3 m_up;
93 btVector3 m_jumpAxis;
95 static btVector3* getUpAxisDirections();
96 bool m_interpolateUp;
97 bool full_drop;
98 bool bounce_fix;
100 btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal);
101 btVector3 parallelComponent (const btVector3& direction, const btVector3& normal);
102 btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal);
104 bool recoverFromPenetration ( btCollisionWorld* collisionWorld);
105 void stepUp (btCollisionWorld* collisionWorld);
106 void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
107 void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove);
108 void stepDown (btCollisionWorld* collisionWorld, btScalar dt);
110 virtual bool needsCollision(const btCollisionObject* body0, const btCollisionObject* body1);
112 void setUpVector(const btVector3& up);
114 btQuaternion getRotation(btVector3& v0, btVector3& v1) const;
116 public:
118 BT_DECLARE_ALIGNED_ALLOCATOR();
120 btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, const btVector3& up = btVector3(1.0,0.0,0.0));
121 ~btKinematicCharacterController ();
124 ///btActionInterface interface
125 virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
127 preStep ( collisionWorld);
128 playerStep (collisionWorld, deltaTime);
131 ///btActionInterface interface
132 void debugDraw(btIDebugDraw* debugDrawer);
134 void setUp(const btVector3& up);
136 const btVector3& getUp() { return m_up; }
138 /// This should probably be called setPositionIncrementPerSimulatorStep.
139 /// This is neither a direction nor a velocity, but the amount to
140 /// increment the position each simulation iteration, regardless
141 /// of dt.
142 /// This call will reset any velocity set by setVelocityForTimeInterval().
143 virtual void setWalkDirection(const btVector3& walkDirection);
145 /// Caller provides a velocity with which the character should move for
146 /// the given time period. After the time period, velocity is reset
147 /// to zero.
148 /// This call will reset any walk direction set by setWalkDirection().
149 /// Negative time intervals will result in no motion.
150 virtual void setVelocityForTimeInterval(const btVector3& velocity,
151 btScalar timeInterval);
153 virtual void setAngularVelocity(const btVector3& velocity);
154 virtual const btVector3& getAngularVelocity() const;
156 virtual void setLinearVelocity(const btVector3& velocity);
157 virtual btVector3 getLinearVelocity() const;
159 void setLinearDamping(btScalar d) { m_linearDamping = btClamped(d, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0)); }
160 btScalar getLinearDamping() const { return m_linearDamping; }
161 void setAngularDamping(btScalar d) { m_angularDamping = btClamped(d, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0)); }
162 btScalar getAngularDamping() const { return m_angularDamping; }
164 void reset ( btCollisionWorld* collisionWorld );
165 void warp (const btVector3& origin);
167 void preStep ( btCollisionWorld* collisionWorld);
168 void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
170 void setStepHeight(btScalar h);
171 btScalar getStepHeight() const { return m_stepHeight; }
172 void setFallSpeed (btScalar fallSpeed);
173 btScalar getFallSpeed() const { return m_fallSpeed; }
174 void setJumpSpeed (btScalar jumpSpeed);
175 btScalar getJumpSpeed() const { return m_jumpSpeed; }
176 void setMaxJumpHeight (btScalar maxJumpHeight);
177 bool canJump () const;
179 void jump(const btVector3& v = btVector3());
181 void applyImpulse(const btVector3& v) { jump(v); }
183 void setGravity(const btVector3& gravity);
184 btVector3 getGravity() const;
186 /// The max slope determines the maximum angle that the controller can walk up.
187 /// The slope angle is measured in radians.
188 void setMaxSlope(btScalar slopeRadians);
189 btScalar getMaxSlope() const;
191 void setMaxPenetrationDepth(btScalar d);
192 btScalar getMaxPenetrationDepth() const;
194 btPairCachingGhostObject* getGhostObject();
195 void setUseGhostSweepTest(bool useGhostObjectSweepTest)
197 m_useGhostObjectSweepTest = useGhostObjectSweepTest;
200 bool onGround () const;
201 void setUpInterpolate (bool value);
204 #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H