Bullet physics: fixed some accuracy problem (square length should be compared to...
authorErwin Coumans <blender@erwincoumans.com>
Mon, 17 Apr 2006 06:27:57 +0000 (06:27 +0000)
committerErwin Coumans <blender@erwincoumans.com>
Mon, 17 Apr 2006 06:27:57 +0000 (06:27 +0000)
14 files changed:
extern/bullet/Bullet/CollisionDispatch/CollisionDispatcher.cpp
extern/bullet/Bullet/CollisionDispatch/CollisionDispatcher.h
extern/bullet/Bullet/CollisionDispatch/CollisionObject.cpp
extern/bullet/Bullet/CollisionDispatch/ConvexConvexAlgorithm.cpp
extern/bullet/Bullet/CollisionShapes/BoxShape.h
extern/bullet/Bullet/CollisionShapes/ConeShape.cpp
extern/bullet/Bullet/CollisionShapes/ConvexHullShape.cpp
extern/bullet/Bullet/CollisionShapes/ConvexShape.cpp
extern/bullet/Bullet/CollisionShapes/CylinderShape.h
extern/bullet/Bullet/CollisionShapes/SphereShape.cpp
extern/bullet/Extras/PhysicsInterface/CcdPhysics/CcdPhysicsController.cpp
extern/bullet/Extras/PhysicsInterface/CcdPhysics/CcdPhysicsEnvironment.cpp
source/gameengine/Physics/Bullet/CcdPhysicsController.cpp
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp

index 565045b39a34b4aa0034253f0824acc0a3f83e0f..a1e132c0f607a78b73f7b38e71d239523ee9d57d 100644 (file)
@@ -33,13 +33,20 @@ void CollisionDispatcher::FindUnions()
                {
                        const PersistentManifold* manifold = this->GetManifoldByIndexInternal(i);
                        //static objects (invmass 0.f) don't merge !
-                       if ((((CollisionObject*)manifold->GetBody0()) && (((CollisionObject*)manifold->GetBody0())->mergesSimulationIslands())) &&
-                               (((CollisionObject*)manifold->GetBody1()) && (((CollisionObject*)manifold->GetBody1())->mergesSimulationIslands())))
-                       {
 
-                               m_unionFind.unite(((CollisionObject*)manifold->GetBody0())->m_islandTag1,
-                                       ((CollisionObject*)manifold->GetBody1())->m_islandTag1);
-                       }
+                        const  CollisionObject* colObj0 = static_cast<const CollisionObject*>(manifold->GetBody0());
+                        const  CollisionObject* colObj1 = static_cast<const CollisionObject*>(manifold->GetBody1());
+
+                        if (colObj0 && colObj1 && NeedsResponse(*colObj0,*colObj1))
+                        {
+                               if (((colObj0) && ((colObj0)->mergesSimulationIslands())) &&
+                                       ((colObj1) && ((colObj1)->mergesSimulationIslands())))
+                               {
+
+                                       m_unionFind.unite((colObj0)->m_islandTag1,
+                                               (colObj1)->m_islandTag1);
+                               }
+                        }
                        
                        
                }
@@ -119,7 +126,7 @@ void CollisionDispatcher::BuildAndProcessIslands(int numBodies, IslandCallback*
                         CollisionObject* colObj0 = static_cast<CollisionObject*>(manifold->GetBody0());
                         CollisionObject* colObj1 = static_cast<CollisionObject*>(manifold->GetBody1());
 
-                        if (NeedsResponse(*colObj0,*colObj1))
+                        
                         {
                                if (((colObj0) && (colObj0)->m_islandTag1 == (islandId)) ||
                                        ((colObj1) && (colObj1)->m_islandTag1 == (islandId)))
@@ -224,11 +231,11 @@ CollisionAlgorithm* CollisionDispatcher::InternalFindAlgorithm(BroadphaseProxy&
        
 }
 
-bool   CollisionDispatcher::NeedsResponse(CollisionObject& colObj0,CollisionObject& colObj1)
+bool   CollisionDispatcher::NeedsResponse(const  CollisionObject& colObj0,const CollisionObject& colObj1)
 {
        //here you can do filtering
        bool hasResponse = 
-               (!(colObj0.m_collisionFlags & CollisionObject::noContactResponse)) &
+               (!(colObj0.m_collisionFlags & CollisionObject::noContactResponse)) &&
                (!(colObj1.m_collisionFlags & CollisionObject::noContactResponse));
        return hasResponse;
 }
index 91cacccc52d3232fe91f15301c1e3b7e03adc470..e3f3c09e76d243c7e7e9ce74e293850fea720f73 100644 (file)
@@ -124,7 +124,7 @@ public:
        
        virtual bool    NeedsCollision(BroadphaseProxy& proxy0,BroadphaseProxy& proxy1);
        
-       virtual bool    NeedsResponse(CollisionObject& colObj0,CollisionObject& colObj1);
+       virtual bool    NeedsResponse(const CollisionObject& colObj0,const CollisionObject& colObj1);
 
        virtual int GetUniqueId() { return RIGIDBODY_DISPATCHER;}
        
index 33c45179104f110093ce6002746b003c6e8b94c9..ab0fea21605cbd41363b03d5f7a261a69134fe0b 100644 (file)
@@ -40,8 +40,11 @@ void CollisionObject::ForceActivationState(int newState)
 
 void CollisionObject::activate()
 {
+       if (!(m_collisionFlags & isStatic))
+       {
                SetActivationState(1);
                m_deactivationTime = 0.f;
+       }
 }
 
 bool CollisionObject::mergesSimulationIslands() const
index 443899e62a310f361fa52cd61def699f7623fbc8..ad06b68aa92233b89c0ac2c9b0ab05068ab64010 100644 (file)
@@ -333,7 +333,7 @@ void ConvexConvexAlgorithm ::ProcessCollision (BroadphaseProxy* ,BroadphaseProxy
                input.m_maximumDistanceSquared*= input.m_maximumDistanceSquared;
        }
 
-       input.m_maximumDistanceSquared = 1e30;//
+       input.m_maximumDistanceSquared = 1e30f;//
        
        input.m_transformA = col0->m_worldTransform;
        input.m_transformB = col1->m_worldTransform;
index f7c5bba73455b30e976cf4ada74c72582292e178..f47f982112d0f337c88b98b93f71963bd3592d84 100644 (file)
@@ -59,7 +59,7 @@ public:
                if ( GetMargin()!=0.f )
                {
                        SimdVector3 vecnorm = vec;
-                       if (vecnorm .length2() == 0.f)
+                       if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
                        {
                                vecnorm.setValue(-1.f,-1.f,-1.f);
                        } 
index 9543aa48139abbc49eae2fad014dad16f85af4fa..fb99c2c44803099c0c61e8c9a6d6498e0247f79b 100644 (file)
@@ -87,7 +87,7 @@ SimdVector3   ConeShape::LocalGetSupportingVertex(const SimdVector3& vec)  const
        if ( GetMargin()!=0.f )
        {
                SimdVector3 vecnorm = vec;
-               if (vecnorm .length2() == 0.f)
+               if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
                {
                        vecnorm.setValue(-1.f,-1.f,-1.f);
                } 
index 5de2ea3dcb8ea7d59375b38903ea5435931ddd3f..e527dcd7e84302d1a0e5962abadd34b34cb7a09e 100644 (file)
@@ -97,7 +97,7 @@ SimdVector3   ConvexHullShape::LocalGetSupportingVertex(const SimdVector3& vec)con
        if ( GetMargin()!=0.f )
        {
                SimdVector3 vecnorm = vec;
-               if (vecnorm .length2() == 0.f)
+               if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
                {
                        vecnorm.setValue(-1.f,-1.f,-1.f);
                } 
index 2ca3511a8daccf145a3504e908e1f197c167de66..951f7a07d3a2e6dd657bb691248b91572bcf3a95 100644 (file)
@@ -60,7 +60,7 @@ SimdVector3   ConvexShape::LocalGetSupportingVertex(const SimdVector3& vec)const
        if ( GetMargin()!=0.f )
        {
                SimdVector3 vecnorm = vec;
-               if (vecnorm .length2() == 0.f)
+               if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
                {
                        vecnorm.setValue(-1.f,-1.f,-1.f);
                } 
index b0814f98066084cae36437bd9e20854de02ecaa8..2c0c8008d719aa9df5a7f672b159f30f1376ceb6 100644 (file)
@@ -45,7 +45,7 @@ public:
                if ( GetMargin()!=0.f )
                {
                        SimdVector3 vecnorm = vec;
-                       if (vecnorm .length2() == 0.f)
+                       if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
                        {
                                vecnorm.setValue(-1.f,-1.f,-1.f);
                        } 
index 6cc9fd098871da20b4049f9801fd335d44dc0941..28c46a28ab74ee1bdfc714a561f95c9e72dc864a 100644 (file)
@@ -44,7 +44,7 @@ SimdVector3   SphereShape::LocalGetSupportingVertex(const SimdVector3& vec)const
        supVertex = LocalGetSupportingVertexWithoutMargin(vec);
 
        SimdVector3 vecnorm = vec;
-       if (SimdFuzzyZero(vecnorm .length2()))
+       if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
        {
                vecnorm.setValue(-1.f,-1.f,-1.f);
        } 
index 57f2957556995a1d9154bf4da94644da2f0b1562..cbe7961773d4c91186d5bbedfa15a5ebe6e272a7 100644 (file)
@@ -195,16 +195,21 @@ void              CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionsta
                // kinematic methods
 void           CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local)
 {
-       SimdVector3 dloc(dlocX,dlocY,dlocZ);
-       SimdTransform xform = m_body->getCenterOfMassTransform();
-
-       if (local)
+       if (m_body)
        {
-               dloc = xform.getBasis()*dloc;
-       }
+               m_body->activate();
+
+               SimdVector3 dloc(dlocX,dlocY,dlocZ);
+               SimdTransform xform = m_body->getCenterOfMassTransform();
+
+               if (local)
+               {
+                       dloc = xform.getBasis()*dloc;
+               }
 
-       xform.setOrigin(xform.getOrigin() + dloc);
-       this->m_body->setCenterOfMassTransform(xform);
+               xform.setOrigin(xform.getOrigin() + dloc);
+               m_body->setCenterOfMassTransform(xform);
+       }
 
 }
 
@@ -212,6 +217,8 @@ void                CcdPhysicsController::RelativeRotate(const float rotval[9],bool local)
 {
        if (m_body )
        {
+               m_body->activate();
+
                SimdMatrix3x3 drotmat(  rotval[0],rotval[1],rotval[2],
                                                                rotval[4],rotval[5],rotval[6],
                                                                rotval[8],rotval[9],rotval[10]);
@@ -319,34 +326,47 @@ void              CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bo
 void           CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
 {
        SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
-       SimdTransform xform = m_body->getCenterOfMassTransform();
-       if (local)
+       if (angvel.length2() > (SIMD_EPSILON*SIMD_EPSILON))
        {
-               angvel  = xform.getBasis()*angvel;
-       }
+               SimdTransform xform = m_body->getCenterOfMassTransform();
+               if (local)
+               {
+                       angvel  = xform.getBasis()*angvel;
+               }
 
-       m_body->setAngularVelocity(angvel);
+               m_body->setAngularVelocity(angvel);
+       }
 
 }
 void           CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
 {
+
        SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
-       SimdTransform xform = m_body->getCenterOfMassTransform();
-       if (local)
+       if (linVel.length2() > (SIMD_EPSILON*SIMD_EPSILON))
        {
-               linVel  = xform.getBasis()*linVel;
+               m_body->activate();
+               SimdTransform xform = m_body->getCenterOfMassTransform();
+               if (local)
+               {
+                       linVel  = xform.getBasis()*linVel;
+               }
+               m_body->setLinearVelocity(linVel);
        }
-       m_body->setLinearVelocity(linVel);
 }
 void           CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
 {
-       
        SimdVector3 impulse(impulseX,impulseY,impulseZ);
-       SimdVector3 pos(attachX,attachY,attachZ);
 
-       m_body->activate();
+       if (impulse.length2() > (SIMD_EPSILON*SIMD_EPSILON))
+       {
+               m_body->activate();
+               
+               SimdVector3 pos(attachX,attachY,attachZ);
+
+               m_body->activate();
 
-       m_body->applyImpulse(impulse,pos);
+               m_body->applyImpulse(impulse,pos);
+       }
 
 }
 void           CcdPhysicsController::SetActive(bool active)
index 7f9bbd62ceb99fb02cae9aa80ee7bbc69c376a7f..cf9c41574753530566e1b41bcdb338465d3424c0 100644 (file)
@@ -1462,15 +1462,15 @@ TypedConstraint*        CcdPhysicsEnvironment::getConstraintById(int constraintId)
 
 void CcdPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl)
 {
-       printf("addSensor\n");
+       //printf("addSensor\n");
 }
 void CcdPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl)
 {
-       printf("removeSensor\n");
+       //printf("removeSensor\n");
 }
 void CcdPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)
 {
-       printf("addTouchCallback\n(response class = %i)\n",response_class);
+/*     printf("addTouchCallback\n(response class = %i)\n",response_class);
 
        //map PHY_ convention into SM_ convention
        switch (response_class)
@@ -1494,6 +1494,7 @@ void CcdPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCal
                assert(0);
                return;
        }
+*/
 
        m_triggerCallbacks[response_class] = callback;
        m_triggerCallbacksUserPtrs[response_class] = user;
@@ -1503,7 +1504,7 @@ void CcdPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ctr
 {
        CcdPhysicsController* ccdCtrl = static_cast<CcdPhysicsController*>(ctrl);
 
-       printf("requestCollisionCallback\n");
+       //printf("requestCollisionCallback\n");
        m_triggerControllers.push_back(ccdCtrl);
 }
 
@@ -1514,7 +1515,7 @@ void      CcdPhysicsEnvironment::CallbackTriggers()
 
        if (m_triggerCallbacks[PHY_OBJECT_RESPONSE])
        {
-
+               //walk over all overlapping pairs, and if one of the involved bodies is registered for trigger callback, perform callback
                int numManifolds = m_collisionWorld->GetDispatcher()->GetNumManifolds();
                for (int i=0;i<numManifolds;i++)
                {
@@ -1547,7 +1548,7 @@ void      CcdPhysicsEnvironment::CallbackTriggers()
                
 
        }
-       //walk over all overlapping pairs, and if
+
 }
 
 
index 57f2957556995a1d9154bf4da94644da2f0b1562..cbe7961773d4c91186d5bbedfa15a5ebe6e272a7 100644 (file)
@@ -195,16 +195,21 @@ void              CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionsta
                // kinematic methods
 void           CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local)
 {
-       SimdVector3 dloc(dlocX,dlocY,dlocZ);
-       SimdTransform xform = m_body->getCenterOfMassTransform();
-
-       if (local)
+       if (m_body)
        {
-               dloc = xform.getBasis()*dloc;
-       }
+               m_body->activate();
+
+               SimdVector3 dloc(dlocX,dlocY,dlocZ);
+               SimdTransform xform = m_body->getCenterOfMassTransform();
+
+               if (local)
+               {
+                       dloc = xform.getBasis()*dloc;
+               }
 
-       xform.setOrigin(xform.getOrigin() + dloc);
-       this->m_body->setCenterOfMassTransform(xform);
+               xform.setOrigin(xform.getOrigin() + dloc);
+               m_body->setCenterOfMassTransform(xform);
+       }
 
 }
 
@@ -212,6 +217,8 @@ void                CcdPhysicsController::RelativeRotate(const float rotval[9],bool local)
 {
        if (m_body )
        {
+               m_body->activate();
+
                SimdMatrix3x3 drotmat(  rotval[0],rotval[1],rotval[2],
                                                                rotval[4],rotval[5],rotval[6],
                                                                rotval[8],rotval[9],rotval[10]);
@@ -319,34 +326,47 @@ void              CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bo
 void           CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
 {
        SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
-       SimdTransform xform = m_body->getCenterOfMassTransform();
-       if (local)
+       if (angvel.length2() > (SIMD_EPSILON*SIMD_EPSILON))
        {
-               angvel  = xform.getBasis()*angvel;
-       }
+               SimdTransform xform = m_body->getCenterOfMassTransform();
+               if (local)
+               {
+                       angvel  = xform.getBasis()*angvel;
+               }
 
-       m_body->setAngularVelocity(angvel);
+               m_body->setAngularVelocity(angvel);
+       }
 
 }
 void           CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
 {
+
        SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
-       SimdTransform xform = m_body->getCenterOfMassTransform();
-       if (local)
+       if (linVel.length2() > (SIMD_EPSILON*SIMD_EPSILON))
        {
-               linVel  = xform.getBasis()*linVel;
+               m_body->activate();
+               SimdTransform xform = m_body->getCenterOfMassTransform();
+               if (local)
+               {
+                       linVel  = xform.getBasis()*linVel;
+               }
+               m_body->setLinearVelocity(linVel);
        }
-       m_body->setLinearVelocity(linVel);
 }
 void           CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
 {
-       
        SimdVector3 impulse(impulseX,impulseY,impulseZ);
-       SimdVector3 pos(attachX,attachY,attachZ);
 
-       m_body->activate();
+       if (impulse.length2() > (SIMD_EPSILON*SIMD_EPSILON))
+       {
+               m_body->activate();
+               
+               SimdVector3 pos(attachX,attachY,attachZ);
+
+               m_body->activate();
 
-       m_body->applyImpulse(impulse,pos);
+               m_body->applyImpulse(impulse,pos);
+       }
 
 }
 void           CcdPhysicsController::SetActive(bool active)
index 7f9bbd62ceb99fb02cae9aa80ee7bbc69c376a7f..cf9c41574753530566e1b41bcdb338465d3424c0 100644 (file)
@@ -1462,15 +1462,15 @@ TypedConstraint*        CcdPhysicsEnvironment::getConstraintById(int constraintId)
 
 void CcdPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl)
 {
-       printf("addSensor\n");
+       //printf("addSensor\n");
 }
 void CcdPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl)
 {
-       printf("removeSensor\n");
+       //printf("removeSensor\n");
 }
 void CcdPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)
 {
-       printf("addTouchCallback\n(response class = %i)\n",response_class);
+/*     printf("addTouchCallback\n(response class = %i)\n",response_class);
 
        //map PHY_ convention into SM_ convention
        switch (response_class)
@@ -1494,6 +1494,7 @@ void CcdPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCal
                assert(0);
                return;
        }
+*/
 
        m_triggerCallbacks[response_class] = callback;
        m_triggerCallbacksUserPtrs[response_class] = user;
@@ -1503,7 +1504,7 @@ void CcdPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ctr
 {
        CcdPhysicsController* ccdCtrl = static_cast<CcdPhysicsController*>(ctrl);
 
-       printf("requestCollisionCallback\n");
+       //printf("requestCollisionCallback\n");
        m_triggerControllers.push_back(ccdCtrl);
 }
 
@@ -1514,7 +1515,7 @@ void      CcdPhysicsEnvironment::CallbackTriggers()
 
        if (m_triggerCallbacks[PHY_OBJECT_RESPONSE])
        {
-
+               //walk over all overlapping pairs, and if one of the involved bodies is registered for trigger callback, perform callback
                int numManifolds = m_collisionWorld->GetDispatcher()->GetNumManifolds();
                for (int i=0;i<numManifolds;i++)
                {
@@ -1547,7 +1548,7 @@ void      CcdPhysicsEnvironment::CallbackTriggers()
                
 
        }
-       //walk over all overlapping pairs, and if
+
 }