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

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
22 #include "btCharacterControllerInterface.h"
23
24 #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
25
26
27 class btCollisionShape;
28 class btConvexShape;
29 class btRigidBody;
30 class btCollisionWorld;
31 class btCollisionDispatcher;
32 class btPairCachingGhostObject;
33
37 ATTRIBUTE_ALIGNED16(class) btKinematicCharacterController : public btCharacterControllerInterface
38 {
39 protected:
40
41 btScalar m_halfHeight;
42
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
45
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;
56
57 btScalar m_turnAngle;
58
59 btScalar m_stepHeight;
60
61 btScalar m_addedMargin;//@todo: remove this and fix the code
62
64 btVector3 m_walkDirection;
65 btVector3 m_normalizedDirection;
66 btVector3 m_AngVel;
67
68 btVector3 m_jumpPosition;
69
70 //some internal variables
71 btVector3 m_currentPosition;
72 btScalar m_currentStepOffset;
73 btVector3 m_targetPosition;
74
75 btQuaternion m_currentOrientation;
76 btQuaternion m_targetOrientation;
77
79 btManifoldArray m_manifoldArray;
80
81 bool m_touchingContact;
82 btVector3 m_touchingNormal;
83
84 btScalar m_linearDamping;
85 btScalar m_angularDamping;
86
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;
94
95 static btVector3* getUpAxisDirections();
96 bool m_interpolateUp;
97 bool full_drop;
98 bool bounce_fix;
99
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);
103
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);
109
110 virtual bool needsCollision(const btCollisionObject* body0, const btCollisionObject* body1);
111
112 void setUpVector(const btVector3& up);
113
114 btQuaternion getRotation(btVector3& v0, btVector3& v1) const;
115
116 public:
117
118 BT_DECLARE_ALIGNED_ALLOCATOR();
119
120 btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, const btVector3& up = btVector3(1.0,0.0,0.0));
121 ~btKinematicCharacterController ();
122
123
125 virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
126 {
127 preStep ( collisionWorld);
128 playerStep (collisionWorld, deltaTime);
129 }
130
132 void debugDraw(btIDebugDraw* debugDrawer);
133
134 void setUp(const btVector3& up);
135
136 const btVector3& getUp() { return m_up; }
137
143 virtual void setWalkDirection(const btVector3& walkDirection);
144
150 virtual void setVelocityForTimeInterval(const btVector3& velocity,
151 btScalar timeInterval);
152
153 virtual void setAngularVelocity(const btVector3& velocity);
154 virtual const btVector3& getAngularVelocity() const;
155
156 virtual void setLinearVelocity(const btVector3& velocity);
157 virtual btVector3 getLinearVelocity() const;
158
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; }
163
164 void reset ( btCollisionWorld* collisionWorld );
165 void warp (const btVector3& origin);
166
167 void preStep ( btCollisionWorld* collisionWorld);
168 void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
169
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;
178
179 void jump(const btVector3& v = btVector3());
180
181 void applyImpulse(const btVector3& v) { jump(v); }
182
183 void setGravity(const btVector3& gravity);
184 btVector3 getGravity() const;
185
188 void setMaxSlope(btScalar slopeRadians);
189 btScalar getMaxSlope() const;
190
191 void setMaxPenetrationDepth(btScalar d);
192 btScalar getMaxPenetrationDepth() const;
193
194 btPairCachingGhostObject* getGhostObject();
195 void setUseGhostSweepTest(bool useGhostObjectSweepTest)
196 {
197 m_useGhostObjectSweepTest = useGhostObjectSweepTest;
198 }
199
200 bool onGround () const;
201 void setUpInterpolate (bool value);
202 };
203
204 #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H
btKinematicCharacterController::m_fallSpeed
btScalar m_fallSpeed
Definition: btKinematicCharacterController.h:49
btKinematicCharacterController::getUp
const btVector3 & getUp()
Definition: btKinematicCharacterController.h:136
btKinematicCharacterController::m_touchingContact
bool m_touchingContact
Definition: btKinematicCharacterController.h:81
btKinematicCharacterController::m_ghostObject
btPairCachingGhostObject * m_ghostObject
Definition: btKinematicCharacterController.h:43
btKinematicCharacterController::getLinearDamping
btScalar getLinearDamping() const
Definition: btKinematicCharacterController.h:160
btKinematicCharacterController::m_jumpAxis
btVector3 m_jumpAxis
Definition: btKinematicCharacterController.h:93
btKinematicCharacterController::setUseGhostSweepTest
void setUseGhostSweepTest(bool useGhostObjectSweepTest)
Definition: btKinematicCharacterController.h:195
btKinematicCharacterController::m_velocityTimeInterval
btScalar m_velocityTimeInterval
Definition: btKinematicCharacterController.h:91
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition: btAlignedObjectArray.h:53
btVector3.h
btKinematicCharacterController::applyImpulse
void applyImpulse(const btVector3 &v)
Definition: btKinematicCharacterController.h:181
btKinematicCharacterController::bounce_fix
bool bounce_fix
Definition: btKinematicCharacterController.h:98
btKinematicCharacterController::m_linearDamping
btScalar m_linearDamping
Definition: btKinematicCharacterController.h:84
btCollisionShape
The btCollisionShape class provides an interface for collision shapes that can be shared among btColl...
Definition: btCollisionShape.h:27
btKinematicCharacterController::m_maxSlopeRadians
btScalar m_maxSlopeRadians
Definition: btKinematicCharacterController.h:53
btKinematicCharacterController::m_wasOnGround
bool m_wasOnGround
Definition: btKinematicCharacterController.h:87
btCollisionDispatcher
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs...
Definition: btCollisionDispatcher.h:43
btKinematicCharacterController::m_wasJumping
bool m_wasJumping
Definition: btKinematicCharacterController.h:88
btKinematicCharacterController::m_normalizedDirection
btVector3 m_normalizedDirection
Definition: btKinematicCharacterController.h:65
btKinematicCharacterController::m_maxSlopeCosine
btScalar m_maxSlopeCosine
Definition: btKinematicCharacterController.h:54
btKinematicCharacterController::setLinearDamping
void setLinearDamping(btScalar d)
Definition: btKinematicCharacterController.h:159
btKinematicCharacterController::m_maxJumpHeight
btScalar m_maxJumpHeight
Definition: btKinematicCharacterController.h:52
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
btKinematicCharacterController::m_stepHeight
btScalar m_stepHeight
Definition: btKinematicCharacterController.h:59
btKinematicCharacterController::m_verticalVelocity
btScalar m_verticalVelocity
Definition: btKinematicCharacterController.h:47
btCharacterControllerInterface
Definition: btCharacterControllerInterface.h:26
btKinematicCharacterController::m_maxPenetrationDepth
btScalar m_maxPenetrationDepth
Definition: btKinematicCharacterController.h:46
btKinematicCharacterController::getStepHeight
btScalar getStepHeight() const
Definition: btKinematicCharacterController.h:171
btKinematicCharacterController::m_halfHeight
btScalar m_halfHeight
Definition: btKinematicCharacterController.h:41
btKinematicCharacterController::setAngularDamping
void setAngularDamping(btScalar d)
Definition: btKinematicCharacterController.h:161
btKinematicCharacterController::m_jumpSpeed
btScalar m_jumpSpeed
Definition: btKinematicCharacterController.h:50
btKinematicCharacterController::m_touchingNormal
btVector3 m_touchingNormal
Definition: btKinematicCharacterController.h:82
btKinematicCharacterController::m_currentStepOffset
btScalar m_currentStepOffset
Definition: btKinematicCharacterController.h:72
btKinematicCharacterController::m_interpolateUp
bool m_interpolateUp
Definition: btKinematicCharacterController.h:96
btKinematicCharacterController::full_drop
bool full_drop
Definition: btKinematicCharacterController.h:97
btKinematicCharacterController::m_useGhostObjectSweepTest
bool m_useGhostObjectSweepTest
Definition: btKinematicCharacterController.h:89
btKinematicCharacterController::m_angularDamping
btScalar m_angularDamping
Definition: btKinematicCharacterController.h:85
btKinematicCharacterController::m_up
btVector3 m_up
Definition: btKinematicCharacterController.h:92
btKinematicCharacterController::m_useWalkDirection
bool m_useWalkDirection
Definition: btKinematicCharacterController.h:90
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:49
btIDebugDraw
The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations...
Definition: btIDebugDraw.h:29
btRigidBody
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:62
btKinematicCharacterController::m_currentPosition
btVector3 m_currentPosition
Definition: btKinematicCharacterController.h:71
btKinematicCharacterController::m_convexShape
btConvexShape * m_convexShape
Definition: btKinematicCharacterController.h:44
btKinematicCharacterController::updateAction
virtual void updateAction(btCollisionWorld *collisionWorld, btScalar deltaTime)
btActionInterface interface
Definition: btKinematicCharacterController.h:125
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
ATTRIBUTE_ALIGNED16
#define ATTRIBUTE_ALIGNED16(a)
Definition: btScalar.h:82
btKinematicCharacterController::m_manifoldArray
btManifoldArray m_manifoldArray
keep track of the contact manifolds
Definition: btKinematicCharacterController.h:79
btKinematicCharacterController::m_AngVel
btVector3 m_AngVel
Definition: btKinematicCharacterController.h:66
btKinematicCharacterController::m_walkDirection
btVector3 m_walkDirection
this is the desired walk direction, set by the user
Definition: btKinematicCharacterController.h:64
btKinematicCharacterController::getFallSpeed
btScalar getFallSpeed() const
Definition: btKinematicCharacterController.h:173
btKinematicCharacterController::m_targetPosition
btVector3 m_targetPosition
Definition: btKinematicCharacterController.h:73
btKinematicCharacterController::m_verticalOffset
btScalar m_verticalOffset
Definition: btKinematicCharacterController.h:48
btCollisionWorld
CollisionWorld is interface and container for the collision detection.
Definition: btCollisionWorld.h:88
btKinematicCharacterController
btKinematicCharacterController is an object that supports a sliding motion in a world.
Definition: btKinematicCharacterController.h:37
BT_DECLARE_ALIGNED_ALLOCATOR
#define BT_DECLARE_ALIGNED_ALLOCATOR()
Definition: btScalar.h:403
btKinematicCharacterController::m_SetjumpSpeed
btScalar m_SetjumpSpeed
Definition: btKinematicCharacterController.h:51
btKinematicCharacterController::getAngularDamping
btScalar getAngularDamping() const
Definition: btKinematicCharacterController.h:162
btKinematicCharacterController::m_targetOrientation
btQuaternion m_targetOrientation
Definition: btKinematicCharacterController.h:76
btKinematicCharacterController::m_currentOrientation
btQuaternion m_currentOrientation
Definition: btKinematicCharacterController.h:75
btCharacterControllerInterface.h
btQuaternion
The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatr...
Definition: btQuaternion.h:55
btKinematicCharacterController::m_jumpPosition
btVector3 m_jumpPosition
Definition: btKinematicCharacterController.h:68
btKinematicCharacterController::m_addedMargin
btScalar m_addedMargin
Definition: btKinematicCharacterController.h:61
btKinematicCharacterController::m_gravity
btScalar m_gravity
Definition: btKinematicCharacterController.h:55
btClamped
const T & btClamped(const T &a, const T &lb, const T &ub)
Definition: btMinMax.h:35
btKinematicCharacterController::getJumpSpeed
btScalar getJumpSpeed() const
Definition: btKinematicCharacterController.h:175
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
btKinematicCharacterController::m_turnAngle
btScalar m_turnAngle
Definition: btKinematicCharacterController.h:57
btPairCachingGhostObject
Definition: btGhostObject.h:99
btCollisionAlgorithm.h