Changeset 3352

Show
Ignore:
Timestamp:
02/01/10 15:59:41 (7 weeks ago)
Author:
Azaezel
Message:

initialization and static setting expansion, ZenMotionState? becomes MotionState?, fixed method definition order for CustomMaterialCombinerCallback? in PhysicsService?

Location:
plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src/PhysicsActor.cpp

    r3349 r3352  
    5959    m_name = ""; 
    6060    m_MotionState = NULL; 
     61    m_mass = 0.0f; 
    6162} 
    6263 
     
    7980{ 
    8081    m_CollisionShape = _pCollisionShape; 
     82    m_mass = 0.0f; 
    8183    attachBody(m_CollisionShape); 
    8284} 
     
    126128    //   TreeCollision (not impl yet), UserMesh (heightfield) 
    127129 
    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  
    133130    // 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); 
    136136    m_pActor->setUserPointer(this); 
    137  
     137    m_pActor->setCollisionFlags(m_pActor->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); 
    138138    return true; 
    139139} 
     
    204204PhysicsActor::getMass() 
    205205{ 
    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 
    216211void  
    217212PhysicsActor::SetStatic(bool _isStatic = false) 
     
    221216        { 
    222217                setMass(0); 
     218      m_pActor->setCollisionFlags(m_pActor->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT); 
    223219        } 
    224220        else 
     221   { 
    225222                setMass(m_mass); 
     223      m_pActor->setCollisionFlags(m_pActor->getCollisionFlags() & ~btCollisionObject::CF_STATIC_OBJECT); 
     224   } 
    226225} 
    227226 
     
    245244PhysicsActor::setMass(float _mass) 
    246245{ 
    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; 
    253247} 
    254248 
     
    425419//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    426420void 
    427 PhysicsActor::ZenMotionState::getWorldTransform(btTransform& _worldTrans) const 
    428 { 
    429  
    430 } 
    431  
    432 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    433 void 
    434 PhysicsActor::ZenMotionState::setWorldTransform(const btTransform& _worldTrans) 
     421PhysicsActor::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//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     427void 
     428PhysicsActor::MotionState::setWorldTransform(const btTransform& _worldTrans) 
    435429{ 
    436430    if (m_userPointer == NULL) return; 
  • plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src/PhysicsActor.hpp

    r3349 r3352  
    154154         pOwningObject_type                      m_pOwner; 
    155155    pCollisionShape_type                    m_CollisionShape; 
    156  
    157156    /// @} 
    158157 
     
    357356        //The btMotionState interface class allows the dynamics world to synchronize and interpolate the updated world transforms with graphics 
    358357        //For optimizations, potentially only moving objects get synchronized (using setWorldPosition/setWorldOrientation) 
    359         class ZenMotionState : public btMotionState 
     358        class MotionState : public btMotionState 
    360359        { 
    361360        public: 
    362             ZenMotionState(void* _userData); 
    363             virtual ~ZenMotionState(); 
     361            MotionState(void* _userData); 
     362            virtual ~MotionState(); 
    364363            virtual void getWorldTransform(btTransform& _worldTrans) const; 
    365364            //Bullet only calls the update of worldtransform for active objects 
     
    369368    /// @name Extended Member Variables 
    370369    /// @{ 
    371     ZenMotionState * m_MotionState; 
     370    MotionState * m_MotionState; 
    372371    /// @} 
    373372};  // class PhysicsActor 
     
    383382//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    384383inline 
    385 PhysicsActor::ZenMotionState::ZenMotionState(void* _userData) 
     384PhysicsActor::MotionState::MotionState(void* _userData) 
    386385: m_userPointer(_userData) 
    387386{ 
     
    390389//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    391390inline 
    392 PhysicsActor::ZenMotionState::~ZenMotionState() 
     391PhysicsActor::MotionState::~MotionState() 
    393392{ 
    394393 
  • plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBullet/src/PhysicsService.cpp

    r3347 r3352  
    3737namespace ZBullet { 
    3838//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     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; 
     42inline 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 
     55inline btScalar calculateCombinedRestitution(float restitution0,float restitution1) 
     56{ 
     57        return restitution0 * restitution1; 
     58} 
     59 
     60static 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 
    3993PhysicsService::PhysicsService() 
    4094{ 
    4195    //gContactAddedCallback usage can be found under btManifoldResult::addContactPoint 
    4296    //it is an explicitly defined global method handle called during btManifoldResult::addContactPoint 
    43     gContactAddedCallback = Zen::ZBullet::CustomMaterialCombinerCallback; 
     97    gContactAddedCallback = CustomMaterialCombinerCallback; 
    4498} 
    4599 
     
    107161} 
    108162 
    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 library 
    132  
    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,index0 
    141                 restitution0 = 0.f; 
    142         } 
    143         if (colObj1->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) 
    144         { 
    145                 if (index1&1) 
    146                 { 
    147                         friction1 = 1.0f;//partId1,index1 
    148                 } else 
    149                 { 
    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 friction 
    159         return true; 
    160 } 
    161  
    162163//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    163164}   // namespace ZBullet