bugfix: adding static objects will 'replace' previously added static object in game...
[blender-staging.git] / source / gameengine / Physics / Bullet / CcdPhysicsController.cpp
index e542c3365591baddd8fe331b7280f5b6d69a9c91..5a45ce020cce3cc8e940f416abba587d966b594f 100644 (file)
@@ -99,19 +99,26 @@ public:
                m_blenderMotionState->setWorldPosition(worldTrans.getOrigin().getX(),worldTrans.getOrigin().getY(),worldTrans.getOrigin().getZ());
                btQuaternion rotQuat = worldTrans.getRotation();
                m_blenderMotionState->setWorldOrientation(rotQuat[0],rotQuat[1],rotQuat[2],rotQuat[3]);
-
+               m_blenderMotionState->calculateWorldTransformations();
        }
 
 };
 
+
 void CcdPhysicsController::CreateRigidbody()
 {
 
-       btTransform trans = GetTransformFromMotionState(m_cci.m_MotionState);
+       btTransform trans = GetTransformFromMotionState(m_MotionState);
 
-       m_bulletMotionState = new BlenderBulletMotionState(m_cci.m_MotionState);
+       m_bulletMotionState = new BlenderBulletMotionState(m_MotionState);
+
+       m_body = new btRigidBody(m_cci.m_mass,
+               m_bulletMotionState,
+               m_cci.m_collisionShape,
+               m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor,
+               m_cci.m_linearDamping,m_cci.m_angularDamping,
+               m_cci.m_friction,m_cci.m_restitution);
 
-       m_body = new btRigidBody(m_cci.m_mass,m_bulletMotionState,m_cci.m_collisionShape,m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor,m_cci.m_friction,m_cci.m_restitution);
        
 
        //
@@ -120,7 +127,8 @@ void CcdPhysicsController::CreateRigidbody()
        
        //setMassProps this also sets collisionFlags
        //convert collision flags!
-//     m_body->m_collisionFlags = m_cci.m_collisionFlags;
+
+       m_body->setCollisionFlags(m_body->getCollisionFlags() | m_cci.m_collisionFlags);
        
        m_body->setGravity( m_cci.m_gravity);
        m_body->setDamping(m_cci.m_linearDamping, m_cci.m_angularDamping);
@@ -165,7 +173,7 @@ bool                CcdPhysicsController::SynchronizeMotionStates(float time)
                btVector3 worldPos;
                btQuaternion worldquat;
 
-               m_MotionState->getWorldPosition(worldPos[0],worldPos[1],worldPos[2]);
+/*             m_MotionState->getWorldPosition(worldPos[0],worldPos[1],worldPos[2]);
                m_MotionState->getWorldOrientation(worldquat[0],worldquat[1],worldquat[2],worldquat[3]);
                btTransform oldTrans = m_body->getCenterOfMassTransform();
                btTransform newTrans(worldquat,worldPos);
@@ -174,7 +182,7 @@ bool                CcdPhysicsController::SynchronizeMotionStates(float time)
                //need to keep track of previous position for friction effects...
                
                m_MotionState->calculateWorldTransformations();
-
+*/
                float scale[3];
                m_MotionState->getWorldScaling(scale[0],scale[1],scale[2]);
                btVector3 scaling(scale[0],scale[1],scale[2]);
@@ -245,7 +253,12 @@ void               CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dloc
 {
        if (m_body)
        {
-               m_body->activate();
+               m_body->activate(true);
+               if (m_body->isStaticObject())
+               {
+                       m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
+               }
+
 
                btVector3 dloc(dlocX,dlocY,dlocZ);
                btTransform xform = m_body->getCenterOfMassTransform();
@@ -265,7 +278,11 @@ void               CcdPhysicsController::RelativeRotate(const float rotval[9],bool local)
 {
        if (m_body )
        {
-               m_body->activate();
+               m_body->activate(true);
+               if (m_body->isStaticObject())
+               {
+                       m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
+               }
 
                btMatrix3x3 drotmat(    rotval[0],rotval[1],rotval[2],
                                                                rotval[4],rotval[5],rotval[6],
@@ -303,21 +320,34 @@ void              CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,flo
 }
 void           CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
 {
-       m_body->activate();
+       m_body->activate(true);
+       if (m_body->isStaticObject())
+       {
+               m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
+       }
+
        m_MotionState->setWorldOrientation(quatImag0,quatImag1,quatImag2,quatReal);
        btTransform xform  = m_body->getCenterOfMassTransform();
        xform.setRotation(btQuaternion(quatImag0,quatImag1,quatImag2,quatReal));
        m_body->setCenterOfMassTransform(xform);
+       m_bulletMotionState->setWorldTransform(xform);
 
 }
 
 void           CcdPhysicsController::setPosition(float posX,float posY,float posZ)
 {
-       m_body->activate();
+       m_body->activate(true);
+       if (m_body->isStaticObject())
+       {
+               m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
+       }
+       
        m_MotionState->setWorldPosition(posX,posY,posZ);
        btTransform xform  = m_body->getCenterOfMassTransform();
        xform.setOrigin(btVector3(posX,posY,posZ));
        m_body->setCenterOfMassTransform(xform);
+       m_bulletMotionState->setWorldTransform(xform);
+
 
 }
 void           CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
@@ -343,8 +373,14 @@ void               CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
                if (m_body && m_body->getCollisionShape())
                {
                        m_body->getCollisionShape()->setLocalScaling(m_cci.m_scaling);
-                       m_body->getCollisionShape()->calculateLocalInertia(m_cci.m_mass, m_cci.m_localInertiaTensor);
-                       m_body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor);
+                       
+                       //printf("no inertia recalc for fixed objects with mass=0\n");
+                       if (m_cci.m_mass)
+                       {
+                               m_body->getCollisionShape()->calculateLocalInertia(m_cci.m_mass, m_cci.m_localInertiaTensor);
+                               m_body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor);
+                       } 
+                       
                }
        }
 }
@@ -354,6 +390,10 @@ void               CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torque
 {
        btVector3 torque(torqueX,torqueY,torqueZ);
        btTransform xform = m_body->getCenterOfMassTransform();
+       if (torque.length2() > (SIMD_EPSILON*SIMD_EPSILON))
+       {
+               m_body->activate();
+       }
        if (local)
        {
                torque  = xform.getBasis()*torque;
@@ -364,6 +404,13 @@ void               CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torque
 void           CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
 {
        btVector3 force(forceX,forceY,forceZ);
+       
+       if (force.length2() > (SIMD_EPSILON*SIMD_EPSILON))
+       {
+               m_body->activate();
+       }
+
+
        btTransform xform = m_body->getCenterOfMassTransform();
        if (local)
        {
@@ -376,7 +423,7 @@ void                CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,flo
        btVector3 angvel(ang_velX,ang_velY,ang_velZ);
        if (angvel.length2() > (SIMD_EPSILON*SIMD_EPSILON))
        {
-               m_body->activate();
+               m_body->activate(true);
        }
 
        {
@@ -396,7 +443,7 @@ void                CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,floa
        btVector3 linVel(lin_velX,lin_velY,lin_velZ);
        if (linVel.length2() > (SIMD_EPSILON*SIMD_EPSILON))
        {
-               m_body->activate();
+               m_body->activate(true);
        }
        
        {