Bullet Collision Detection & Physics Library
btKinematicCharacterController.h
Go to the documentation of this file.
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 
17 #ifndef BT_KINEMATIC_CHARACTER_CONTROLLER_H
18 #define BT_KINEMATIC_CHARACTER_CONTROLLER_H
19 
20 #include "LinearMath/btVector3.h"
21 
23 
25 
26 
27 class btCollisionShape;
28 class btConvexShape;
29 class btRigidBody;
30 class btCollisionWorld;
33 
38 {
39 protected:
40 
42 
44  btConvexShape* m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast
45 
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)
54 
56 
58 
59  btScalar m_addedMargin;//@todo: remove this and fix the code
60 
64 
65  //some internal variables
69 
72 
75 
81  int m_upAxis;
82 
83  static btVector3* getUpAxisDirections();
84 
85  btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal);
86  btVector3 parallelComponent (const btVector3& direction, const btVector3& normal);
87  btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal);
88 
89  bool recoverFromPenetration ( btCollisionWorld* collisionWorld);
90  void stepUp (btCollisionWorld* collisionWorld);
91  void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
92  void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove);
93  void stepDown (btCollisionWorld* collisionWorld, btScalar dt);
94 public:
95 
97 
98  btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1);
100 
101 
103  virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
104  {
105  preStep ( collisionWorld);
106  playerStep (collisionWorld, deltaTime);
107  }
108 
110  void debugDraw(btIDebugDraw* debugDrawer);
111 
112  void setUpAxis (int axis)
113  {
114  if (axis < 0)
115  axis = 0;
116  if (axis > 2)
117  axis = 2;
118  m_upAxis = axis;
119  }
120 
126  virtual void setWalkDirection(const btVector3& walkDirection);
127 
133  virtual void setVelocityForTimeInterval(const btVector3& velocity,
134  btScalar timeInterval);
135 
136  void reset ();
137  void warp (const btVector3& origin);
138 
139  void preStep ( btCollisionWorld* collisionWorld);
140  void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
141 
142  void setFallSpeed (btScalar fallSpeed);
143  void setJumpSpeed (btScalar jumpSpeed);
144  void setMaxJumpHeight (btScalar maxJumpHeight);
145  bool canJump () const;
146 
147  void jump ();
148 
149  void setGravity(btScalar gravity);
150  btScalar getGravity() const;
151 
154  void setMaxSlope(btScalar slopeRadians);
155  btScalar getMaxSlope() const;
156 
157  btPairCachingGhostObject* getGhostObject();
158  void setUseGhostSweepTest(bool useGhostObjectSweepTest)
159  {
160  m_useGhostObjectSweepTest = useGhostObjectSweepTest;
161  }
162 
163  bool onGround () const;
164 };
165 
166 #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H