• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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.
ATTRIBUTE_ALIGNED16(class)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_verticalVelocity;
47 	btScalar m_verticalOffset;
48 	btScalar m_fallSpeed;
49 	btScalar m_jumpSpeed;
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 
61 	///this is the desired walk direction, set by the user
62 	btVector3	m_walkDirection;
63 	btVector3	m_normalizedDirection;
64 
65 	//some internal variables
66 	btVector3 m_currentPosition;
67 	btScalar  m_currentStepOffset;
68 	btVector3 m_targetPosition;
69 
70 	///keep track of the contact manifolds
71 	btManifoldArray	m_manifoldArray;
72 
73 	bool m_touchingContact;
74 	btVector3 m_touchingNormal;
75 
76 	bool  m_wasOnGround;
77 	bool  m_wasJumping;
78 	bool	m_useGhostObjectSweepTest;
79 	bool	m_useWalkDirection;
80 	btScalar	m_velocityTimeInterval;
81 	int m_upAxis;
82 
83 	static btVector3* getUpAxisDirections();
84 	bool  m_interpolateUp;
85 	bool  full_drop;
86 	bool  bounce_fix;
87 
88 	btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal);
89 	btVector3 parallelComponent (const btVector3& direction, const btVector3& normal);
90 	btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal);
91 
92 	bool recoverFromPenetration ( btCollisionWorld* collisionWorld);
93 	void stepUp (btCollisionWorld* collisionWorld);
94 	void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0));
95 	void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove);
96 	void stepDown (btCollisionWorld* collisionWorld, btScalar dt);
97 public:
98 
99 	BT_DECLARE_ALIGNED_ALLOCATOR();
100 
101 	btKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1);
102 	~btKinematicCharacterController ();
103 
104 
105 	///btActionInterface interface
106 	virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime)
107 	{
108 		preStep ( collisionWorld);
109 		playerStep (collisionWorld, deltaTime);
110 	}
111 
112 	///btActionInterface interface
113 	void	debugDraw(btIDebugDraw* debugDrawer);
114 
115 	void setUpAxis (int axis)
116 	{
117 		if (axis < 0)
118 			axis = 0;
119 		if (axis > 2)
120 			axis = 2;
121 		m_upAxis = axis;
122 	}
123 
124 	/// This should probably be called setPositionIncrementPerSimulatorStep.
125 	/// This is neither a direction nor a velocity, but the amount to
126 	///	increment the position each simulation iteration, regardless
127 	///	of dt.
128 	/// This call will reset any velocity set by setVelocityForTimeInterval().
129 	virtual void	setWalkDirection(const btVector3& walkDirection);
130 
131 	/// Caller provides a velocity with which the character should move for
132 	///	the given time period.  After the time period, velocity is reset
133 	///	to zero.
134 	/// This call will reset any walk direction set by setWalkDirection().
135 	/// Negative time intervals will result in no motion.
136 	virtual void setVelocityForTimeInterval(const btVector3& velocity,
137 				btScalar timeInterval);
138 
139 	void reset ( btCollisionWorld* collisionWorld );
140 	void warp (const btVector3& origin);
141 
142 	void preStep (  btCollisionWorld* collisionWorld);
143 	void playerStep ( btCollisionWorld* collisionWorld, btScalar dt);
144 
145 	void setFallSpeed (btScalar fallSpeed);
146 	void setJumpSpeed (btScalar jumpSpeed);
147 	void setMaxJumpHeight (btScalar maxJumpHeight);
148 	bool canJump () const;
149 
150 	void jump ();
151 
152 	void setGravity(btScalar gravity);
153 	btScalar getGravity() const;
154 
155 	/// The max slope determines the maximum angle that the controller can walk up.
156 	/// The slope angle is measured in radians.
157 	void setMaxSlope(btScalar slopeRadians);
158 	btScalar getMaxSlope() const;
159 
160 	btPairCachingGhostObject* getGhostObject();
161 	void	setUseGhostSweepTest(bool useGhostObjectSweepTest)
162 	{
163 		m_useGhostObjectSweepTest = useGhostObjectSweepTest;
164 	}
165 
166 	bool onGround () const;
167 	void setUpInterpolate (bool value);
168 };
169 
170 #endif // BT_KINEMATIC_CHARACTER_CONTROLLER_H
171