1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com> |
---|
6 | // This program is free software; you can redistribute it and/or |
---|
7 | // modify it under the terms of the Do What The Fuck You Want To |
---|
8 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
10 | // |
---|
11 | |
---|
12 | // |
---|
13 | // The BulletCharacterController class |
---|
14 | // ------------------ |
---|
15 | // This class is a equivalent of btKinematicCharacterController, but more useful for Lol. |
---|
16 | // |
---|
17 | |
---|
18 | #if !defined __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__ |
---|
19 | #define __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__ |
---|
20 | |
---|
21 | #ifdef HAVE_PHYS_USE_BULLET
|
---|
22 | #include "core.h"
|
---|
23 | #include "EasyPhysics.h"
|
---|
24 | //#include "BulletDynamics\Character\btCharacterControllerInterface.h" |
---|
25 | #endif
|
---|
26 |
|
---|
27 | namespace lol |
---|
28 | { |
---|
29 | |
---|
30 | namespace phys |
---|
31 | { |
---|
32 | |
---|
33 | #if 0 |
---|
34 | #ifdef HAVE_PHYS_USE_BULLET |
---|
35 | |
---|
36 | ///BulletKinematicCharacterController is an object that supports a sliding motion in a world. |
---|
37 | ///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. |
---|
38 | ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user. |
---|
39 | class BulletKinematicCharacterController : public btCharacterControllerInterface |
---|
40 | { |
---|
41 | protected: |
---|
42 | |
---|
43 | btScalar m_halfHeight; |
---|
44 | |
---|
45 | btPairCachingGhostObject* m_ghostObject; |
---|
46 | btConvexShape* m_convexShape;//is also in m_ghostObject, but it needs to be convex, so we store it here to avoid upcast |
---|
47 | |
---|
48 | btScalar m_verticalVelocity; |
---|
49 | btScalar m_verticalOffset; |
---|
50 | btScalar m_fallSpeed; |
---|
51 | btScalar m_jumpSpeed; |
---|
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 | |
---|
63 | ///this is the desired walk direction, set by the user |
---|
64 | btVector3 m_walkDirection; |
---|
65 | btVector3 m_normalizedDirection; |
---|
66 | |
---|
67 | //some internal variables |
---|
68 | btVector3 m_currentPosition; |
---|
69 | btScalar m_currentStepOffset; |
---|
70 | btVector3 m_targetPosition; |
---|
71 | |
---|
72 | ///keep track of the contact manifolds |
---|
73 | btManifoldArray m_manifoldArray; |
---|
74 | |
---|
75 | bool m_touchingContact; |
---|
76 | btVector3 m_touchingNormal; |
---|
77 | |
---|
78 | bool m_wasOnGround; |
---|
79 | bool m_wasJumping; |
---|
80 | bool m_useGhostObjectSweepTest; |
---|
81 | bool m_useWalkDirection; |
---|
82 | btScalar m_velocityTimeInterval; |
---|
83 | int m_upAxis; |
---|
84 | |
---|
85 | static btVector3* getUpAxisDirections(); |
---|
86 | |
---|
87 | btVector3 computeReflectionDirection (const btVector3& direction, const btVector3& normal); |
---|
88 | btVector3 parallelComponent (const btVector3& direction, const btVector3& normal); |
---|
89 | btVector3 perpindicularComponent (const btVector3& direction, const btVector3& normal); |
---|
90 | |
---|
91 | bool recoverFromPenetration ( btCollisionWorld* collisionWorld); |
---|
92 | void stepUp (btCollisionWorld* collisionWorld); |
---|
93 | void updateTargetPositionBasedOnCollision (const btVector3& hit_normal, btScalar tangentMag = btScalar(0.0), btScalar normalMag = btScalar(1.0)); |
---|
94 | void stepForwardAndStrafe (btCollisionWorld* collisionWorld, const btVector3& walkMove); |
---|
95 | void stepDown (btCollisionWorld* collisionWorld, btScalar dt); |
---|
96 | public: |
---|
97 | BulletKinematicCharacterController (btPairCachingGhostObject* ghostObject,btConvexShape* convexShape,btScalar stepHeight, int upAxis = 1); |
---|
98 | ~BulletKinematicCharacterController (); |
---|
99 | |
---|
100 | |
---|
101 | ///btActionInterface interface |
---|
102 | virtual void updateAction( btCollisionWorld* collisionWorld,btScalar deltaTime) |
---|
103 | { |
---|
104 | preStep ( collisionWorld); |
---|
105 | playerStep (collisionWorld, deltaTime); |
---|
106 | } |
---|
107 | |
---|
108 | ///btActionInterface interface |
---|
109 | void debugDraw(btIDebugDraw* debugDrawer); |
---|
110 | |
---|
111 | void setUpAxis (int axis) |
---|
112 | { |
---|
113 | if (axis < 0) |
---|
114 | axis = 0; |
---|
115 | if (axis > 2) |
---|
116 | axis = 2; |
---|
117 | m_upAxis = axis; |
---|
118 | } |
---|
119 | |
---|
120 | /// This should probably be called setPositionIncrementPerSimulatorStep. |
---|
121 | /// This is neither a direction nor a velocity, but the amount to |
---|
122 | /// increment the position each simulation iteration, regardless |
---|
123 | /// of dt. |
---|
124 | /// This call will reset any velocity set by setVelocityForTimeInterval(). |
---|
125 | virtual void setWalkDirection(const btVector3& walkDirection); |
---|
126 | |
---|
127 | /// Caller provides a velocity with which the character should move for |
---|
128 | /// the given time period. After the time period, velocity is reset |
---|
129 | /// to zero. |
---|
130 | /// This call will reset any walk direction set by setWalkDirection(). |
---|
131 | /// Negative time intervals will result in no motion. |
---|
132 | virtual void setVelocityForTimeInterval(const btVector3& velocity, |
---|
133 | btScalar timeInterval); |
---|
134 | |
---|
135 | void reset (); |
---|
136 | void warp (const btVector3& origin); |
---|
137 | |
---|
138 | void preStep ( btCollisionWorld* collisionWorld); |
---|
139 | void playerStep ( btCollisionWorld* collisionWorld, btScalar dt); |
---|
140 | |
---|
141 | void setFallSpeed (btScalar fallSpeed); |
---|
142 | void setJumpSpeed (btScalar jumpSpeed); |
---|
143 | void setMaxJumpHeight (btScalar maxJumpHeight); |
---|
144 | bool canJump () const; |
---|
145 | |
---|
146 | void jump (); |
---|
147 | |
---|
148 | void setGravity(btScalar gravity); |
---|
149 | btScalar getGravity() const; |
---|
150 | |
---|
151 | /// The max slope determines the maximum angle that the controller can walk up. |
---|
152 | /// The slope angle is measured in radians. |
---|
153 | void setMaxSlope(btScalar slopeRadians); |
---|
154 | btScalar getMaxSlope() const; |
---|
155 | |
---|
156 | btPairCachingGhostObject* getGhostObject(); |
---|
157 | void setUseGhostSweepTest(bool useGhostObjectSweepTest) |
---|
158 | { |
---|
159 | m_useGhostObjectSweepTest = useGhostObjectSweepTest; |
---|
160 | } |
---|
161 | |
---|
162 | bool onGround () const; |
---|
163 | }; |
---|
164 | |
---|
165 | #endif // HAVE_PHYS_USE_BULLET |
---|
166 | #endif // 0 |
---|
167 | |
---|
168 | } /* namespace phys */ |
---|
169 | |
---|
170 | } /* namespace lol */ |
---|
171 | |
---|
172 | #endif /* __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__ */ |
---|
173 | |
---|