BGE patch: Optimization of bullet adaptation layer - part 1.
authorBenoit Bolsee <benoit.bolsee@online.be>
Mon, 21 Jul 2008 12:37:27 +0000 (12:37 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Mon, 21 Jul 2008 12:37:27 +0000 (12:37 +0000)
First batch of optimizaton of the bullet adaptation layer in the BGE.
- remove circular motion state update.
- optimization of physic adaptation layer for bullet: bypass
  unecessary conversion of rotation matrix to quaternion and back.
- remove double updates during object replication.

12 files changed:
source/gameengine/Ketsji/KX_BulletPhysicsController.cpp
source/gameengine/Ketsji/KX_BulletPhysicsController.h
source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_IPhysicsController.h
source/gameengine/Ketsji/KX_OdePhysicsController.cpp
source/gameengine/Ketsji/KX_OdePhysicsController.h
source/gameengine/Ketsji/KX_RadarSensor.cpp
source/gameengine/Ketsji/KX_Scene.cpp
source/gameengine/Ketsji/KX_SumoPhysicsController.cpp
source/gameengine/Ketsji/KX_SumoPhysicsController.h
source/gameengine/Physics/Bullet/CcdPhysicsController.cpp
source/gameengine/Physics/Bullet/CcdPhysicsController.h

index 1553c4c61c2d62fc6acda463e85f1879aa7a11cb..c7b2a671f78b7e9dae03c191ed5ec7b2d3d28bb9 100644 (file)
@@ -133,9 +133,10 @@ void       KX_BulletPhysicsController::getOrientation(MT_Quaternion& orn)
        CcdPhysicsController::getOrientation(myorn[0],myorn[1],myorn[2],myorn[3]);
        orn = MT_Quaternion(myorn[0],myorn[1],myorn[2],myorn[3]);
 }
-void KX_BulletPhysicsController::setOrientation(const MT_Quaternion& orn)
+void KX_BulletPhysicsController::setOrientation(const MT_Matrix3x3& orn)
 {
-       CcdPhysicsController::setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
+       btMatrix3x3 btmat(orn[0][0], orn[0][1], orn[1][2], orn[1][0], orn[1][1], orn[1][2], orn[2][0], orn[2][1], orn[2][2]);
+       CcdPhysicsController::setWorldOrientation(btmat);
 }
 void KX_BulletPhysicsController::setPosition(const MT_Point3& pos)
 {
index 93ee03660db94c140b273697e9e168bf306a1854..2efe0474b30589196032edd8b11f9b734fdf8423 100644 (file)
@@ -35,7 +35,7 @@ public:
        virtual void    SetAngularVelocity(const MT_Vector3& ang_vel,bool local);
        virtual void    SetLinearVelocity(const MT_Vector3& lin_vel,bool local);
        virtual void    getOrientation(MT_Quaternion& orn);
-       virtual void setOrientation(const MT_Quaternion& orn);
+       virtual void setOrientation(const MT_Matrix3x3& orn);
        virtual void setPosition(const MT_Point3& pos);
        virtual void setScaling(const MT_Vector3& scaling);
        virtual MT_Scalar       GetMass();
index 6d0b7219ed17130713626f801ba1327a70136d0c..d40fa1e24469daa53af3d5b7f95935f78feb813f 100644 (file)
@@ -744,7 +744,7 @@ void KX_GameObject::NodeSetLocalOrientation(const MT_Matrix3x3& rot)
        if (m_pPhysicsController1 && (!GetSGNode() || !GetSGNode()->GetSGParent()))
        {
                // see note above
-               m_pPhysicsController1->setOrientation(rot.getRotation());
+               m_pPhysicsController1->setOrientation(rot);
        }
        if (GetSGNode())
                GetSGNode()->SetLocalOrientation(rot);
index bc4cc185a0462d46ab29e97365a9edc42a49ea8d..ecfdb8c4275867cf1203041762c73bb06b504f3c 100644 (file)
@@ -71,7 +71,8 @@ public:
        virtual void    resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ) = 0;
 
        virtual void    getOrientation(MT_Quaternion& orn)=0;
-       virtual void setOrientation(const MT_Quaternion& orn)=0;
+       virtual void setOrientation(const MT_Matrix3x3& orn)=0;
+       //virtual       void setOrientation(const MT_Quaternion& orn)=0;
        virtual void setPosition(const MT_Point3& pos)=0;
        virtual void setScaling(const MT_Vector3& scaling)=0;
        virtual MT_Scalar       GetMass()=0;
index 8b0a6dafc22074d9a645ad8e4ebb87ea3417fb9c..05feb11a2bcb17b6f49f1137e6b4b1cb9fde6ed8 100644 (file)
@@ -133,8 +133,9 @@ void        KX_OdePhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool l
        ODEPhysicsController::SetLinearVelocity(lin_vel[0],lin_vel[1],lin_vel[2],local);
 }
 
-void KX_OdePhysicsController::setOrientation(const MT_Quaternion& orn)
+void KX_OdePhysicsController::setOrientation(const MT_Matrix3x3& rot)
 {
+       MT_Quaternion orn = rot.getRotation();
        ODEPhysicsController::setOrientation(orn[0],orn[1],orn[2],orn[3]);
 }
 
index c96c71c81f9a8a3bc05ea9ba50eab36dc84f1537..18f9edc68355a909369bc93be2abc4377ce36daa 100644 (file)
@@ -67,7 +67,7 @@ public:
        virtual void    SetLinearVelocity(const MT_Vector3& lin_vel,bool local);
        virtual void            resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ);
        virtual void            getOrientation(MT_Quaternion& orn);
-       virtual void setOrientation(const MT_Quaternion& orn);
+       virtual void setOrientation(const MT_Matrix3x3& orn);
        virtual void setPosition(const MT_Point3& pos);
        virtual void setScaling(const MT_Vector3& scaling);
        virtual MT_Scalar       GetMass();
index bf2ba18f4905adf6979ddd386421ff1d889585dc..de4979ac4c951ef1565be9d4bb56721eecf0d004 100644 (file)
@@ -176,8 +176,10 @@ void KX_RadarSensor::SynchronizeTransform()
 
        if (m_physCtrl)
        {
-               m_physCtrl->setPosition(trans.getOrigin().x(),trans.getOrigin().y(),trans.getOrigin().z());
-               m_physCtrl->setOrientation(trans.getRotation().x(),trans.getRotation().y(),trans.getRotation().z(),trans.getRotation().w());
+               MT_Quaternion orn = trans.getRotation();
+               MT_Point3 pos = trans.getOrigin();
+               m_physCtrl->setPosition(pos[0],pos[1],pos[2]);
+               m_physCtrl->setOrientation(orn[0],orn[1],orn[2],orn[3]);
                m_physCtrl->calcXform();
        }
 
index 1bdd400280586880e131b993bfbab2977d4e3599..c374b9d6fd158ea2d5a7ac743d24f693c715c6ec 100644 (file)
@@ -712,8 +712,12 @@ void KX_Scene::DupliGroupRecurse(CValue* obj, int level)
 
                if (replica->GetPhysicsController())
                {
-                       replica->GetPhysicsController()->setPosition(newpos);
-                       replica->GetPhysicsController()->setOrientation(newori.getRotation());
+                       // not required, already done in NodeSetLocalOrientation..
+                       //replica->GetPhysicsController()->setPosition(newpos);
+                       //replica->GetPhysicsController()->setOrientation(newori.getRotation());
+                       // Scaling has been set relatively hereabove, this does not 
+                       // set the scaling of the controller. I don't know why it's just the
+                       // relative scale and not the full scale that has to be put here...
                        replica->GetPhysicsController()->setScaling(newscale);
                }
 
@@ -843,8 +847,9 @@ SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
 
        if (replica->GetPhysicsController())
        {
-               replica->GetPhysicsController()->setPosition(newpos);
-               replica->GetPhysicsController()->setOrientation(newori.getRotation());
+               // not needed, already done in NodeSetLocalPosition()
+               //replica->GetPhysicsController()->setPosition(newpos);
+               //replica->GetPhysicsController()->setOrientation(newori.getRotation());
                replica->GetPhysicsController()->setScaling(newscale);
        }
 
index ffb078b346e379167bb4790383ce4c9afd3add24..4032a795ce3892f4a7fed2c97618712f630d7dca 100644 (file)
@@ -170,8 +170,9 @@ void        KX_SumoPhysicsController::setMargin(float collisionMargin)
 }
 
 
-void KX_SumoPhysicsController::setOrientation(const MT_Quaternion& orn)
+void KX_SumoPhysicsController::setOrientation(const MT_Matrix3x3& rot)
 {
+       MT_Quaternion orn = rot.getRotation();
        SumoPhysicsController::setOrientation(
                orn[0],orn[1],orn[2],orn[3]);
 
index 33666036c0d83a4a1fa6a317b4cbc12176177de4..1dd930bf3d971e2b07ea4cf3351abad2315a0ba8 100644 (file)
@@ -79,7 +79,7 @@ public:
        void    SuspendDynamics(bool);
        void    RestoreDynamics();
        virtual void    getOrientation(MT_Quaternion& orn);
-       virtual void setOrientation(const MT_Quaternion& orn);
+       virtual void setOrientation(const MT_Matrix3x3& orn);
        
        virtual void setPosition(const MT_Point3& pos);
        virtual void setScaling(const MT_Vector3& scaling);
index e444c4c73be1c18e28a1d855c38fcab640432b0b..5c70b07166172627d8ee95afe7943e737d0b46c1 100644 (file)
@@ -337,12 +337,33 @@ void              CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float
                {
                        m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
                }
-
-               m_MotionState->setWorldOrientation(quatImag0,quatImag1,quatImag2,quatReal);
+               // not required
+               //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);
+               // not required
+               //m_bulletMotionState->setWorldTransform(xform);
+       }
+
+}
+
+void CcdPhysicsController::setWorldOrientation(const btMatrix3x3& orn)
+{
+       if (m_body)
+       {
+               m_body->activate(true);
+               if (m_body->isStaticObject())
+               {
+                       m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
+               }
+               // not required
+               //m_MotionState->setWorldOrientation(quatImag0,quatImag1,quatImag2,quatReal);
+               btTransform xform  = m_body->getCenterOfMassTransform();
+               xform.setBasis(orn);
+               m_body->setCenterOfMassTransform(xform);
+               // not required
+               //m_bulletMotionState->setWorldTransform(xform);
        }
 
 }
@@ -356,12 +377,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);
+               // not required, this function is only used to update the physic controller
+               //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);
+               // not required
+               //m_bulletMotionState->setWorldTransform(xform);
        }
 
 
index abb71956f5a3cd029c04d29ef232c4a454603bb1..64f1876e19945af3e152e47388aa816ac4d8891a 100644 (file)
@@ -116,6 +116,9 @@ class CcdPhysicsController : public PHY_IPhysicsController
 
        void CreateRigidbody();
 
+       protected:
+               void setWorldOrientation(const btMatrix3x3& mat);
+
        public:
        
                int                             m_collisionDelay;