GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KinematicCharacterController.hpp
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
12  original software. If you use this software in a product, an acknowledgment in the product
13  documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being
15  the original software.
16 3. This notice may not be removed or altered from any source distribution.
17 
18 -----------
19 
20 This file is taken from the original Bullet engine source, and is MODIFIED to add support for what
21 we need.
22 
23 */
24 #ifndef RADIX_KINEMATIC_CHARACTER_CONTROLLER_HPP
25 #define RADIX_KINEMATIC_CHARACTER_CONTROLLER_HPP
26 
27 #include <bullet/LinearMath/btVector3.h>
28 #include <bullet/BulletDynamics/Character/btCharacterControllerInterface.h>
29 #include <bullet/BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h>
30 
31 
32 class btCollisionShape;
33 class btConvexShape;
34 class btRigidBody;
35 class btCollisionWorld;
36 class btCollisionDispatcher;
37 class btPairCachingGhostObject;
38 
39 namespace radix {
40 
46 class KinematicCharacterController : public btCharacterControllerInterface {
47 protected:
48 
49  btScalar m_halfHeight;
50 
51  btPairCachingGhostObject* m_ghostObject;
52  btConvexShape* m_convexShape;// is also in m_ghostObject, but it needs to be convex, so we store
53  // it here to avoid upcast
54 
56  btScalar m_verticalOffset;
57  btScalar m_fallSpeed;
58  btScalar m_jumpSpeed;
59  btScalar m_maxJumpHeight;
60  btScalar m_maxSlopeRadians; // Slope angle that is set(used for returning the exact value)
61  btScalar m_maxSlopeCosine; // Cosine equivalent of m_maxSlopeRadians(calculated once when set,
62  // for optimization)
63  btScalar m_gravity;
64 
65  btScalar m_turnAngle;
66 
67  btScalar m_stepHeight;
68 
69  btScalar m_addedMargin;//@todo: remove this and fix the code
70 
72  btVector3 m_walkDirection;
74 
75  //some internal variables
76  btVector3 m_currentPosition;
78  btVector3 m_targetPosition;
79 
81  btManifoldArray m_manifoldArray;
82 
84  btVector3 m_touchingNormal;
85 
91  int m_upAxis;
92 
93  static btVector3* getUpAxisDirections();
95  bool full_drop;
96  bool bounce_fix;
97 
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);
101 
102  bool recoverFromPenetration(btCollisionWorld* collisionWorld);
103  void stepUp(btCollisionWorld* collisionWorld);
104  void updateTargetPositionBasedOnCollision(const btVector3& hit_normal,
105  btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
106  void stepForwardAndStrafe(btCollisionWorld* collisionWorld, const btVector3& walkMove);
107  void stepDown(btCollisionWorld* collisionWorld, btScalar dt);
108 public:
109 
111 
112  KinematicCharacterController(btPairCachingGhostObject* ghostObject, btConvexShape* convexShape,
113  btScalar stepHeight, int upAxis = 1);
115 
116 
118  virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTime) {
119  preStep(collisionWorld);
120  playerStep(collisionWorld, deltaTime);
121  }
122 
124  void debugDraw(btIDebugDraw* debugDrawer);
125 
126  void setUpAxis(int axis) {
127  if (axis < 0) {
128  axis = 0;
129  } else if (axis > 2) {
130  axis = 2;
131  }
132  m_upAxis = axis;
133  }
134 
140  virtual void setWalkDirection(const btVector3& walkDirection);
141 
147  virtual void setVelocityForTimeInterval(const btVector3& velocity,
148  btScalar timeInterval);
149 
150  void reset(btCollisionWorld* collisionWorld );
151  void warp(const btVector3& origin);
152 
153  void preStep( btCollisionWorld* collisionWorld);
154  void playerStep(btCollisionWorld* collisionWorld, btScalar dt);
155 
156  void setFallSpeed(btScalar fallSpeed);
157  void setJumpSpeed(btScalar jumpSpeed);
158  void setMaxJumpHeight(btScalar maxJumpHeight);
159  bool canJump() const;
160 
161  void jump(const btVector3& dir = btVector3());
162 
163  void setGravity(btScalar gravity);
164  btScalar getGravity() const;
165 
168  void setMaxSlope(btScalar slopeRadians);
169  btScalar getMaxSlope() const;
170 
171  btPairCachingGhostObject* getGhostObject();
172  void setUseGhostSweepTest(bool useGhostObjectSweepTest) {
173  m_useGhostObjectSweepTest = useGhostObjectSweepTest;
174  }
175 
176  bool onGround() const;
177  void setUpInterpolate(bool value);
178 };
179 
180 } /* namespace radix */
181 
182 #endif /* RADIX_KINEMATIC_CHARACTER_CONTROLLER_HPP */
bool m_wasJumping
Definition: KinematicCharacterController.hpp:87
bool m_useGhostObjectSweepTest
Definition: KinematicCharacterController.hpp:88
Definition: GameController.hpp:7
bool m_touchingContact
Definition: KinematicCharacterController.hpp:83
btScalar m_halfHeight
Definition: KinematicCharacterController.hpp:49
KinematicCharacterController(btPairCachingGhostObject *ghostObject, btConvexShape *convexShape, btScalar stepHeight, int upAxis=1)
Definition: KinematicCharacterController.cpp:160
~KinematicCharacterController()
Definition: KinematicCharacterController.cpp:188
bool onGround() const
Definition: KinematicCharacterController.cpp:724
int m_upAxis
Definition: KinematicCharacterController.hpp:91
btScalar m_maxSlopeRadians
Definition: KinematicCharacterController.hpp:60
virtual void setVelocityForTimeInterval(const btVector3 &velocity, btScalar timeInterval)
Caller provides a velocity with which the character should move for the given time period...
Definition: KinematicCharacterController.cpp:591
virtual void setWalkDirection(const btVector3 &walkDirection)
This should probably be called setPositionIncrementPerSimulatorStep.
Definition: KinematicCharacterController.cpp:585
btVector3 m_normalizedDirection
Definition: KinematicCharacterController.hpp:73
btScalar m_addedMargin
Definition: KinematicCharacterController.hpp:69
btScalar getGravity() const
Definition: KinematicCharacterController.cpp:711
bool m_interpolateUp
Definition: KinematicCharacterController.hpp:94
void updateTargetPositionBasedOnCollision(const btVector3 &hit_normal, btScalar tangentMag=btScalar(0.0), btScalar normalMag=btScalar(1.0))
Definition: KinematicCharacterController.cpp:331
static btVector3 * getUpAxisDirections()
Definition: KinematicCharacterController.cpp:729
btScalar m_verticalVelocity
Definition: KinematicCharacterController.hpp:55
btVector3 m_walkDirection
this is the desired walk direction, set by the user
Definition: KinematicCharacterController.hpp:72
btScalar m_jumpSpeed
Definition: KinematicCharacterController.hpp:58
void preStep(btCollisionWorld *collisionWorld)
Definition: KinematicCharacterController.cpp:624
void setUpAxis(int axis)
Definition: KinematicCharacterController.hpp:126
btVector3 perpindicularComponent(const btVector3 &direction, const btVector3 &normal)
Definition: KinematicCharacterController.cpp:154
void warp(const btVector3 &origin)
Definition: KinematicCharacterController.cpp:616
bool m_useWalkDirection
Definition: KinematicCharacterController.hpp:89
btVector3 parallelComponent(const btVector3 &direction, const btVector3 &normal)
Definition: KinematicCharacterController.cpp:144
btScalar m_velocityTimeInterval
Definition: KinematicCharacterController.hpp:90
btScalar m_turnAngle
Definition: KinematicCharacterController.hpp:65
btScalar getMaxSlope() const
Definition: KinematicCharacterController.cpp:720
void playerStep(btCollisionWorld *collisionWorld, btScalar dt)
Definition: KinematicCharacterController.cpp:639
btScalar m_fallSpeed
Definition: KinematicCharacterController.hpp:57
void setFallSpeed(btScalar fallSpeed)
Definition: KinematicCharacterController.cpp:682
btScalar m_maxJumpHeight
Definition: KinematicCharacterController.hpp:59
void debugDraw(btIDebugDraw *debugDrawer)
btActionInterface interface
Definition: KinematicCharacterController.cpp:736
btScalar m_maxSlopeCosine
Definition: KinematicCharacterController.hpp:61
bool full_drop
Definition: KinematicCharacterController.hpp:95
void stepForwardAndStrafe(btCollisionWorld *collisionWorld, const btVector3 &walkMove)
Definition: KinematicCharacterController.cpp:364
btVector3 m_targetPosition
Definition: KinematicCharacterController.hpp:78
void setGravity(btScalar gravity)
Definition: KinematicCharacterController.cpp:707
btVector3 m_touchingNormal
Definition: KinematicCharacterController.hpp:84
btScalar m_stepHeight
Definition: KinematicCharacterController.hpp:67
void setMaxSlope(btScalar slopeRadians)
The max slope determines the maximum angle that the controller can walk up.
Definition: KinematicCharacterController.cpp:715
btScalar m_currentStepOffset
Definition: KinematicCharacterController.hpp:77
bool bounce_fix
Definition: KinematicCharacterController.hpp:96
void setJumpSpeed(btScalar jumpSpeed)
Definition: KinematicCharacterController.cpp:686
void setUpInterpolate(bool value)
Definition: KinematicCharacterController.cpp:739
btPairCachingGhostObject * m_ghostObject
Definition: KinematicCharacterController.hpp:51
void setUseGhostSweepTest(bool useGhostObjectSweepTest)
Definition: KinematicCharacterController.hpp:172
void stepUp(btCollisionWorld *collisionWorld)
Definition: KinematicCharacterController.cpp:272
btScalar m_gravity
Definition: KinematicCharacterController.hpp:63
bool canJump() const
Definition: KinematicCharacterController.cpp:694
btVector3 m_currentPosition
Definition: KinematicCharacterController.hpp:76
bool m_wasOnGround
Definition: KinematicCharacterController.hpp:86
btVector3 computeReflectionDirection(const btVector3 &direction, const btVector3 &normal)
Definition: KinematicCharacterController.cpp:135
btPairCachingGhostObject * getGhostObject()
Definition: KinematicCharacterController.cpp:191
virtual void updateAction(btCollisionWorld *collisionWorld, btScalar deltaTime)
btActionInterface interface
Definition: KinematicCharacterController.hpp:118
void jump(const btVector3 &dir=btVector3())
Definition: KinematicCharacterController.cpp:698
btManifoldArray m_manifoldArray
keep track of the contact manifolds
Definition: KinematicCharacterController.hpp:81
void stepDown(btCollisionWorld *collisionWorld, btScalar dt)
Definition: KinematicCharacterController.cpp:448
btConvexShape * m_convexShape
Definition: KinematicCharacterController.hpp:52
void reset(btCollisionWorld *collisionWorld)
Definition: KinematicCharacterController.cpp:599
KinematicCharacterController is an object that supports a sliding motion in a world.
Definition: KinematicCharacterController.hpp:46
bool recoverFromPenetration(btCollisionWorld *collisionWorld)
Definition: KinematicCharacterController.cpp:195
btScalar m_verticalOffset
Definition: KinematicCharacterController.hpp:56
void setMaxJumpHeight(btScalar maxJumpHeight)
Definition: KinematicCharacterController.cpp:690