Bullet Collision Detection & Physics Library: btKinematicCharacterController.h Source File

Bullet Collision Detection & Physics Library

btKinematicCharacterController.h
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#ifndef BT_KINEMATIC_CHARACTER_CONTROLLER_H
17#define BT_KINEMATIC_CHARACTER_CONTROLLER_H
18
19#include "LinearMath/btVector3.h"
20
21#include "btCharacterControllerInterface.h"
22
23#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
24
25class btCollisionShape;
26class btConvexShape;
27class btRigidBody;
28class btCollisionWorld;
29class btCollisionDispatcher;
30class btPairCachingGhostObject;
31
35 ATTRIBUTE_ALIGNED16(class)
36 btKinematicCharacterController : public btCharacterControllerInterface
37{
38protected:
39 btScalar m_halfHeight;
40
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
43
44 btScalar m_maxPenetrationDepth;
45 btScalar m_verticalVelocity;
46 btScalar m_verticalOffset;
47 btScalar m_fallSpeed;
48 btScalar m_jumpSpeed;
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)
53 btScalar m_gravity;
54
55 btScalar m_turnAngle;
56
57 btScalar m_stepHeight;
58
59 btScalar m_addedMargin; //@todo: remove this and fix the code
60
62 btVector3 m_walkDirection;
63 btVector3 m_normalizedDirection;
64 btVector3 m_AngVel;
65
66 btVector3 m_jumpPosition;
67
68 //some internal variables
69 btVector3 m_currentPosition;
70 btScalar m_currentStepOffset;
71 btVector3 m_targetPosition;
72
73 btQuaternion m_currentOrientation;
74 btQuaternion m_targetOrientation;
75
77 btManifoldArray m_manifoldArray;
78
79 bool m_touchingContact;
80 btVector3 m_touchingNormal;
81
82 btScalar m_linearDamping;
83 btScalar m_angularDamping;
84
85 bool m_wasOnGround;
86 bool m_wasJumping;
87 bool m_useGhostObjectSweepTest;
88 bool m_useWalkDirection;
89 btScalar m_velocityTimeInterval;
90 btVector3 m_up;
91 btVector3 m_jumpAxis;
92
93 static btVector3* getUpAxisDirections();
94 bool m_interpolateUp;
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, 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);
107
108 virtual bool needsCollision(const btCollisionObject* body0, const btCollisionObject* body1);
109
110 void setUpVector(const btVector3& up);
111
112 btQuaternion getRotation(btVector3 & v0, btVector3 & v1) const;
113
114public:
115 BT_DECLARE_ALIGNED_ALLOCATOR();
116
117 btKinematicCharacterController(btPairCachingGhostObject * ghostObject, btConvexShape * convexShape, btScalar stepHeight, const btVector3& up = btVector3(1.0, 0.0, 0.0));
118 ~btKinematicCharacterController();
119
121 virtual void updateAction(btCollisionWorld * collisionWorld, btScalar deltaTime)
122 {
123 preStep(collisionWorld);
124 playerStep(collisionWorld, deltaTime);
125 }
126
128 void debugDraw(btIDebugDraw * debugDrawer);
129
130 void setUp(const btVector3& up);
131
132 const btVector3& getUp() { return m_up; }
133
139 virtual void setWalkDirection(const btVector3& walkDirection);
140
146 virtual void setVelocityForTimeInterval(const btVector3& velocity,
147 btScalar timeInterval);
148
149 virtual void setAngularVelocity(const btVector3& velocity);
150 virtual const btVector3& getAngularVelocity() const;
151
152 virtual void setLinearVelocity(const btVector3& velocity);
153 virtual btVector3 getLinearVelocity() const;
154
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; }
159
160 void reset(btCollisionWorld * collisionWorld);
161 void warp(const btVector3& origin);
162
163 void preStep(btCollisionWorld * collisionWorld);
164 void playerStep(btCollisionWorld * collisionWorld, btScalar dt);
165
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;
174
175 void jump(const btVector3& v = btVector3(0, 0, 0));
176
177 void applyImpulse(const btVector3& v) { jump(v); }
178
179 void setGravity(const btVector3& gravity);
180 btVector3 getGravity() const;
181
184 void setMaxSlope(btScalar slopeRadians);
185 btScalar getMaxSlope() const;
186
187 void setMaxPenetrationDepth(btScalar d);
188 btScalar getMaxPenetrationDepth() const;
189
190 btPairCachingGhostObject* getGhostObject();
191 void setUseGhostSweepTest(bool useGhostObjectSweepTest)
192 {
193 m_useGhostObjectSweepTest = useGhostObjectSweepTest;
194 }
195
196 bool onGround() const;
197 void setUpInterpolate(bool value);
198};
199
200#endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H
btCharacterControllerInterface.h
btCollisionAlgorithm.h
btManifoldArray
btAlignedObjectArray< btPersistentManifold * > btManifoldArray
Definition btCollisionAlgorithm.h:30
btClamped
const T & btClamped(const T &a, const T &lb, const T &ub)
Definition btMinMax.h:33
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
ATTRIBUTE_ALIGNED16
#define ATTRIBUTE_ALIGNED16(a)
Definition btScalar.h:99
btVector3.h
btCharacterControllerInterface::btCharacterControllerInterface
btCharacterControllerInterface()
Definition btCharacterControllerInterface.h:29
btCollisionDispatcher
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs.
Definition btCollisionDispatcher.h:43
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition btCollisionObject.h:51
btCollisionShape
The btCollisionShape class provides an interface for collision shapes that can be shared among btColl...
Definition btCollisionShape.h:28
btCollisionWorld
CollisionWorld is interface and container for the collision detection.
Definition btCollisionWorld.h:86
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition btConvexShape.h:33
btIDebugDraw
The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations.
Definition btIDebugDraw.h:27
btKinematicCharacterController::m_addedMargin
btScalar m_addedMargin
Definition btKinematicCharacterController.h:59
btKinematicCharacterController::setUpVector
void setUpVector(const btVector3 &up)
Definition btKinematicCharacterController.cpp:964
btKinematicCharacterController::m_targetOrientation
btQuaternion m_targetOrientation
Definition btKinematicCharacterController.h:74
btKinematicCharacterController::perpindicularComponent
btVector3 perpindicularComponent(const btVector3 &direction, const btVector3 &normal)
Definition btKinematicCharacterController.cpp:129
btKinematicCharacterController::m_walkDirection
btVector3 m_walkDirection
this is the desired walk direction, set by the user
Definition btKinematicCharacterController.h:62
btKinematicCharacterController::m_touchingContact
bool m_touchingContact
Definition btKinematicCharacterController.h:79
btKinematicCharacterController::needsCollision
virtual bool needsCollision(const btCollisionObject *body0, const btCollisionObject *body1)
Definition btKinematicCharacterController.cpp:340
btKinematicCharacterController::m_maxSlopeRadians
btScalar m_maxSlopeRadians
Definition btKinematicCharacterController.h:51
btKinematicCharacterController::getFallSpeed
btScalar getFallSpeed() const
Definition btKinematicCharacterController.h:169
btKinematicCharacterController::m_currentOrientation
btQuaternion m_currentOrientation
Definition btKinematicCharacterController.h:73
btKinematicCharacterController::m_targetPosition
btVector3 m_targetPosition
Definition btKinematicCharacterController.h:71
btKinematicCharacterController::getStepHeight
btScalar getStepHeight() const
Definition btKinematicCharacterController.h:167
btKinematicCharacterController::playerStep
void playerStep(btCollisionWorld *collisionWorld, btScalar dt)
Definition btKinematicCharacterController.cpp:710
btKinematicCharacterController::getJumpSpeed
btScalar getJumpSpeed() const
Definition btKinematicCharacterController.h:171
btKinematicCharacterController::m_AngVel
btVector3 m_AngVel
Definition btKinematicCharacterController.h:64
btKinematicCharacterController::preStep
void preStep(btCollisionWorld *collisionWorld)
Definition btKinematicCharacterController.cpp:700
btKinematicCharacterController::m_jumpSpeed
btScalar m_jumpSpeed
Definition btKinematicCharacterController.h:48
btKinematicCharacterController::m_currentPosition
btVector3 m_currentPosition
Definition btKinematicCharacterController.h:69
btKinematicCharacterController::computeReflectionDirection
btVector3 computeReflectionDirection(const btVector3 &direction, const btVector3 &normal)
Definition btKinematicCharacterController.cpp:112
btKinematicCharacterController::applyImpulse
void applyImpulse(const btVector3 &v)
Definition btKinematicCharacterController.h:177
btKinematicCharacterController::updateAction
virtual void updateAction(btCollisionWorld *collisionWorld, btScalar deltaTime)
btActionInterface interface
Definition btKinematicCharacterController.h:121
btKinematicCharacterController::m_maxPenetrationDepth
btScalar m_maxPenetrationDepth
Definition btKinematicCharacterController.h:44
btKinematicCharacterController::setUseGhostSweepTest
void setUseGhostSweepTest(bool useGhostObjectSweepTest)
Definition btKinematicCharacterController.h:191
btKinematicCharacterController::stepDown
void stepDown(btCollisionWorld *collisionWorld, btScalar dt)
Definition btKinematicCharacterController.cpp:466
btKinematicCharacterController::m_interpolateUp
bool m_interpolateUp
Definition btKinematicCharacterController.h:94
btKinematicCharacterController::m_maxJumpHeight
btScalar m_maxJumpHeight
Definition btKinematicCharacterController.h:50
btKinematicCharacterController::m_velocityTimeInterval
btScalar m_velocityTimeInterval
Definition btKinematicCharacterController.h:89
btKinematicCharacterController::m_useWalkDirection
bool m_useWalkDirection
Definition btKinematicCharacterController.h:88
btKinematicCharacterController::m_manifoldArray
btManifoldArray m_manifoldArray
keep track of the contact manifolds
Definition btKinematicCharacterController.h:77
btKinematicCharacterController::m_touchingNormal
btVector3 m_touchingNormal
Definition btKinematicCharacterController.h:80
btKinematicCharacterController::btKinematicCharacterController
btKinematicCharacterController(btPairCachingGhostObject *ghostObject, btConvexShape *convexShape, btScalar stepHeight, const btVector3 &up=btVector3(1.0, 0.0, 0.0))
Definition btKinematicCharacterController.cpp:134
btKinematicCharacterController::jump
void jump(const btVector3 &v=btVector3(0, 0, 0))
Definition btKinematicCharacterController.cpp:873
btKinematicCharacterController::m_SetjumpSpeed
btScalar m_SetjumpSpeed
Definition btKinematicCharacterController.h:49
btKinematicCharacterController::m_useGhostObjectSweepTest
bool m_useGhostObjectSweepTest
Definition btKinematicCharacterController.h:87
btKinematicCharacterController::m_verticalVelocity
btScalar m_verticalVelocity
Definition btKinematicCharacterController.h:45
btKinematicCharacterController::m_currentStepOffset
btScalar m_currentStepOffset
Definition btKinematicCharacterController.h:70
btKinematicCharacterController::m_verticalOffset
btScalar m_verticalOffset
Definition btKinematicCharacterController.h:46
btKinematicCharacterController::m_gravity
btScalar m_gravity
Definition btKinematicCharacterController.h:53
btKinematicCharacterController::stepForwardAndStrafe
void stepForwardAndStrafe(btCollisionWorld *collisionWorld, const btVector3 &walkMove)
Definition btKinematicCharacterController.cpp:384
btKinematicCharacterController::setLinearDamping
void setLinearDamping(btScalar d)
Definition btKinematicCharacterController.h:155
btKinematicCharacterController::getAngularDamping
btScalar getAngularDamping() const
Definition btKinematicCharacterController.h:158
btKinematicCharacterController::m_stepHeight
btScalar m_stepHeight
Definition btKinematicCharacterController.h:57
btKinematicCharacterController::updateTargetPositionBasedOnCollision
void updateTargetPositionBasedOnCollision(const btVector3 &hit_normal, btScalar tangentMag=btScalar(0.0), btScalar normalMag=btScalar(1.0))
Definition btKinematicCharacterController.cpp:347
btKinematicCharacterController::stepUp
void stepUp(btCollisionWorld *collisionWorld)
Definition btKinematicCharacterController.cpp:257
btKinematicCharacterController::getLinearDamping
btScalar getLinearDamping() const
Definition btKinematicCharacterController.h:156
btKinematicCharacterController::m_maxSlopeCosine
btScalar m_maxSlopeCosine
Definition btKinematicCharacterController.h:52
btKinematicCharacterController::getUp
const btVector3 & getUp()
Definition btKinematicCharacterController.h:132
btKinematicCharacterController::bounce_fix
bool bounce_fix
Definition btKinematicCharacterController.h:96
btKinematicCharacterController::m_angularDamping
btScalar m_angularDamping
Definition btKinematicCharacterController.h:83
btKinematicCharacterController::m_turnAngle
btScalar m_turnAngle
Definition btKinematicCharacterController.h:55
btKinematicCharacterController::m_linearDamping
btScalar m_linearDamping
Definition btKinematicCharacterController.h:82
btKinematicCharacterController::m_halfHeight
btScalar m_halfHeight
Definition btKinematicCharacterController.h:39
btKinematicCharacterController::m_convexShape
btConvexShape * m_convexShape
Definition btKinematicCharacterController.h:42
btKinematicCharacterController::BT_DECLARE_ALIGNED_ALLOCATOR
BT_DECLARE_ALIGNED_ALLOCATOR()
btKinematicCharacterController::m_normalizedDirection
btVector3 m_normalizedDirection
Definition btKinematicCharacterController.h:63
btKinematicCharacterController::m_wasJumping
bool m_wasJumping
Definition btKinematicCharacterController.h:86
btKinematicCharacterController::setAngularDamping
void setAngularDamping(btScalar d)
Definition btKinematicCharacterController.h:157
btKinematicCharacterController::getRotation
btQuaternion getRotation(btVector3 &v0, btVector3 &v1) const
Definition btKinematicCharacterController.cpp:987
btKinematicCharacterController::parallelComponent
btVector3 parallelComponent(const btVector3 &direction, const btVector3 &normal)
Definition btKinematicCharacterController.cpp:120
btKinematicCharacterController::m_fallSpeed
btScalar m_fallSpeed
Definition btKinematicCharacterController.h:47
btKinematicCharacterController::full_drop
bool full_drop
Definition btKinematicCharacterController.h:95
btKinematicCharacterController::recoverFromPenetration
bool recoverFromPenetration(btCollisionWorld *collisionWorld)
Definition btKinematicCharacterController.cpp:177
btKinematicCharacterController::m_jumpAxis
btVector3 m_jumpAxis
Definition btKinematicCharacterController.h:91
btKinematicCharacterController::getUpAxisDirections
static btVector3 * getUpAxisDirections()
Definition btKinematicCharacterController.cpp:937
btKinematicCharacterController::m_jumpPosition
btVector3 m_jumpPosition
Definition btKinematicCharacterController.h:66
btKinematicCharacterController::m_ghostObject
btPairCachingGhostObject * m_ghostObject
Definition btKinematicCharacterController.h:41
btKinematicCharacterController::m_wasOnGround
bool m_wasOnGround
Definition btKinematicCharacterController.h:85
btKinematicCharacterController::m_up
btVector3 m_up
Definition btKinematicCharacterController.h:90
btPairCachingGhostObject
Definition btGhostObject.h:97
btQuaternion
The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatr...
Definition btQuaternion.h:50
btRigidBody
The btRigidBody is the main class for rigid body objects.
Definition btRigidBody.h:60
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82