Changeset 3352
- Timestamp:
- 02/01/10 15:59:41 (7 weeks ago)
- Location:
- plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src
- Files:
-
- 3 modified
-
PhysicsActor.cpp (modified) (7 diffs)
-
PhysicsActor.hpp (modified) (5 diffs)
-
PhysicsService.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src/PhysicsActor.cpp
r3349 r3352 59 59 m_name = ""; 60 60 m_MotionState = NULL; 61 m_mass = 0.0f; 61 62 } 62 63 … … 79 80 { 80 81 m_CollisionShape = _pCollisionShape; 82 m_mass = 0.0f; 81 83 attachBody(m_CollisionShape); 82 84 } … … 126 128 // TreeCollision (not impl yet), UserMesh (heightfield) 127 129 128 129 130 m_pActor = NewtonCreateBody(dynamic_cast<PhysicsZone*>(m_pZone.get())->getZonePtr(), dynamic_cast<CollisionShape*>(_collision.get())->getShapePtr());131 NewtonReleaseCollision(dynamic_cast<PhysicsZone*>(m_pZone.get())->getZonePtr(), dynamic_cast<CollisionShape*>(_collision.get())->getShapePtr());132 133 130 // set the transform call back function 134 m_MotionState = new PhysicsActor::ZenMotionState(this); 135 131 m_MotionState = new MotionState(this); 132 //mass, motionstate, collisionshape, and inertia are all necessary prior to creation for bullet 133 btRigidBody::btRigidBodyConstructionInfo rbInfo(m_mass,m_MotionState,dynamic_cast<CollisionShape*>(_collision.get())->getShapePtr(),btVector3(0.0f,0.0f,0.0f)); 134 135 m_pActor = new btRigidBody(rbInfo); 136 136 m_pActor->setUserPointer(this); 137 137 m_pActor->setCollisionFlags(m_pActor->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); 138 138 return true; 139 139 } … … 204 204 PhysicsActor::getMass() 205 205 { 206 Zen::Math::Real mass; 207 Zen::Math::Real Ixx; 208 Zen::Math::Real Iyy; 209 Zen::Math::Real Izz; 210 btRigidBodyGetMassMatrix(m_pActor, &mass, &Ixx, &Iyy, &Izz); 211 212 return mass; 213 } 214 215 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 206 return m_mass; 207 } 208 209 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 210 //this gets a bit arcane since we're dealing with bit-flags. see http://www.cprogramming.com/tutorial/bitwise_operators.html for reference - bjr 216 211 void 217 212 PhysicsActor::SetStatic(bool _isStatic = false) … … 221 216 { 222 217 setMass(0); 218 m_pActor->setCollisionFlags(m_pActor->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT); 223 219 } 224 220 else 221 { 225 222 setMass(m_mass); 223 m_pActor->setCollisionFlags(m_pActor->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT); 224 } 226 225 } 227 226 … … 245 244 PhysicsActor::setMass(float _mass) 246 245 { 247 // set the mass to zero to make this a static body. 248 // set the mass to any positive value to make this a dynamic body. 249 Zen::Math::Real Ixx = _mass * m_scaleX * m_scaleX; 250 Zen::Math::Real Iyy = _mass * m_scaleY * m_scaleY; 251 Zen::Math::Real Izz = _mass * m_scaleZ * m_scaleZ; 252 btRigidBodySetMassMatrix(m_pActor, _mass, Ixx, Iyy, Izz); 246 m_mass = _mass; 253 247 } 254 248 … … 425 419 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 426 420 void 427 PhysicsActor:: ZenMotionState::getWorldTransform(btTransform& _worldTrans) const428 { 429 430 } 431 432 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 433 void 434 PhysicsActor:: ZenMotionState::setWorldTransform(const btTransform& _worldTrans)421 PhysicsActor::MotionState::getWorldTransform(btTransform& _worldTrans) const 422 { 423 //don't belive we really need anything here since were updating visuals on the set end... might need to crosscheck later -bjr 424 } 425 426 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 427 void 428 PhysicsActor::MotionState::setWorldTransform(const btTransform& _worldTrans) 435 429 { 436 430 if (m_userPointer == NULL) return; -
plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src/PhysicsActor.hpp
r3349 r3352 154 154 pOwningObject_type m_pOwner; 155 155 pCollisionShape_type m_CollisionShape; 156 157 156 /// @} 158 157 … … 357 356 //The btMotionState interface class allows the dynamics world to synchronize and interpolate the updated world transforms with graphics 358 357 //For optimizations, potentially only moving objects get synchronized (using setWorldPosition/setWorldOrientation) 359 class ZenMotionState : public btMotionState358 class MotionState : public btMotionState 360 359 { 361 360 public: 362 ZenMotionState(void* _userData);363 virtual ~ ZenMotionState();361 MotionState(void* _userData); 362 virtual ~MotionState(); 364 363 virtual void getWorldTransform(btTransform& _worldTrans) const; 365 364 //Bullet only calls the update of worldtransform for active objects … … 369 368 /// @name Extended Member Variables 370 369 /// @{ 371 ZenMotionState * m_MotionState;370 MotionState * m_MotionState; 372 371 /// @} 373 372 }; // class PhysicsActor … … 383 382 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 384 383 inline 385 PhysicsActor:: ZenMotionState::ZenMotionState(void* _userData)384 PhysicsActor::MotionState::MotionState(void* _userData) 386 385 : m_userPointer(_userData) 387 386 { … … 390 389 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 391 390 inline 392 PhysicsActor:: ZenMotionState::~ZenMotionState()391 PhysicsActor::MotionState::~MotionState() 393 392 { 394 393 -
plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src/PhysicsService.cpp
r3347 r3352 37 37 namespace ZBullet { 38 38 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 39 40 // See CustomMaterialCombinerCallback in Bullet/Demos/ConcaveDemo/ConcavePhysicsDemo.cpp for an example implementation. 41 ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback; 42 inline btScalar calculateCombinedFriction(float friction0,float friction1) 43 { 44 btScalar friction = friction0 * friction1; 45 46 const btScalar MAX_FRICTION = 10.f; 47 if (friction < -MAX_FRICTION) 48 friction = -MAX_FRICTION; 49 if (friction > MAX_FRICTION) 50 friction = MAX_FRICTION; 51 return friction; 52 53 } 54 55 inline btScalar calculateCombinedRestitution(float restitution0,float restitution1) 56 { 57 return restitution0 * restitution1; 58 } 59 60 static bool CustomMaterialCombinerCallback(btManifoldPoint& cp, const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1) 61 { 62 //point of contaact, object, face, child shape, face2, child shape2. see also ConvexDecompositionDemo MyContactCallback that ships with the bullet library 63 64 float friction0 = colObj0->getFriction(); 65 float friction1 = colObj1->getFriction(); 66 float restitution0 = colObj0->getRestitution(); 67 float restitution1 = colObj1->getRestitution(); 68 69 if (colObj0->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) 70 { 71 friction0 = 1.0;//partId0,index0 72 restitution0 = 0.f; 73 } 74 if (colObj1->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) 75 { 76 if (index1&1) 77 { 78 friction1 = 1.0f;//partId1,index1 79 } else 80 { 81 friction1 = 0.f; 82 } 83 restitution1 = 0.f; 84 } 85 86 cp.m_combinedFriction = calculateCombinedFriction(friction0,friction1); 87 cp.m_combinedRestitution = calculateCombinedRestitution(restitution0,restitution1); 88 89 //this return value is currently ignored, but to be on the safe side: return false if you don't calculate friction 90 return true; 91 } 92 39 93 PhysicsService::PhysicsService() 40 94 { 41 95 //gContactAddedCallback usage can be found under btManifoldResult::addContactPoint 42 96 //it is an explicitly defined global method handle called during btManifoldResult::addContactPoint 43 gContactAddedCallback = Zen::ZBullet::CustomMaterialCombinerCallback;97 gContactAddedCallback = CustomMaterialCombinerCallback; 44 98 } 45 99 … … 107 161 } 108 162 109 // See CustomMaterialCombinerCallback in Bullet/Demos/ConcaveDemo/ConcavePhysicsDemo.cpp for an example implementation.110 ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback;111 inline btScalar calculateCombinedFriction(float friction0,float friction1)112 {113 btScalar friction = friction0 * friction1;114 115 const btScalar MAX_FRICTION = 10.f;116 if (friction < -MAX_FRICTION)117 friction = -MAX_FRICTION;118 if (friction > MAX_FRICTION)119 friction = MAX_FRICTION;120 return friction;121 122 }123 124 inline btScalar calculateCombinedRestitution(float restitution0,float restitution1)125 {126 return restitution0 * restitution1;127 }128 129 static bool CustomMaterialCombinerCallback(btManifoldPoint& cp, const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1)130 {131 //point of contaact, object, face, child shape, face2, child shape2. see also ConvexDecompositionDemo MyContactCallback that ships with the bullet library132 133 float friction0 = colObj0->getFriction();134 float friction1 = colObj1->getFriction();135 float restitution0 = colObj0->getRestitution();136 float restitution1 = colObj1->getRestitution();137 138 if (colObj0->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)139 {140 friction0 = 1.0;//partId0,index0141 restitution0 = 0.f;142 }143 if (colObj1->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)144 {145 if (index1&1)146 {147 friction1 = 1.0f;//partId1,index1148 } else149 {150 friction1 = 0.f;151 }152 restitution1 = 0.f;153 }154 155 cp.m_combinedFriction = calculateCombinedFriction(friction0,friction1);156 cp.m_combinedRestitution = calculateCombinedRestitution(restitution0,restitution1);157 158 //this return value is currently ignored, but to be on the safe side: return false if you don't calculate friction159 return true;160 }161 162 163 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 163 164 } // namespace ZBullet
