Changeset 1633
- Timestamp:
- Jul 15, 2012, 6:07:12 PM (9 years ago)
- Location:
- trunk/test
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/BtPhysTest.cpp
r1630 r1633 101 101 } 102 102 103 for (int x=0; x < 6; x++)104 { 105 for (int y=0; y < 6; y++)106 { 107 for (int z=0; z < 5; z++)103 if (1) 104 { 105 for (int x=0; x < 6; x++) 106 { 107 for (int y=0; y < 6; y++) 108 108 { 109 PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f, 110 vec3(-20.f, 15.f, -20.f) + 111 vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z)); 112 m_physobj_list << new_physobj; 113 Ticker::Ref(new_physobj); 109 for (int z=0; z < 5; z++) 110 { 111 PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f, 112 vec3(-20.f, 15.f, -20.f) + 113 vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z)); 114 m_physobj_list << new_physobj; 115 Ticker::Ref(new_physobj); 116 } 117 } 118 } 119 } 120 121 if (1) 122 { 123 Array<PhysicsObject*> RopeElements; 124 for (int i = 0; i < 14; i++) 125 { 126 PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f, 127 vec3(0.f, 15.f, -20.f) + 128 vec3(0.f, 0.f, 2.f * (float)i), 1); 129 RopeElements << new_physobj; 130 m_physobj_list << new_physobj; 131 Ticker::Ref(new_physobj); 132 if (RopeElements.Count() > 1) 133 { 134 EasyConstraint* new_constraint = new EasyConstraint(); 135 136 vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform().v3.xyz - 137 RopeElements[i - 1]->GetPhysic()->GetTransform().v3.xyz); 138 new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B)); 139 new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B)); 140 new_constraint->InitConstraintToPoint2Point(); 141 new_constraint->DisableCollisionBetweenObjs(true); 142 new_constraint->AddToSimulation(m_simulation); 143 m_constraint_list << new_constraint; 114 144 } 115 145 } … … 299 329 } 300 330 301 for (int i = 0; i < m_ground_list.Count(); i++) 302 { 303 PhysicsObject* PhysObj = m_ground_list[i]; 304 305 mat4 GroundMat = PhysObj->GetTransform(); 306 mat4 CenterMx = mat4::translate(GroundBarycenter); 307 GroundMat = inverse(CenterMx) * GroundMat; 308 GroundMat = CenterMx * 309 mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds)) 310 * GroundMat; 311 PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat)); 331 if (1) 332 { 333 for (int i = 0; i < m_ground_list.Count(); i++) 334 { 335 PhysicsObject* PhysObj = m_ground_list[i]; 336 337 mat4 GroundMat = PhysObj->GetTransform(); 338 mat4 CenterMx = mat4::translate(GroundBarycenter); 339 GroundMat = inverse(CenterMx) * GroundMat; 340 GroundMat = CenterMx * 341 mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds)) 342 * GroundMat; 343 PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat)); 344 } 312 345 } 313 346 … … 404 437 { 405 438 Ticker::Unref(m_camera); 439 440 while (m_constraint_list.Count()) 441 { 442 EasyConstraint* CurPop = m_constraint_list.Last(); 443 m_constraint_list.Pop(); 444 CurPop->RemoveFromSimulation(m_simulation); 445 delete CurPop; 446 } 406 447 while (m_ground_list.Count()) 407 448 { 408 449 PhysicsObject* CurPop = m_ground_list.Last(); 409 450 m_ground_list.Pop(); 451 CurPop->GetPhysic()->RemoveFromSimulation(m_simulation); 410 452 Ticker::Unref(CurPop); 411 453 } … … 414 456 PhysicsObject* CurPop = m_physobj_list.Last(); 415 457 m_physobj_list.Pop(); 458 CurPop->GetPhysic()->RemoveFromSimulation(m_simulation); 416 459 Ticker::Unref(CurPop); 417 460 } -
trunk/test/BtPhysTest.h
r1607 r1633 26 26 27 27 lol::phys::Simulation* m_simulation; 28 Array<EasyConstraint*> m_constraint_list; 28 29 Array<PhysicsObject*> m_physobj_list; 29 30 Array<PhysicsObject*> m_ground_list; -
trunk/test/PhysicObject.h
r1632 r1633 35 35 } 36 36 37 PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location )37 PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1) 38 38 : m_ready(false), m_should_render(true) 39 39 { … … 83 83 MeshRand << "[sc#aad scb#aad acap3 2 1]"; 84 84 85 int RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1)); 85 switch (RandValue) 86 { 87 case 0: 88 { 89 RandValue = (int)(lol::RandF() * (SphereLimit - 1)); 90 break; 91 } 92 case 1: 93 { 94 RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1)); 95 break; 96 } 97 case 2: 98 { 99 RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1)); 100 break; 101 } 102 case 3: 103 { 104 RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1)); 105 break; 106 } 107 case 4: 108 { 109 RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1)); 110 break; 111 } 112 default: 113 { 114 RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1)); 115 } 116 } 86 117 87 118 m_mesh.Compile(MeshRand[RandValue]); … … 114 145 } 115 146 116 m_physics.SetCollisionChannel(ColGroup, (1<<ColGroup)|(1)); 147 m_physics.SetCollisionChannel(0, 0xFF); 148 //m_physics.SetCollisionChannel(ColGroup, (1<<ColGroup)|(1)); 117 149 m_physics.SetMass(base_mass); 118 150 m_physics.SetTransform(base_location); … … 136 168 } 137 169 170 EasyMesh *GetMesh() { return &m_mesh; } 171 EasyPhysic *GetPhysic() { return &m_physics; } 172 138 173 ~PhysicsObject() 139 174 { -
trunk/test/Physics/EasyPhysics.cpp
r1632 r1633 11 11 // 12 12 13 //14 // The EasyMesh class15 // ------------------16 //17 18 13 #if defined HAVE_CONFIG_H 19 14 # include "config.h" … … 30 25 31 26 #ifdef HAVE_PHYS_USE_BULLET 27 28 //------------------------------------------------------------------------- 29 //EASY_PHYSIC 30 //-- 32 31 33 32 EasyPhysic::EasyPhysic() : … … 109 108 { 110 109 if (m_motion_state) 111 m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location )));110 m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT))); 112 111 else 113 m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location )));112 m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT))); 114 113 } 115 114 … … 157 156 { 158 157 btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); 159 if (m_rigid_body) 160 { 161 dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask); 162 if (m_mass != .0f) 163 current_simulation->AddToDynamic(this); 158 if (dynamics_world) 159 { 160 if (m_rigid_body) 161 { 162 dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask); 163 if (m_mass != .0f) 164 current_simulation->AddToDynamic(this); 165 else 166 current_simulation->AddToStatic(this); 167 } 164 168 else 165 current_simulation->AddToStatic(this); 166 } 167 else 168 dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask); 169 dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask); 170 } 171 } 172 173 void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation) 174 { 175 btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); 176 if (dynamics_world) 177 { 178 if (m_rigid_body) 179 dynamics_world->removeRigidBody(m_rigid_body); 180 else 181 dynamics_world->removeCollisionObject(m_collision_object); 182 } 169 183 } 170 184 … … 196 210 } 197 211 212 //------------------------------------------------------------------------- 213 //EASY_CONSTRAINT 214 //-- 215 216 void EasyConstraint::AddToSimulation(class Simulation* current_simulation) 217 { 218 btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); 219 if (dynamics_world && m_typed_constraint) 220 { 221 dynamics_world->addConstraint(m_typed_constraint, m_disable_a2b_collision); 222 current_simulation->AddToConstraint(this); 223 } 224 } 225 226 void EasyConstraint::RemoveFromSimulation(class Simulation* current_simulation) 227 { 228 btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); 229 if (dynamics_world, m_typed_constraint) 230 dynamics_world->removeConstraint(m_typed_constraint); 231 } 232 198 233 #endif // HAVE_PHYS_USE_BULLET 199 234 -
trunk/test/Physics/EasyPhysics.h
r1632 r1633 33 33 { 34 34 35 friend class EasyConstraint; 36 35 37 #ifdef HAVE_PHYS_USE_BULLET 36 38 … … 50 52 void InitBodyToRigid(bool ZeroMassIsKinematic=false); 51 53 void AddToSimulation(class Simulation* current_simulation); 54 void RemoveFromSimulation(class Simulation* current_simulation); 52 55 mat4 GetTransform(); 53 56 … … 80 83 void InitBodyToRigid() { } 81 84 void AddToSimulation(class Simulation* current_simulation) { } 85 void RemoveFromSimulation(class Simulation* current_simulation) { } 82 86 mat4 GetTransform() { return mat4(1.0f); } 83 87 … … 109 113 class EasyConstraint 110 114 { 111 EasyConstraint()112 {113 //btPoint2PointConstraint(bA, bB, PivotA, PivotB)114 //btHingeConstraint(bA, bB, TransfA, TransfB, UseRefA)115 //btSliderConstraint(bA, bB, TransfA, TransfB, UseRefA)116 //btConeTwistConstraint(bA, bB, TransfA, TransfB)117 //btGeneric6DofConstraint(bA, bB, TransfA, TransfB, UseRefA)118 }119 120 115 #ifdef HAVE_PHYS_USE_BULLET 121 116 117 public: 118 EasyConstraint() : 119 m_typed_constraint(NULL), 120 m_p2p_constraint(NULL), 121 m_hinge_constraint(NULL), 122 m_slider_constraint(NULL), 123 m_cone_twist_constraint(NULL), 124 m_6dof_constraint(NULL), 125 m_a_physobj(NULL), 126 m_b_physobj(NULL), 127 m_a_transform(lol::mat4(1.f)), 128 m_b_transform(lol::mat4(1.f)), 129 m_using_ref_a(false), 130 m_disable_a2b_collision(false) 131 132 { 133 } 134 ~EasyConstraint() 135 { 136 delete m_typed_constraint; 137 m_p2p_constraint = NULL; 138 m_hinge_constraint = NULL; 139 m_slider_constraint = NULL; 140 m_cone_twist_constraint = NULL; 141 m_6dof_constraint = NULL; 142 } 143 144 void AddToSimulation(class Simulation* current_simulation); 145 void RemoveFromSimulation(class Simulation* current_simulation); 146 147 private: 148 149 //check if Init can be done 150 bool CanProceedWithInit() 151 { 152 if (!m_a_physobj || !m_b_physobj) 153 return false; 154 155 if (!m_a_physobj->m_rigid_body || !m_b_physobj->m_rigid_body) 156 return false; 157 158 return true; 159 } 160 161 //------------------------------------------------------------------------- 162 //Init constraint functions 163 //-- 164 void CustomInitConstraintToPoint2Point() 165 { 166 m_p2p_constraint = new btPoint2PointConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 167 LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)); 168 m_typed_constraint = m_p2p_constraint; 169 } 170 171 void CustomInitConstraintToHinge() 172 { 173 m_hinge_constraint = new btHingeConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 174 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 175 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)), 176 m_using_ref_a); 177 m_typed_constraint = m_hinge_constraint; 178 } 179 180 void CustomInitConstraintToSlider() 181 { 182 m_slider_constraint = new btSliderConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 183 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 184 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)), 185 m_using_ref_a); 186 m_typed_constraint = m_slider_constraint; 187 } 188 189 void CustomInitConstraintToConeTwist() 190 { 191 m_cone_twist_constraint = new btConeTwistConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 192 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 193 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT))); 194 m_typed_constraint = m_cone_twist_constraint; 195 } 196 197 void CustomInitConstraintTo6Dof() 198 { 199 m_6dof_constraint = new btGeneric6DofConstraint(*m_a_physobj->m_rigid_body, *m_b_physobj->m_rigid_body, 200 btTransform(LOL2BT_QUAT(quat(m_a_transform)), LOL2BT_VEC3(m_a_transform.v3.xyz * LOL2BT_UNIT)), 201 btTransform(LOL2BT_QUAT(quat(m_b_transform)), LOL2BT_VEC3(m_b_transform.v3.xyz * LOL2BT_UNIT)), 202 m_using_ref_a); 203 m_typed_constraint = m_6dof_constraint; 204 } 205 122 206 btTypedConstraint* m_typed_constraint; 207 btPoint2PointConstraint* m_p2p_constraint; 208 btHingeConstraint* m_hinge_constraint; 209 btSliderConstraint* m_slider_constraint; 210 btConeTwistConstraint* m_cone_twist_constraint; 211 btGeneric6DofConstraint* m_6dof_constraint; 123 212 124 213 #else // NO PHYSIC IMPLEMENTATION 125 214 215 public: 216 EasyConstraint() : 217 m_a_physobj(NULL), 218 m_b_physobj(NULL), 219 m_a_transform(lol::mat4(1.f)), 220 m_b_transform(lol::mat4(1.f)), 221 m_using_ref_a(false), 222 m_disable_a2b_collision(false) 223 { 224 } 225 226 private: 227 228 void AddToSimulation(class Simulation* current_simulation) { } 229 void RemoveFromSimulation(class Simulation* current_simulation) { } 230 231 //check if Init can be done 232 bool CanProceedWithInit() { return false; } 233 void CustomInitConstraintToPoint2Point() { } 234 void CustomInitConstraintToHinge() { } 235 void CustomInitConstraintToSlider() { } 236 void CustomInitConstraintToConeTwist() { } 237 void CustomInitConstraintTo6Dof() { } 238 126 239 #endif // PHYSIC IMPLEMENTATION 127 240 241 public: 242 void InitConstraintToPoint2Point() { if (CanProceedWithInit()) CustomInitConstraintToPoint2Point(); } 243 void InitConstraintToHinge() { if (CanProceedWithInit()) CustomInitConstraintToHinge(); } 244 void InitConstraintToSlider() { if (CanProceedWithInit()) CustomInitConstraintToSlider(); } 245 void InitConstraintToConeTwist() { if (CanProceedWithInit()) CustomInitConstraintToConeTwist(); } 246 void InitConstraintTo6Dof() { if (CanProceedWithInit()) CustomInitConstraintTo6Dof(); } 247 248 //Set given physic object to the proper slot. 249 void SetPhysObjA(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(false, NewPhysObj, NewTransform); } 250 void SetPhysObjB(EasyPhysic* NewPhysObj, lol::mat4 NewTransform) { SetPhysObj(true, NewPhysObj, NewTransform); } 251 void SetPhysObj(bool SetToB, EasyPhysic* NewPhysObj, lol::mat4 NewTransform) 252 { 253 if (SetToB) 254 { 255 m_b_physobj = NewPhysObj; 256 m_b_transform = NewTransform; 257 } 258 else 259 { 260 m_a_physobj = NewPhysObj; 261 m_a_transform = NewTransform; 262 } 263 } 264 265 //Set whether or not the physic engine should use the A object as the reference (most constraint transform are local). 266 void SetRefAsA(bool NewUseRefA) 267 { 268 m_using_ref_a = NewUseRefA; 269 } 270 271 //Set whether or not to disable the collision between the bodies 272 void DisableCollisionBetweenObjs(bool DisableCollision) 273 { 274 m_disable_a2b_collision = DisableCollision; 275 } 276 277 private: 278 EasyPhysic* m_a_physobj; 279 EasyPhysic* m_b_physobj; 280 lol::mat4 m_a_transform; 281 lol::mat4 m_b_transform; 282 bool m_using_ref_a; 283 bool m_disable_a2b_collision; 284 128 285 }; 129 286
Note: See TracChangeset
for help on using the changeset viewer.