synched with trunk at revision 32129
[blender.git] / source / gameengine / Ketsji / KX_BulletPhysicsController.cpp
index 300a7906e811a7134925b6412bd8289c3d03253f..1b2ceae6560ec2630f56fbed3ddc2a2a1f5036e1 100644 (file)
@@ -17,8 +17,8 @@
 #include "BulletSoftBody/btSoftBody.h"
 
 
-KX_BulletPhysicsController::KX_BulletPhysicsController (const CcdConstructionInfo& ci, bool dyna, bool compound)
-: KX_IPhysicsController(dyna,compound,(PHY_IPhysicsController*)this),
+KX_BulletPhysicsController::KX_BulletPhysicsController (const CcdConstructionInfo& ci, bool dyna, bool sensor, bool compound)
+: KX_IPhysicsController(dyna,sensor,compound,(PHY_IPhysicsController*)this),
 CcdPhysicsController(ci),
 m_savedCollisionFlags(0),
 m_savedCollisionFilterGroup(0),
@@ -92,7 +92,13 @@ void KX_BulletPhysicsController::SetObject (SG_IObject* object)
        gameobj->SetPhysicsController(this,gameobj->IsDynamic());
        CcdPhysicsController::setNewClientInfo(gameobj->getClientInfo());
 
-
+       if (m_bSensor)
+       {
+               // use a different callback function for sensor object, 
+               // bullet will not synchronize, we must do it explicitely
+               SG_Callbacks& callbacks = gameobj->GetSGNode()->GetCallBackFunctions();
+               callbacks.m_updatefunc = KX_GameObject::SynchronizeTransformFunc;
+       } 
 }
 
 MT_Scalar KX_BulletPhysicsController::GetRadius()
@@ -174,6 +180,20 @@ void KX_BulletPhysicsController::setScaling(const MT_Vector3& scaling)
 {
        CcdPhysicsController::setScaling(scaling.x(),scaling.y(),scaling.z());
 }
+void KX_BulletPhysicsController::SetTransform()
+{
+       btVector3 pos;
+       btVector3 scale;
+       float ori[12];
+       m_MotionState->getWorldPosition(pos.m_floats[0],pos.m_floats[1],pos.m_floats[2]);
+       m_MotionState->getWorldScaling(scale.m_floats[0],scale.m_floats[1],scale.m_floats[2]);
+       m_MotionState->getWorldOrientation(ori);
+       btMatrix3x3 rot(ori[0], ori[4], ori[8],
+                                       ori[1], ori[5], ori[9],
+                                       ori[2], ori[6], ori[10]);
+       CcdPhysicsController::forceWorldTransform(rot, pos);
+}
+
 MT_Scalar      KX_BulletPhysicsController::GetMass()
 {
        if (GetSoftBody())
@@ -259,13 +279,15 @@ void    KX_BulletPhysicsController::AddCompoundChild(KX_IPhysicsController* chil
        // we will need this to make sure that we remove the right proxy later when unparenting
        proxyShapeInfo->m_userData = childCtrl;
        proxyShapeInfo->SetProxy(childCtrl->GetShapeInfo()->AddRef());
-       // add to parent compound shapeinfo
+       // add to parent compound shapeinfo (increments ref count)
        GetShapeInfo()->AddShape(proxyShapeInfo);
        // create new bullet collision shape from the object shapeinfo and set scaling
-       btCollisionShape* newChildShape = proxyShapeInfo->CreateBulletShape();
+       btCollisionShape* newChildShape = proxyShapeInfo->CreateBulletShape(childCtrl->GetMargin(), childCtrl->getConstructionInfo().m_bGimpact, true);
        newChildShape->setLocalScaling(relativeScale);
        // add bullet collision shape to parent compound collision shape
        compoundShape->addChildShape(proxyShapeInfo->m_childTrans,newChildShape);
+       // proxyShapeInfo is not needed anymore, release it
+       proxyShapeInfo->Release();
        // remember we created this shape
        childCtrl->m_bulletChildShape = newChildShape;
        // recompute inertia of parent
@@ -359,7 +381,7 @@ void KX_BulletPhysicsController::SetMass(MT_Scalar newmass)
 void   KX_BulletPhysicsController::SuspendDynamics(bool ghost)
 {
        btRigidBody *body = GetRigidBody();
-       if (body && !m_suspended)
+       if (body && !m_suspended && !IsSensor())
        {
                btBroadphaseProxy* handle = body->getBroadphaseHandle();
                m_savedCollisionFlags = body->getCollisionFlags();
@@ -382,6 +404,8 @@ void        KX_BulletPhysicsController::RestoreDynamics()
        btRigidBody *body = GetRigidBody();
        if (body && m_suspended)
        {
+               // before make sure any position change that was done in this logic frame are accounted for
+               SetTransform();
                GetPhysicsEnvironment()->updateCcdPhysicsController(this, 
                        m_savedMass,
                        m_savedCollisionFlags,
@@ -445,7 +469,7 @@ SG_Controller*      KX_BulletPhysicsController::GetReplica(class SG_Node* destnode)
 void   KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
 {
 
-       if (!m_bDyna)
+       if (!m_bDyna && !m_bSensor)
        {
                btCollisionObject* object = GetRigidBody();
                object->setActivationState(ACTIVE_TAG);