Changeset 1819 for trunk/test/Physics/Include
- Timestamp:
- Aug 25, 2012, 7:21:55 PM (10 years ago)
- Location:
- trunk/test/Physics/Include
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/Physics/Include/BulletCharacterController.h
r1802 r1819 25 25 #endif 26 26 27 #define USE_LOL_CTRLR_CHARAC 28 27 29 namespace lol 28 30 { … … 31 33 { 32 34 33 #if 035 #ifdef USE_LOL_CTRLR_CHARAC 34 36 #ifdef HAVE_PHYS_USE_BULLET 35 37 … … 37 39 ///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 40 ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user. 39 class BulletKinematicCharacterController : public bt CharacterControllerInterface41 class BulletKinematicCharacterController : public btActionInterface 40 42 { 43 public: 44 BulletKinematicCharacterController(btPairCachingGhostObject* NewGhostObject, btConvexShape* NewConvexShape, float NewStepHeight, int NewUpAxis=1) 45 { 46 m_convex_shape = NewConvexShape; 47 m_up_axis = NewUpAxis; 48 m_ghost_object = NewGhostObject; 49 m_step_height = NewStepHeight; 50 51 m_added_margin = 0.02f; 52 m_walk_direction = vec3(.0f, .0f, .0f); 53 m_do_gobject_sweep_test = true; 54 m_turn_angle = .0f; 55 m_use_walk_direction = false; // Should remove walk direction, this doesn't work correctly. 56 m_velocity_time_interval = .0f; 57 m_vertical_velocity = .0f; 58 m_vertical_offset = .0f; 59 m_gravity = 9.8f * 3.f; // 3G acceleration. 60 m_fall_speed = 55.f; // Terminal velocity of a sky diver in m/s. 61 m_jump_speed = 10.f; // ? 62 m_was_on_ground = false; 63 m_was_jumping = false; 64 SetMaxSlope(45.f); 65 } 66 ~BulletKinematicCharacterController() { } 67 41 68 protected: 42 69 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; 70 static vec3* GetUpAxisDirections() 71 { 72 static vec3 sUpAxisDirection[3] = { vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f) }; 73 74 return sUpAxisDirection; 75 } 76 77 //-------------------------- 78 //CONVENIENCE FUNCTIONS 79 //-- 80 81 //Returns the reflection Direction of a ray going 'Direction' hitting a surface with Normal 'Normal' from: http://www-cs-students.stanford.edu/~adityagp/final/node3.html 82 vec3 GetReflectedDir(const vec3& Direction, const vec3& Normal) 83 { 84 return Direction - (2.f * dot(Direction, Normal) * Normal); 85 } 86 //Returns the portion of 'direction' that is parallel to 'normal' 87 vec3 ProjectDirOnNorm(const vec3& Direction, const vec3& Normal) 88 { 89 return Normal * dot(Direction, Normal); 90 } 91 //Returns the portion of 'Direction' that is perpindicular to 'Normal' 92 vec3 ProjectDirOnNormPerpindicular(const vec3& Direction, const vec3& Normal) 93 { 94 return Direction - ProjectDirOnNorm(Direction, Normal); 95 } 96 //Returns Ghost Object. -duh- 97 btPairCachingGhostObject* GetGhostObject() 98 { 99 return m_ghost_object; 100 } 101 102 //"Real" war functions 103 bool RecoverFromPenetration(btCollisionWorld* CollisionWorld); 104 void UpdateTargetOnHit(const vec3& hit_normal, float TangentMag = .0f, float NormalMag = 1.f); 105 void StepUp(btCollisionWorld* CollisionWorld); 106 void StepForwardAndStrafe(btCollisionWorld* CollisionWorld, const vec3& MoveStep); 107 void StepDown(btCollisionWorld* CollisionWorld, float DeltaTime); 108 109 public: 110 ///btActionInterface interface : KEEP IN camelCase 111 virtual void updateAction(btCollisionWorld* CollisionWorld, float deltaTime) 112 { 113 PreStep(CollisionWorld); 114 PlayerStep(CollisionWorld, deltaTime); 115 } 116 117 //not in the interface, but called above 118 void PreStep(btCollisionWorld* CollisionWorld); 119 void PlayerStep(btCollisionWorld* CollisionWorld, float DeltaTime); 120 121 ///btActionInterface interface : KEEP IN camelCase 122 void debugDraw(btIDebugDraw* debugDrawer) { } 123 124 void SetUpAxis(int NewAxis) 125 { 126 if (NewAxis < 0) 127 NewAxis = 0; 128 if (NewAxis > 2) 129 NewAxis = 2; 130 m_up_axis = NewAxis; 131 } 132 133 //!!!!!! SHOULD DITCH THAT !!!!!! 134 //This should probably be called setPositionIncrementPerSimulatorStep. 135 //This is neither a Direction nor a velocity, but the amount to 136 //increment the position each simulation iteration, regardless 137 //of DeltaTime. 138 //This call will Reset any velocity set by SetVelocityForTimeInterval(). 139 virtual void SetWalkDirection(const vec3& walkDirection) 140 { 141 m_use_walk_direction = true; 142 m_walk_direction = walkDirection; 143 m_normalized_direction = normalize(m_walk_direction); 144 } 145 146 //Caller provides a velocity with which the character should MoveStep for 147 //the given time period. After the time period, velocity is Reset 148 //to zero. 149 //This call will Reset any walk Direction set by SetWalkDirection(). 150 //Negative time intervals will result in no motion. 151 virtual void SetVelocityForTimeInterval(const vec3& velocity, float timeInterval) 152 { 153 m_use_walk_direction = false; 154 m_walk_direction = velocity; 155 m_normalized_direction = normalize(m_walk_direction); 156 m_velocity_time_interval = timeInterval; 157 } 158 159 //Usefulness ? 160 void Reset() { } 161 void Warp(const vec3& NewOrigin) 162 { 163 btTransform NewTransform; 164 NewTransform.setIdentity(); 165 NewTransform.setOrigin(LOL2BTU_VEC3(NewOrigin)); 166 m_ghost_object->setWorldTransform(NewTransform); 167 } 168 169 //External Setup 170 //-- 171 172 void SetFallSpeed(float NewFallSpeed) { m_fall_speed = NewFallSpeed; } 173 void SetJumpSpeed(float NewJumpSpeed) { m_jump_speed = NewJumpSpeed; } 174 void SetMaxJumpHeight(float NewMaxJumpHeight) { m_max_jump_height = NewMaxJumpHeight; } 175 176 //Jump logic will go in EasyCC 177 bool CanJump() const { return OnGround(); } 178 void Jump(); 179 180 //NewGravity functions 181 void SetGravity(float NewGravity) { m_gravity = NewGravity; } 182 float GetGravity() const { return m_gravity; } 183 184 //The max slope determines the maximum angle that the controller can walk up. 185 //The slope angle is measured in radians. 186 void SetMaxSlope(float NewSlopeRadians) { m_max_slope_radians = NewSlopeRadians; m_max_slope_cosine = lol::cos(NewSlopeRadians); } 187 float GetMaxSlope() const { return m_max_slope_radians; } 188 189 void SetUseGhostSweepTest(bool UseGObjectSweepTest) { m_do_gobject_sweep_test = UseGObjectSweepTest; } 190 191 bool OnGround() const { return m_vertical_velocity == .0f && m_vertical_offset == .0f; } 192 193 private: 194 195 btPairCachingGhostObject* m_ghost_object; 196 btConvexShape* m_convex_shape; //is also in m_ghost_object, but it needs to be convex, so we store it here to avoid upcast 197 198 //keep track of the contact manifolds 199 btManifoldArray m_manifold_array; 200 201 float m_half_height; 202 float m_velocity_time_interval; 203 float m_vertical_velocity; 204 float m_vertical_offset; 205 float m_fall_speed; 206 float m_jump_speed; 207 float m_max_jump_height; 208 float m_max_slope_radians; // Slope angle that is set (used for returning the exact value) 209 float m_max_slope_cosine; // Cosine equivalent of m_max_slope_radians (calculated once when set, for optimization) 210 float m_gravity; 211 float m_turn_angle; 212 float m_step_height; 213 float m_added_margin;//@todo: remove this and fix the code 214 215 ///this is the desired walk Direction, set by the user 216 vec3 m_walk_direction; 217 vec3 m_normalized_direction; 66 218 67 219 //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; 220 vec3 m_current_position; 221 float m_current_step_offset; 222 vec3 m_target_position; 223 224 vec3 m_touching_normal; 225 bool m_touching_contact; 226 227 bool m_was_on_ground; 228 bool m_was_jumping; 229 bool m_do_gobject_sweep_test; 230 bool m_use_walk_direction; 231 int m_up_axis; 163 232 }; 164 233 165 234 #endif // HAVE_PHYS_USE_BULLET 166 #endif // 0235 #endif // USE_LOL_CTRLR_CHARAC 167 236 168 237 } /* namespace phys */ -
trunk/test/Physics/Include/EasyCharacterController.h
r1795 r1819 24 24 #include "core.h" 25 25 #include "EasyPhysics.h" 26 #include "BulletCharacterController.h" 26 27 #include <BulletDynamics/Character/btKinematicCharacterController.h> 27 28 #endif … … 83 84 84 85 btPairCachingGhostObject* m_pair_caching_object; 85 btKinematicCharacterController* m_character; 86 //btKinematicCharacterController* m_character; 87 BulletKinematicCharacterController* m_character; 86 88 87 89 float m_step_height; -
trunk/test/Physics/Include/EasyPhysics.h
r1782 r1819 23 23 #include <bullet/btBulletCollisionCommon.h> 24 24 #include <bullet/BulletCollision/CollisionDispatch/btGhostObject.h> 25 #endif 25 #endif //HAVE_PHYS_USE_BULLET 26 26 27 27 namespace lol … … 157 157 158 158 //Base/Attachment logic 159 Array<EasyPhysic*> m_based_physic_list; //List of objects based on this : this object moves, its based object movewith it.159 Array<EasyPhysic*> m_based_physic_list; //List of objects based on this : this object moves, its based object MoveStep with it. 160 160 EasyPhysic* m_base_physic; //Base for this object : The base moves, the object moves with it. 161 161 bool m_base_lock_location; //when this is TRUE, location moves with rotation change. -
trunk/test/Physics/Include/LolPhysics.h
r1782 r1819 119 119 } 120 120 121 //R eap-Off of the btKinematicClosestNotMeRayResultCallback122 class LolClosestNotMeRayResultCallback : public btCollisionWorld::ClosestRayResultCallback121 //Rip-Off of the btKinematicClosestNotMeRayResultCallback 122 class ClosestNotMeRayResultCallback : public btCollisionWorld::ClosestRayResultCallback 123 123 { 124 124 public: 125 LolClosestNotMeRayResultCallback(btCollisionObject* Me, const btVector3& rayFromWorld, const btVector3& rayToWorld) :125 ClosestNotMeRayResultCallback(btCollisionObject* Me, const btVector3& rayFromWorld, const btVector3& rayToWorld) : 126 126 btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld) 127 127 { … … 169 169 { 170 170 if (SourceCaster) 171 BtRayResult_Closest = new LolClosestNotMeRayResultCallback(SourceCaster->m_collision_object, LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo));171 BtRayResult_Closest = new ClosestNotMeRayResultCallback(SourceCaster->m_collision_object, LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo)); 172 172 else 173 173 BtRayResult_Closest = new btCollisionWorld::ClosestRayResultCallback(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo));
Note: See TracChangeset
for help on using the changeset viewer.