bugfix: adding static objects will 'replace' previously added static object in game...
[blender-staging.git] / source / gameengine / Physics / Bullet / CcdPhysicsController.cpp
index 31cea0be9e1cc31bd5dfa37757a276b89a9ff26b..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);
@@ -322,6 +330,7 @@ void                CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float
        btTransform xform  = m_body->getCenterOfMassTransform();
        xform.setRotation(btQuaternion(quatImag0,quatImag1,quatImag2,quatReal));
        m_body->setCenterOfMassTransform(xform);
+       m_bulletMotionState->setWorldTransform(xform);
 
 }
 
@@ -332,10 +341,13 @@ void              CcdPhysicsController::setPosition(float posX,float posY,float posZ)
        {
                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)
@@ -361,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);
+                       } 
+                       
                }
        }
 }
@@ -372,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;
@@ -382,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)
        {