Changeset 1888 for trunk/test/Physics/Include
- Timestamp:
- Sep 7, 2012, 5:00:31 PM (10 years ago)
- Location:
- trunk/test/Physics/Include
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/Physics/Include/BulletCharacterController.h
r1870 r1888 30 30 { 31 31 32 33 32 namespace phys 33 { 34 34 35 35 #ifdef USE_LOL_CTRLR_CHARAC 36 36 #ifdef HAVE_PHYS_USE_BULLET 37 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 btCollisionObject*m_me;73 const vec3m_up;74 floatm_min_slope_dot;75 76 77 78 79 80 81 82 83 84 85 m_convex_shape = NewConvexShape; 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 void SetFallSpeed(float NewFallSpeed){ m_fall_speed = NewFallSpeed; }210 void SetJumpSpeed(float NewJumpSpeed){ m_jump_speed = NewJumpSpeed; }211 void SetMaxJumpHeight(float NewMaxJumpHeight){ m_max_jump_height = NewMaxJumpHeight; }212 213 214 bool CanJump() const{ return OnGround(); }215 216 217 218 void SetGravity(float NewGravity){ m_f_gravity = NewGravity; }219 float GetGravity() const{ return m_f_gravity; }220 221 222 223 void SetMaxSlope(float NewSlopeRadians){ m_max_slope_radians = NewSlopeRadians; m_max_slope_cosine = lol::cos(NewSlopeRadians); }224 float GetMaxSlope() const{ return m_max_slope_radians; }225 226 227 228 bool OnGround() const{ return m_vertical_velocity == .0f && m_vertical_offset == .0f; }229 230 231 232 btPairCachingGhostObject*m_ghost_object;233 btConvexShape*m_convex_shape; //is also in m_ghost_object, but it needs to be convex, so we store it here to avoid upcast234 235 236 btManifoldArraym_manifold_array;237 238 floatm_half_height;239 floatm_velocity_time_interval;240 floatm_vertical_velocity;241 floatm_vertical_offset;242 floatm_fall_speed;243 floatm_jump_speed;244 floatm_max_jump_height;245 floatm_max_slope_radians; // Slope angle that is set (used for returning the exact value)246 floatm_max_slope_cosine; // Cosine equivalent of m_max_slope_radians (calculated once when set, for optimization)247 floatm_f_gravity;248 floatm_turn_angle;249 floatm_step_height;250 floatm_added_margin;//@todo: remove this and fix the code251 252 253 vec3m_walk_direction;254 vec3m_normalized_direction;255 256 257 vec3m_current_position;258 floatm_current_step_offset;259 vec3m_target_position;260 261 vec3m_touching_normal;262 boolm_touching_contact;263 264 boolm_was_on_ground;265 boolm_was_jumping;266 boolm_do_gobject_sweep_test;267 boolm_use_walk_direction;268 intm_i_up_axis;269 270 271 272 273 274 275 vec3m_gravity;276 277 278 vec3m_velocity;279 38 //SweepCallback used for Swweep Tests. 39 class ClosestNotMeConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback 40 { 41 public: 42 ClosestNotMeConvexResultCallback(btCollisionObject* NewMe, const vec3& NewUp, float MinSlopeDot) : 43 btCollisionWorld::ClosestConvexResultCallback(LOL2BTU_VEC3(vec3(.0f)), LOL2BTU_VEC3(vec3(.0f))), 44 m_me(NewMe), 45 m_up(NewUp), 46 m_min_slope_dot(MinSlopeDot) { } 47 48 virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult& ConvexResult, bool NormalInWorld) 49 { 50 //We hit ourselves, FAIL 51 if (ConvexResult.m_hitCollisionObject == m_me) 52 return btScalar(1.f); 53 54 vec3 WorldHitNomal(.0f); 55 if (NormalInWorld) 56 WorldHitNomal = BT2LOL_VEC3(ConvexResult.m_hitNormalLocal); 57 else //need to transform Normal into worldspace 58 { 59 btVector3 TmpWorldHitNormal = ConvexResult.m_hitCollisionObject->getWorldTransform().getBasis() * ConvexResult.m_hitNormalLocal; 60 WorldHitNomal = BT2LOL_VEC3(TmpWorldHitNormal); 61 } 62 63 float DotUp = dot(m_up, WorldHitNomal); 64 //We hit below the accepted slope_dot, FAIL 65 if (DotUp < m_min_slope_dot) 66 return btScalar(1.f); 67 68 //Continue to next. 69 return ClosestConvexResultCallback::addSingleResult(ConvexResult, NormalInWorld); 70 } 71 protected: 72 btCollisionObject* m_me; 73 const vec3 m_up; 74 float m_min_slope_dot; 75 }; 76 77 ///BulletKinematicCharacterController is an object that supports a sliding motion in a world. 78 ///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. 79 ///Interaction between btKinematicCharacterController and dynamic rigid bodies needs to be explicity implemented by the user. 80 class BulletKinematicCharacterController : public btActionInterface 81 { 82 public: 83 BulletKinematicCharacterController(btPairCachingGhostObject* NewGhostObject, btConvexShape* NewConvexShape, float NewStepHeight, int NewUpAxis=1) 84 { 85 m_convex_shape = NewConvexShape; 86 m_i_up_axis = NewUpAxis; 87 m_ghost_object = NewGhostObject; 88 m_step_height = NewStepHeight; 89 90 m_added_margin = 0.02f; 91 m_walk_direction = vec3(.0f, .0f, .0f); 92 m_do_gobject_sweep_test = true; 93 m_turn_angle = .0f; 94 m_use_walk_direction = false; // Should remove walk direction, this doesn't work correctly. 95 m_velocity_time_interval = .0f; 96 m_vertical_velocity = .0f; 97 m_vertical_offset = .0f; 98 m_f_gravity = 9.8f * 3.f; // 3G acceleration. 99 m_fall_speed = 55.f; // Terminal velocity of a sky diver in m/s. 100 m_jump_speed = 10.f; // ? 101 m_was_on_ground = false; 102 m_was_jumping = false; 103 SetMaxSlope(45.f); 104 } 105 ~BulletKinematicCharacterController() { } 106 107 protected: 108 109 static vec3* GetUpAxisDirections() 110 { 111 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) }; 112 113 return sUpAxisDirection; 114 } 115 116 //-------------------------- 117 //CONVENIENCE FUNCTIONS 118 //-- 119 120 //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 121 vec3 GetReflectedDir(const vec3& Direction, const vec3& Normal) 122 { 123 return Direction - (2.f * dot(Direction, Normal) * Normal); 124 } 125 //Returns the portion of 'direction' that is parallel to 'normal' 126 vec3 ProjectDirOnNorm(const vec3& Direction, const vec3& Normal) 127 { 128 return Normal * dot(Direction, Normal); 129 } 130 //Returns the portion of 'Direction' that is perpindicular to 'Normal' 131 vec3 ProjectDirOnNormPerpindicular(const vec3& Direction, const vec3& Normal) 132 { 133 return Direction - ProjectDirOnNorm(Direction, Normal); 134 } 135 //Returns Ghost Object. -duh- 136 btPairCachingGhostObject* GetGhostObject() 137 { 138 return m_ghost_object; 139 } 140 141 //"Real" war functions 142 bool RecoverFromPenetration(btCollisionWorld* CollisionWorld); 143 void UpdateTargetOnHit(const vec3& hit_normal, float TangentMag = .0f, float NormalMag = 1.f); 144 void DoMove(btCollisionWorld* CollisionWorld, const vec3& MoveStep, float DeltaTime); 145 146 public: 147 ///btActionInterface interface : KEEP IN camelCase 148 virtual void updateAction(btCollisionWorld* CollisionWorld, float deltaTime) 149 { 150 PreStep(CollisionWorld); 151 PlayerStep(CollisionWorld, deltaTime); 152 } 153 154 //not in the interface, but called above 155 void PreStep(btCollisionWorld* CollisionWorld); 156 void PlayerStep(btCollisionWorld* CollisionWorld, float DeltaTime); 157 158 ///btActionInterface interface : KEEP IN camelCase 159 void debugDraw(btIDebugDraw* debugDrawer) { } 160 161 void SetUpAxis(int NewAxis) 162 { 163 if (NewAxis < 0) 164 NewAxis = 0; 165 if (NewAxis > 2) 166 NewAxis = 2; 167 m_i_up_axis = NewAxis; 168 } 169 170 //!!!!!! SHOULD DITCH THAT !!!!!! 171 //This should probably be called setPositionIncrementPerSimulatorStep. 172 //This is neither a Direction nor a velocity, but the amount to 173 //increment the position each simulation iteration, regardless 174 //of DeltaTime. 175 //This call will Reset any velocity set by SetVelocityForTimeInterval(). 176 virtual void SetWalkDirection(const vec3& walkDirection) 177 { 178 m_use_walk_direction = true; 179 m_walk_direction = walkDirection; 180 m_normalized_direction = normalize(m_walk_direction); 181 } 182 183 //Caller provides a velocity with which the character should MoveStep for 184 //the given time period. After the time period, velocity is Reset 185 //to zero. 186 //This call will Reset any walk Direction set by SetWalkDirection(). 187 //Negative time intervals will result in no motion. 188 virtual void SetVelocityForTimeInterval(const vec3& velocity, float timeInterval) 189 { 190 m_use_walk_direction = false; 191 m_walk_direction = velocity; 192 m_normalized_direction = normalize(m_walk_direction); 193 m_velocity_time_interval = timeInterval; 194 } 195 196 //Usefulness ? 197 void Reset() { } 198 void Warp(const vec3& NewOrigin) 199 { 200 btTransform NewTransform; 201 NewTransform.setIdentity(); 202 NewTransform.setOrigin(LOL2BTU_VEC3(NewOrigin)); 203 m_ghost_object->setWorldTransform(NewTransform); 204 } 205 206 //External Setup 207 //-- 208 209 void SetFallSpeed(float NewFallSpeed) { m_fall_speed = NewFallSpeed; } 210 void SetJumpSpeed(float NewJumpSpeed) { m_jump_speed = NewJumpSpeed; } 211 void SetMaxJumpHeight(float NewMaxJumpHeight) { m_max_jump_height = NewMaxJumpHeight; } 212 213 //Jump logic will go in EasyCC 214 bool CanJump() const { return OnGround(); } 215 void Jump(); 216 217 //NewGravity functions 218 void SetGravity(float NewGravity) { m_f_gravity = NewGravity; } 219 float GetGravity() const { return m_f_gravity; } 220 221 //The max slope determines the maximum angle that the controller can walk up. 222 //The slope angle is measured in radians. 223 void SetMaxSlope(float NewSlopeRadians) { m_max_slope_radians = NewSlopeRadians; m_max_slope_cosine = lol::cos(NewSlopeRadians); } 224 float GetMaxSlope() const { return m_max_slope_radians; } 225 226 void SetUseGhostSweepTest(bool UseGObjectSweepTest) { m_do_gobject_sweep_test = UseGObjectSweepTest; } 227 228 bool OnGround() const { return m_vertical_velocity == .0f && m_vertical_offset == .0f; } 229 230 private: 231 232 btPairCachingGhostObject* m_ghost_object; 233 btConvexShape* m_convex_shape; //is also in m_ghost_object, but it needs to be convex, so we store it here to avoid upcast 234 235 //keep track of the contact manifolds 236 btManifoldArray m_manifold_array; 237 238 float m_half_height; 239 float m_velocity_time_interval; 240 float m_vertical_velocity; 241 float m_vertical_offset; 242 float m_fall_speed; 243 float m_jump_speed; 244 float m_max_jump_height; 245 float m_max_slope_radians; // Slope angle that is set (used for returning the exact value) 246 float m_max_slope_cosine; // Cosine equivalent of m_max_slope_radians (calculated once when set, for optimization) 247 float m_f_gravity; 248 float m_turn_angle; 249 float m_step_height; 250 float m_added_margin;//@todo: remove this and fix the code 251 252 ///this is the desired walk Direction, set by the user 253 vec3 m_walk_direction; 254 vec3 m_normalized_direction; 255 256 //some internal variables 257 vec3 m_current_position; 258 float m_current_step_offset; 259 vec3 m_target_position; 260 261 vec3 m_touching_normal; 262 bool m_touching_contact; 263 264 bool m_was_on_ground; 265 bool m_was_jumping; 266 bool m_do_gobject_sweep_test; 267 bool m_use_walk_direction; 268 int m_i_up_axis; 269 270 //--------------------------------------------------------------------- 271 //NEW INTERNAL VARS 272 //--------------------------------------------------------------------- 273 274 //Gravity in vec3 275 vec3 m_gravity; 276 277 //Current Velocity 278 vec3 m_velocity; 279 }; 280 280 281 281 #endif // HAVE_PHYS_USE_BULLET 282 282 #endif // USE_LOL_CTRLR_CHARAC 283 283 284 284 } /* namespace phys */ 285 285 286 286 } /* namespace lol */ -
trunk/test/Physics/Include/EasyCharacterController.h
r1870 r1888 35 35 36 36 class EasyCharacterController : public EasyPhysic, 37 37 public Entity 38 38 { 39 39 40 41 40 friend class Simulation; 41 friend class EasyPhysic; 42 42 43 43 #ifdef HAVE_PHYS_USE_BULLET 44 44 45 45 public: 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 46 EasyCharacterController(WorldEntity* NewOwnerEntity) : 47 EasyPhysic(NewOwnerEntity), 48 m_pair_caching_object(NULL), 49 m_character(NULL), 50 m_step_height(.0f), 51 m_base_is_updating(false), 52 m_base_cached_movement(vec3(0.f)), 53 m_frame_cached_movement(vec3(0.f)), 54 m_walk_velocity(vec3(0.f)), 55 m_current_velocity(vec3(0.f)) 56 { 57 m_gamegroup = GAMEGROUP_EZP_CHAR_CTRLR; 58 m_up_axis = 1; 59 m_gravity = vec3(.0f, -9.81f, .0f); 60 m_walk_velocity_damping = 0.2f; 61 } 62 ~EasyCharacterController() 63 { 64 delete m_character; 65 } 66 66 67 68 69 70 71 72 67 virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false); 68 virtual void InitBodyToGhost(); 69 virtual void AddToSimulation(class Simulation* current_simulation); 70 virtual void RemoveFromSimulation(class Simulation* current_simulation); 71 virtual void SetMovementForFrame(vec3 const &MoveQuantity); 72 virtual void Jump(); 73 73 74 74 virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation); 75 75 protected: 76 77 76 virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix); 77 virtual char const *GetName(); 78 78 public: 79 79 virtual void TickGame(float seconds); 80 80 81 81 protected: 82 82 83 83 virtual btGhostObject* GetGhostObjectInstance(); 84 84 85 btPairCachingGhostObject*m_pair_caching_object;86 //btKinematicCharacterController*m_character;87 85 btPairCachingGhostObject* m_pair_caching_object; 86 //btKinematicCharacterController* m_character; 87 BulletKinematicCharacterController* m_character; 88 88 89 floatm_step_height;90 intm_up_axis;91 boolm_base_is_updating;92 vec3m_base_cached_movement;93 vec3m_frame_cached_movement;89 float m_step_height; 90 int m_up_axis; 91 bool m_base_is_updating; 92 vec3 m_base_cached_movement; 93 vec3 m_frame_cached_movement; 94 94 95 96 floatm_walk_velocity_damping;95 //---- 96 float m_walk_velocity_damping; 97 97 98 99 vec3m_gravity;98 //---- 99 vec3 m_gravity; 100 100 101 102 vec3m_walk_velocity;103 vec3m_current_velocity;101 //---- 102 vec3 m_walk_velocity; 103 vec3 m_current_velocity; 104 104 105 105 #else // NO PHYSIC IMPLEMENTATION 106 106 107 108 109 110 111 107 virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false) { } 108 virtual void InitBodyToGhost() { } 109 virtual void AddToSimulation(class Simulation* current_simulation) { } 110 virtual void RemoveFromSimulation(class Simulation* current_simulation) { } 111 virtual void SetMovementForFrame(vec3 const &MoveQuantity) { } 112 112 113 113 #endif // PHYSIC IMPLEMENTATION -
trunk/test/Physics/Include/EasyConstraint.h
r1782 r1888 32 32 { 33 33 34 35 34 friend class Simulation; 35 friend class EasyPhysic; 36 36 37 37 #ifdef HAVE_PHYS_USE_BULLET 38 38 39 39 public: 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 40 EasyConstraint() : 41 m_typed_constraint(NULL), 42 m_p2p_constraint(NULL), 43 m_hinge_constraint(NULL), 44 m_slider_constraint(NULL), 45 m_cone_twist_constraint(NULL), 46 m_6dof_constraint(NULL), 47 m_owner_simulation(NULL), 48 m_a_physobj(NULL), 49 m_b_physobj(NULL), 50 m_a_transform(lol::mat4(1.f)), 51 m_b_transform(lol::mat4(1.f)), 52 m_using_ref_a(false), 53 m_disable_a2b_collision(false) 54 55 { 56 } 57 ~EasyConstraint() 58 { 59 delete m_typed_constraint; 60 m_p2p_constraint = NULL; 61 m_hinge_constraint = NULL; 62 m_slider_constraint = NULL; 63 m_cone_twist_constraint = NULL; 64 m_6dof_constraint = NULL; 65 } 66 67 void AddToSimulation(class Simulation* current_simulation); 68 void RemoveFromSimulation(class Simulation* current_simulation); 69 69 70 70 private: 71 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 btTypedConstraint*m_typed_constraint;130 btPoint2PointConstraint*m_p2p_constraint;131 btHingeConstraint*m_hinge_constraint;132 btSliderConstraint*m_slider_constraint;133 btConeTwistConstraint*m_cone_twist_constraint;134 btGeneric6DofConstraint*m_6dof_constraint;72 //check if Init can be done 73 bool CanProceedWithInit() 74 { 75 if (!m_a_physobj || !m_b_physobj) 76 return false; 77 78 if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body) 79 return false; 80 81 return true; 82 } 83 84 //------------------------------------------------------------------------- 85 //Init constraint functions 86 //-- 87 void CustomInitConstraintToPoint2Point() 88 { 89 m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 90 LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)); 91 m_typed_constraint = m_p2p_constraint; 92 } 93 94 void CustomInitConstraintToHinge() 95 { 96 m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 97 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 98 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)), 99 m_using_ref_a); 100 m_typed_constraint = m_hinge_constraint; 101 } 102 103 void CustomInitConstraintToSlider() 104 { 105 m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 106 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 107 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)), 108 m_using_ref_a); 109 m_typed_constraint = m_slider_constraint; 110 } 111 112 void CustomInitConstraintToConeTwist() 113 { 114 m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 115 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 116 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT))); 117 m_typed_constraint = m_cone_twist_constraint; 118 } 119 120 void CustomInitConstraintTo6Dof() 121 { 122 m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 123 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 124 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)), 125 m_using_ref_a); 126 m_typed_constraint = m_6dof_constraint; 127 } 128 129 btTypedConstraint* m_typed_constraint; 130 btPoint2PointConstraint* m_p2p_constraint; 131 btHingeConstraint* m_hinge_constraint; 132 btSliderConstraint* m_slider_constraint; 133 btConeTwistConstraint* m_cone_twist_constraint; 134 btGeneric6DofConstraint* m_6dof_constraint; 135 135 136 136 #else // NO PHYSIC IMPLEMENTATION 137 137 138 138 public: 139 140 141 142 143 144 145 146 147 139 EasyConstraint() : 140 m_a_physobj(NULL), 141 m_b_physobj(NULL), 142 m_a_transform(lol::mat4(1.f)), 143 m_b_transform(lol::mat4(1.f)), 144 m_using_ref_a(false), 145 m_disable_a2b_collision(false) 146 { 147 } 148 148 149 149 private: 150 150 151 152 153 154 155 156 157 158 159 160 151 void AddToSimulation(class Simulation* current_simulation) { } 152 void RemoveFromSimulation(class Simulation* current_simulation) { } 153 154 //check if Init can be done 155 bool CanProceedWithInit() { return false; } 156 void CustomInitConstraintToPoint2Point() { } 157 void CustomInitConstraintToHinge() { } 158 void CustomInitConstraintToSlider() { } 159 void CustomInitConstraintToConeTwist() { } 160 void CustomInitConstraintTo6Dof() { } 161 161 162 162 #endif // PHYSIC IMPLEMENTATION 163 163 164 164 public: 165 void InitConstraintToPoint2Point(){ if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); }166 void InitConstraintToHinge(){ if (CanProceedWithInit()) CustomInitConstraintToHinge(); }167 void InitConstraintToSlider(){ if (CanProceedWithInit()) CustomInitConstraintToSlider(); }168 void InitConstraintToConeTwist(){ if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); }169 void InitConstraintTo6Dof(){ if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); }170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 165 void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); } 166 void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); } 167 void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); } 168 void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); } 169 void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); } 170 171 //Set given physic object to the proper slot. 172 void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); } 173 void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); } 174 void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform) 175 { 176 if (SetToB) 177 { 178 m_b_physobj = NewPhysObj; 179 m_b_transform = NewTransform; 180 } 181 else 182 { 183 m_a_physobj = NewPhysObj; 184 m_a_transform = NewTransform; 185 } 186 } 187 188 //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local). 189 void SetRefAsA(bool NewUseRefA) 190 { 191 m_using_ref_a = NewUseRefA; 192 } 193 194 //Set whether or not to disable the collision between the bodies 195 void DisableCollisionBetweenObjs(bool DisableCollision) 196 { 197 m_disable_a2b_collision = DisableCollision; 198 } 199 199 200 200 private: 201 Simulation*m_owner_simulation;202 EasyPhysic*m_a_physobj;203 EasyPhysic*m_b_physobj;204 lol::mat4m_a_transform;205 lol::mat4m_b_transform;206 boolm_using_ref_a;207 boolm_disable_a2b_collision;201 Simulation* m_owner_simulation; 202 EasyPhysic* m_a_physobj; 203 EasyPhysic* m_b_physobj; 204 lol::mat4 m_a_transform; 205 lol::mat4 m_b_transform; 206 bool m_using_ref_a; 207 bool m_disable_a2b_collision; 208 208 209 209 }; -
trunk/test/Physics/Include/EasyPhysics.h
r1819 r1888 34 34 { 35 35 36 37 36 friend class Simulation; 37 friend class EasyConstraint; 38 38 39 39 #ifdef HAVE_PHYS_USE_BULLET 40 40 41 41 public: 42 43 42 EasyPhysic(WorldEntity* NewOwnerEntity); 43 ~EasyPhysic(); 44 44 45 46 47 48 49 45 virtual void SetShapeToBox(lol::vec3& box_size); 46 virtual void SetShapeToSphere(float radius); 47 virtual void SetShapeToCone(float radius, float height); 48 virtual void SetShapeToCylinder(lol::vec3& cyl_size); 49 virtual void SetShapeToCapsule(float radius, float height); 50 50 51 52 53 51 virtual bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); } 52 virtual mat4 GetTransform(); 53 virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))); 54 54 protected: 55 55 virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix); 56 56 public: 57 58 59 60 61 57 virtual void SetMass(float mass); 58 virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false); 59 virtual void InitBodyToGhost(); 60 virtual void AddToSimulation(class Simulation* current_simulation); 61 virtual void RemoveFromSimulation(class Simulation* current_simulation); 62 62 63 63 protected: 64 65 64 virtual void SetLocalInertia(float mass); 65 virtual void SetShapeTo(btCollisionShape* collision_shape); 66 66 67 67 virtual btGhostObject* GetGhostObjectInstance(); 68 68 69 btCollisionObject*m_collision_object;69 btCollisionObject* m_collision_object; 70 70 71 btGhostObject*m_ghost_object;71 btGhostObject* m_ghost_object; 72 72 73 btRigidBody*m_rigid_body;74 btVector3m_local_inertia;73 btRigidBody* m_rigid_body; 74 btVector3 m_local_inertia; 75 75 76 btCollisionShape*m_collision_shape;77 btConvexShape*m_convex_shape;78 btMotionState*m_motion_state;76 btCollisionShape* m_collision_shape; 77 btConvexShape* m_convex_shape; 78 btMotionState* m_motion_state; 79 79 80 80 #else // NO PHYSIC IMPLEMENTATION 81 81 82 82 public: 83 83 EasyPhysic(WorldEntity* NewOwnerEntity) { m_owner_entity = NewOwnerEntity; } 84 84 85 86 87 88 89 85 virtual void SetShapeToBox(lol::vec3& BoxSize) { } 86 virtual void SetShapeToSphere(float radius) { } 87 virtual void SetShapeToCone(float radius, float height) { } 88 virtual void SetShapeToCylinder(lol::vec3& cyl_size) { } 89 virtual void SetShapeToCapsule(float radius, float height) { } 90 90 91 92 93 91 virtual bool CanChangeCollisionChannel() { return true; } 92 virtual mat4 GetTransform() { return mat4(1.0f); } 93 virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { } 94 94 private: 95 95 virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix) { } 96 96 public: 97 98 99 100 101 97 virtual void SetMass(float mass) { } 98 virtual void InitBodyToRigid() { } 99 virtual void InitBodyToGhost() { } 100 virtual void AddToSimulation(class Simulation* current_simulation) { } 101 virtual void RemoveFromSimulation(class Simulation* current_simulation) { } 102 102 103 103 virtual void InitBodyToGhost() { } 104 104 105 105 #endif // PHYSIC IMPLEMENTATION 106 106 107 107 public: 108 109 110 111 112 113 114 115 116 117 118 119 120 121 int GetCollisionMask(){ return m_collision_mask; }108 //Sets the collision Group & Mask. 109 //Mask can change at runtime, not group ! 110 virtual bool SetCollisionChannel(int NewGroup, int NewMask) 111 { 112 if (CanChangeCollisionChannel()) 113 { 114 m_collision_group = (1<<NewGroup); 115 m_collision_mask = NewMask; 116 return true; 117 } 118 return false; 119 } 120 int GetCollisionGroup() { return m_collision_group; } 121 int GetCollisionMask() { return m_collision_mask; } 122 122 123 124 125 126 127 123 //Base/Attachment logic 124 virtual void AttachTo(EasyPhysic* NewBase, bool NewBaseLockLocation = true, bool NewBaseLockRotation = true) 125 { 126 if (NewBase == this || (NewBase && NewBase->m_base_physic == this)) 127 return; 128 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 129 if (NewBase) 130 { 131 bool bAlreadyExists = false; 132 for (int i = 0; i < NewBase->m_based_physic_list.Count(); ++i) 133 if (NewBase->m_based_physic_list[i] == this) 134 bAlreadyExists = true; 135 if (!bAlreadyExists) 136 NewBase->m_based_physic_list << this; 137 m_base_physic = NewBase; 138 m_base_lock_location = NewBaseLockLocation; 139 m_base_lock_rotation = NewBaseLockRotation; 140 } 141 else if (m_base_physic) 142 { 143 for (int i = 0; i < m_base_physic->m_based_physic_list.Count(); ++i) 144 if (m_base_physic->m_based_physic_list[i] == this) 145 m_base_physic->m_based_physic_list.Remove(i--); 146 m_base_physic = NULL; 147 } 148 } 149 149 150 150 protected: 151 lol::mat4m_local_to_world;152 floatm_mass;153 intm_collision_group;154 intm_collision_mask;155 WorldEntity*m_owner_entity;156 Simulation*m_owner_simulation;151 lol::mat4 m_local_to_world; 152 float m_mass; 153 int m_collision_group; 154 int m_collision_mask; 155 WorldEntity* m_owner_entity; 156 Simulation* m_owner_simulation; 157 157 158 159 Array<EasyPhysic*> m_based_physic_list;//List of objects based on this : this object moves, its based object MoveStep with it.160 EasyPhysic* m_base_physic;//Base for this object : The base moves, the object moves with it.161 bool m_base_lock_location;//when this is TRUE, location moves with rotation change.162 bool m_base_lock_rotation;//when this is TRUE, rotation moves with rotation change.158 //Base/Attachment logic 159 Array<EasyPhysic*> m_based_physic_list; //List of objects based on this : this object moves, its based object MoveStep with it. 160 EasyPhysic* m_base_physic; //Base for this object : The base moves, the object moves with it. 161 bool m_base_lock_location; //when this is TRUE, location moves with rotation change. 162 bool m_base_lock_rotation; //when this is TRUE, rotation moves with rotation change. 163 163 164 165 Array<EasyPhysic*> m_touching_physic;//Maintained by ghost objects164 //Touch logic 165 Array<EasyPhysic*> m_touching_physic; //Maintained by ghost objects 166 166 }; 167 167 -
trunk/test/Physics/Include/LolBtPhysicsIntegration.h
r1785 r1888 20 20 namespace lol 21 21 { 22 23 24 25 #define GAMEGROUP_ENT_INPUT 26 #define GAMEGROUP_ENT_PLATFORM 27 #define GAMEGROUP_ENT_MAIN 28 #define GAMEGROUP_EZP_CHAR_CTRLR 29 #define GAMEGROUP_SIMULATION 22 //Override Gamegroups names for Physic-useage 23 //"_ENT_" means that this is a group for Entities that use EasyPhysic primitives. 24 //"_EZP_" means that this is a group for EasyPhysic primitives. 25 #define GAMEGROUP_ENT_INPUT GAMEGROUP_BEFORE 26 #define GAMEGROUP_ENT_PLATFORM GAMEGROUP_DEFAULT 27 #define GAMEGROUP_ENT_MAIN GAMEGROUP_AFTER 28 #define GAMEGROUP_EZP_CHAR_CTRLR GAMEGROUP_AFTER_0 29 #define GAMEGROUP_SIMULATION GAMEGROUP_AFTER_1 30 30 31 31 #ifdef HAVE_PHYS_USE_BULLET 32 32 33 #define LOL2BT_UNIT 34 #define BT2LOL_UNIT 33 #define LOL2BT_UNIT 1.0f 34 #define BT2LOL_UNIT 1.0f 35 35 36 #define LOL2BT_SIZE 37 #define BT2LOL_SIZE 36 #define LOL2BT_SIZE 0.5f 37 #define BT2LOL_SIZE 2.0f 38 38 39 #define LOL2BT_VEC3(ELEMENT) 40 #define BT2LOL_VEC3(ELEMENT) 39 #define LOL2BT_VEC3(ELEMENT) btVector3((ELEMENT).x, (ELEMENT).y, (ELEMENT).z) 40 #define BT2LOL_VEC3(ELEMENT) (*(lol::vec3*)(&(ELEMENT))) 41 41 42 42 //Same as above with Unit taken into account 43 #define LOL2BTU_VEC3(ELEMENT) 44 #define BT2LOLU_VEC3(ELEMENT) 43 #define LOL2BTU_VEC3(ELEMENT) btVector3((ELEMENT).x * LOL2BT_UNIT, (ELEMENT).y * LOL2BT_UNIT, (ELEMENT).z * LOL2BT_UNIT) 44 #define BT2LOLU_VEC3(ELEMENT) (*(lol::vec3*)(&(ELEMENT))) * BT2LOL_UNIT 45 45 46 #define LOL2BT_QUAT(ELEMENT) 47 #define BT2LOL_QUAT(ELEMENT) 46 #define LOL2BT_QUAT(ELEMENT) btQuaternion((ELEMENT).x, (ELEMENT).y, (ELEMENT).z, (ELEMENT).w) 47 #define BT2LOL_QUAT(ELEMENT) lol::quat((ELEMENT).getW(), BT2LOL_VEC3((ELEMENT).getAxis()) 48 48 49 49 #endif // HAVE_PHYS_USE_BULLET -
trunk/test/Physics/Include/LolPhysics.h
r1819 r1888 27 27 enum eRaycastType 28 28 { 29 30 31 32 33 29 ERT_Closest, 30 ERT_AllHit, 31 ERT_AnyHit, //Will stop at the first hit. Hit data are supposed to be irrelevant 32 33 ERT_MAX 34 34 }; 35 35 36 36 struct RayCastResult 37 37 { 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 Array<EasyPhysic*>m_collider_list;54 Array<vec3>m_hit_normal_list;55 Array<vec3>m_hit_point_list;56 Array<float>m_hit_fraction_list;57 58 short intm_collision_filter_group;59 short intm_collision_filter_mask;60 unsigned intm_flags; //???38 RayCastResult(int CollisionFilterGroup=1, int CollisionFilterMask=(0xFF)) 39 { 40 memset(this, 0, sizeof(RayCastResult)); 41 42 m_collision_filter_group = CollisionFilterGroup; 43 m_collision_filter_mask = CollisionFilterMask; 44 } 45 void Reset() 46 { 47 m_collider_list.Empty(); 48 m_hit_normal_list.Empty(); 49 m_hit_point_list.Empty(); 50 m_hit_fraction_list.Empty(); 51 } 52 53 Array<EasyPhysic*> m_collider_list; 54 Array<vec3> m_hit_normal_list; 55 Array<vec3> m_hit_point_list; 56 Array<float> m_hit_fraction_list; 57 58 short int m_collision_filter_group; 59 short int m_collision_filter_mask; 60 unsigned int m_flags; //??? 61 61 }; 62 62 … … 64 64 { 65 65 public: 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 66 Simulation() : 67 m_broadphase(0), 68 m_collision_configuration(0), 69 m_dispatcher(0), 70 m_solver(0), 71 m_dynamics_world(0), 72 m_timestep(1.f/60.f) 73 { 74 m_gamegroup = GAMEGROUP_SIMULATION; 75 } 76 ~Simulation() 77 { 78 Exit(); 79 } 80 81 char const *GetName() { return "<Simulation>"; } 82 82 83 83 #ifdef HAVE_PHYS_USE_BULLET 84 84 public: 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 HitResult.m_collider_list<< (EasyPhysic*)BtRayResult_Closest->m_collisionObject->getUserPointer();201 HitResult.m_hit_normal_list<< BT2LOLU_VEC3(BtRayResult_Closest->m_hitNormalWorld);202 HitResult.m_hit_point_list<< BT2LOLU_VEC3(BtRayResult_Closest->m_hitPointWorld);203 HitResult.m_hit_fraction_list<< BtRayResult_Closest->m_closestHitFraction;204 205 206 207 208 209 210 HitResult.m_collider_list<< (EasyPhysic*)BtRayResult_AllHits->m_collisionObjects[i]->getUserPointer();211 HitResult.m_hit_normal_list<< BT2LOLU_VEC3(BtRayResult_AllHits->m_hitNormalWorld[i]);212 HitResult.m_hit_point_list<< BT2LOLU_VEC3(BtRayResult_AllHits->m_hitPointWorld[i]);213 HitResult.m_hit_fraction_list<< BtRayResult_AllHits->m_hitFractions[i];214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 85 void Init() 86 { 87 // Build the broadphase 88 if (1) 89 { 90 m_Sweep_broadphase = new btAxisSweep3(LOL2BT_VEC3(m_world_min), LOL2BT_VEC3(m_world_max)); 91 m_Sweep_broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback()); 92 m_broadphase = m_Sweep_broadphase; 93 } 94 else 95 m_broadphase = new btDbvtBroadphase(); 96 97 // Set up the collision configuration and dispatcher 98 m_collision_configuration = new btDefaultCollisionConfiguration(); 99 m_dispatcher = new btCollisionDispatcher(m_collision_configuration); 100 101 // The actual physics solver 102 m_solver = new btSequentialImpulseConstraintSolver; 103 104 // The world. 105 m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration); 106 } 107 108 virtual void TickGame(float seconds) 109 { 110 Entity::TickGame(seconds); 111 112 //step the simulation 113 if (m_dynamics_world) 114 { 115 //the "+1" is to have at least one Timestep and to ensure float to int .5f conversion. 116 int steps = (int)(seconds / m_timestep) + 1; 117 m_dynamics_world->stepSimulation(seconds, steps, m_timestep); 118 } 119 } 120 121 //Rip-Off of the btKinematicClosestNotMeRayResultCallback 122 class ClosestNotMeRayResultCallback : public btCollisionWorld::ClosestRayResultCallback 123 { 124 public: 125 ClosestNotMeRayResultCallback(btCollisionObject* Me, const btVector3& rayFromWorld, const btVector3& rayToWorld) : 126 btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld) 127 { 128 m_me = Me; 129 } 130 131 virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace) 132 { 133 if (rayResult.m_collisionObject == m_me) 134 return 1.0; 135 136 return ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace); 137 } 138 protected: 139 btCollisionObject* m_me; 140 }; 141 142 //Will stop at the first hit. Hit data are supposed to be irrelevant 143 class AnyHitRayResultCallback : public btCollisionWorld::ClosestRayResultCallback 144 { 145 public: 146 AnyHitRayResultCallback(const btVector3& rayFromWorld, const btVector3& rayToWorld) : 147 btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld) 148 { 149 } 150 151 virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace) 152 { 153 return .0f; 154 } 155 }; 156 157 //Returns true when hitting something. If SourceCaster is set, it will be ignored by Raycast. 158 bool RayHits(RayCastResult& HitResult, eRaycastType RaycastType, const vec3& RayFrom, const vec3& RayTo, EasyPhysic* SourceCaster=NULL) 159 { 160 bool bResult = false; 161 162 btCollisionWorld::RayResultCallback* BtRayResult = NULL; 163 btCollisionWorld::ClosestRayResultCallback* BtRayResult_Closest; 164 btCollisionWorld::AllHitsRayResultCallback* BtRayResult_AllHits; 165 166 switch (RaycastType) 167 { 168 case ERT_Closest: 169 { 170 if (SourceCaster) 171 BtRayResult_Closest = new ClosestNotMeRayResultCallback(SourceCaster->m_collision_object, LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo)); 172 else 173 BtRayResult_Closest = new btCollisionWorld::ClosestRayResultCallback(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo)); 174 BtRayResult = BtRayResult_Closest; 175 break; 176 } 177 case ERT_AllHit: 178 { 179 BtRayResult_AllHits = new btCollisionWorld::AllHitsRayResultCallback(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo)); 180 BtRayResult = BtRayResult_AllHits; 181 break; 182 } 183 case ERT_AnyHit: 184 { 185 BtRayResult_Closest = new AnyHitRayResultCallback(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo)); 186 BtRayResult = BtRayResult_Closest; 187 break; 188 } 189 } 190 191 m_dynamics_world->rayTest(LOL2BTU_VEC3(RayFrom), LOL2BTU_VEC3(RayTo), *BtRayResult); 192 if (BtRayResult->hasHit()) 193 { 194 bResult = true; 195 196 switch (RaycastType) 197 { 198 case ERT_Closest: 199 { 200 HitResult.m_collider_list << (EasyPhysic*)BtRayResult_Closest->m_collisionObject->getUserPointer(); 201 HitResult.m_hit_normal_list << BT2LOLU_VEC3(BtRayResult_Closest->m_hitNormalWorld); 202 HitResult.m_hit_point_list << BT2LOLU_VEC3(BtRayResult_Closest->m_hitPointWorld); 203 HitResult.m_hit_fraction_list << BtRayResult_Closest->m_closestHitFraction; 204 break; 205 } 206 case ERT_AllHit: 207 { 208 for (int i = 0; i < BtRayResult_AllHits->m_collisionObjects.size(); i++) 209 { 210 HitResult.m_collider_list << (EasyPhysic*)BtRayResult_AllHits->m_collisionObjects[i]->getUserPointer(); 211 HitResult.m_hit_normal_list << BT2LOLU_VEC3(BtRayResult_AllHits->m_hitNormalWorld[i]); 212 HitResult.m_hit_point_list << BT2LOLU_VEC3(BtRayResult_AllHits->m_hitPointWorld[i]); 213 HitResult.m_hit_fraction_list << BtRayResult_AllHits->m_hitFractions[i]; 214 } 215 break; 216 } 217 } 218 } 219 220 delete BtRayResult; 221 222 return bResult; 223 } 224 225 226 void Exit() 227 { 228 delete m_dynamics_world; 229 delete m_solver; 230 delete m_dispatcher; 231 delete m_collision_configuration; 232 delete m_broadphase; 233 } 234 235 btDiscreteDynamicsWorld* GetWorld() 236 { 237 return m_dynamics_world; 238 } 239 239 240 240 private: 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 btBroadphaseInterface*m_broadphase;261 btAxisSweep3*m_Sweep_broadphase;262 263 btDefaultCollisionConfiguration*m_collision_configuration;264 btCollisionDispatcher*m_dispatcher;265 266 btSequentialImpulseConstraintSolver*m_solver;267 268 btDiscreteDynamicsWorld*m_dynamics_world;241 void CustomSetContinuousDetection(bool ShouldUseCCD) 242 { 243 if (m_dynamics_world) 244 m_dynamics_world->getDispatchInfo().m_useContinuous = ShouldUseCCD; 245 } 246 247 void CustomSetGravity(vec3 &NewGravity) 248 { 249 if (m_dynamics_world) 250 m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity * LOL2BT_UNIT)); 251 } 252 253 void CustomSetWorldLimit(vec3 const &NewWorldMin, vec3 const &NewWorldMax) 254 { 255 } 256 257 void CustomSetTimestep(float NewTimestep) { } 258 259 //broadphase 260 btBroadphaseInterface* m_broadphase; 261 btAxisSweep3* m_Sweep_broadphase; 262 // Set up the collision configuration and dispatc 263 btDefaultCollisionConfiguration* m_collision_configuration; 264 btCollisionDispatcher* m_dispatcher; 265 // The actual physics solver 266 btSequentialImpulseConstraintSolver* m_solver; 267 // The world. 268 btDiscreteDynamicsWorld* m_dynamics_world; 269 269 270 270 #else // NO PHYSIC IMPLEMENTATION 271 271 272 272 public: 273 274 275 276 273 void Init() { } 274 void TickGame(float seconds) { } 275 bool RayHits(RayCastResult& HitResult, eRaycastType RaycastType, const vec3& RayFrom, const vec3& RayTo, EasyPhysic* SourceCaster=NULL) { return false; } 276 void Exit() { } 277 277 private: 278 279 280 281 278 void CustomSetContinuousDetection(bool ShouldUseCCD) { } 279 void CustomSetGravity(vec3 &NewGravity) { } 280 void CustomSetWorldLimit(vec3 &NewWorldMin, vec3 &NewWorldMax) { } 281 void CustomSetTimestep(float NewTimestep) { } 282 282 283 283 #endif // PHYSIC IMPLEMENTATION 284 284 285 285 public: 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 286 //Main logic : 287 //The Set*() functions do the all-lib-independent data storage. 288 //And then it calls the CustomSet*() which are the specialized versions. 289 290 //Sets the continuous collision detection flag. 291 void SetContinuousDetection(bool ShouldUseCCD) 292 { 293 m_using_CCD = ShouldUseCCD; 294 CustomSetContinuousDetection(ShouldUseCCD); 295 } 296 297 //Sets the simulation gravity. 298 void SetGravity(vec3 &NewGravity) 299 { 300 m_gravity = NewGravity; 301 CustomSetGravity(NewGravity); 302 } 303 304 //Sets the simulation gravity. 305 void SetWorldLimit(vec3 const &NewWorldMin, vec3 const &NewWorldMax) 306 { 307 m_world_min = NewWorldMin; 308 m_world_max = NewWorldMax; 309 CustomSetWorldLimit(NewWorldMin, NewWorldMax); 310 } 311 312 //Sets the simulation fixed timestep. 313 void SetTimestep(float NewTimestep) 314 { 315 if (NewTimestep > .0f) 316 { 317 m_timestep = NewTimestep; 318 CustomSetTimestep(NewTimestep); 319 } 320 } 321 321 322 322 private: 323 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 Array<EasyPhysic*>m_dynamic_list;417 Array<EasyPhysic*>m_static_list;418 Array<EasyPhysic*>m_ghost_list;419 Array<EasyPhysic*>m_collision_object_list;420 Array<EasyPhysic*>m_character_controller_list;421 Array<EasyConstraint*>m_constraint_list;422 423 424 floatm_timestep;425 boolm_using_CCD;426 vec3m_gravity;427 vec3m_world_min;428 vec3m_world_max;324 friend class EasyPhysic; 325 friend class EasyCharacterController; 326 friend class EasyConstraint; 327 328 enum eEasyPhysicType 329 { 330 EEPT_Dynamic, 331 EEPT_Static, 332 EEPT_Ghost, 333 EEPT_CollisionObject, 334 EEPT_CharacterController, 335 336 EEPT_MAX 337 }; 338 339 //m_owner_simulation 340 //Adds the given EasyPhysic to the correct list. 341 void ObjectRegistration(bool AddObject, EasyPhysic* NewEP, eEasyPhysicType CurType) 342 { 343 Array<EasyPhysic*>* SearchList = NULL; 344 switch(CurType) 345 { 346 case EEPT_Dynamic: 347 { 348 SearchList = &m_dynamic_list; 349 break; 350 } 351 case EEPT_Static: 352 { 353 SearchList = &m_static_list; 354 break; 355 } 356 case EEPT_Ghost: 357 { 358 SearchList = &m_ghost_list; 359 break; 360 } 361 case EEPT_CollisionObject: 362 { 363 SearchList = &m_collision_object_list; 364 break; 365 } 366 case EEPT_CharacterController: 367 { 368 SearchList = &m_character_controller_list; 369 break; 370 } 371 } 372 373 if (AddObject) 374 { 375 NewEP->m_owner_simulation = this; 376 (*SearchList) << NewEP; 377 } 378 else 379 { 380 NewEP->m_owner_simulation = NULL; 381 for (int i = 0; i < SearchList->Count(); ++i) 382 { 383 if ((*SearchList)[i] == NewEP) 384 { 385 SearchList->Remove(i--); 386 break; 387 } 388 } 389 } 390 } 391 void ObjectRegistration(bool AddObject, EasyConstraint* NewEC) 392 { 393 Array<EasyConstraint*>* SearchList = NULL; 394 SearchList = &m_constraint_list; 395 396 if (AddObject) 397 { 398 NewEC->m_owner_simulation = this; 399 (*SearchList) << NewEC; 400 } 401 else 402 { 403 NewEC->m_owner_simulation = NULL; 404 for (int i = 0; i < SearchList->Count(); ++i) 405 { 406 if ((*SearchList)[i] == NewEC) 407 { 408 SearchList->Remove(i--); 409 break; 410 } 411 } 412 } 413 } 414 415 //Easy Physics body List 416 Array<EasyPhysic*> m_dynamic_list; 417 Array<EasyPhysic*> m_static_list; 418 Array<EasyPhysic*> m_ghost_list; 419 Array<EasyPhysic*> m_collision_object_list; 420 Array<EasyPhysic*> m_character_controller_list; 421 Array<EasyConstraint*> m_constraint_list; 422 423 //Easy Physics data storage 424 float m_timestep; 425 bool m_using_CCD; 426 vec3 m_gravity; 427 vec3 m_world_min; 428 vec3 m_world_max; 429 429 }; 430 430
Note: See TracChangeset
for help on using the changeset viewer.