Bullet patch: option to return true face normal, complete triangle information and...
authorBenoit Bolsee <benoit.bolsee@online.be>
Wed, 27 Aug 2008 19:16:21 +0000 (19:16 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Wed, 27 Aug 2008 19:16:21 +0000 (19:16 +0000)
extern/bullet2/src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp
extern/bullet2/src/BulletCollision/CollisionDispatch/btCollisionWorld.h
extern/bullet2/src/BulletCollision/NarrowPhaseCollision/btRaycastCallback.cpp
extern/bullet2/src/BulletCollision/NarrowPhaseCollision/btRaycastCallback.h

index b49036a5b50322b12b27e726b9024ebb0241d3b7..7dc7d8d2f68e1483707fa1e278983e24eb87d2ab 100644 (file)
@@ -181,7 +181,9 @@ void        btCollisionWorld::rayTestSingle(const btTransform& rayFromTrans,const btTra
                                          btCollisionObject* collisionObject,
                                          const btCollisionShape* collisionShape,
                                          const btTransform& colObjWorldTransform,
-                                         RayResultCallback& resultCallback,short int collisionFilterMask)
+                                         RayResultCallback& resultCallback,
+                                         short int collisionFilterMask,
+                                         bool faceNormal)
 {
        
        btSphereShape pointShape(btScalar(0.0));
@@ -191,14 +193,16 @@ void      btCollisionWorld::rayTestSingle(const btTransform& rayFromTrans,const btTra
                                          collisionObject,
                                          collisionShape,
                                          colObjWorldTransform,
-                                         resultCallback,collisionFilterMask);
+                                         resultCallback,collisionFilterMask,faceNormal);
 }
 
 void   btCollisionWorld::objectQuerySingle(const btConvexShape* castShape,const btTransform& rayFromTrans,const btTransform& rayToTrans,
                                          btCollisionObject* collisionObject,
                                          const btCollisionShape* collisionShape,
                                          const btTransform& colObjWorldTransform,
-                                         RayResultCallback& resultCallback,short int collisionFilterMask)
+                                         RayResultCallback& resultCallback,
+                                         short int collisionFilterMask,
+                                         bool faceNormal)
 {
        
 
@@ -257,9 +261,9 @@ void        btCollisionWorld::objectQuerySingle(const btConvexShape* castShape,const bt
                                                        btCollisionObject*      m_collisionObject;
                                                        btTriangleMeshShape*    m_triangleMesh;
 
-                                                       BridgeTriangleRaycastCallback( const btVector3& from,const btVector3& to,
-                                                               btCollisionWorld::RayResultCallback* resultCallback, btCollisionObject* collisionObject,btTriangleMeshShape*    triangleMesh):
-                                                               btTriangleRaycastCallback(from,to),
+                                                       BridgeTriangleRaycastCallback( const btVector3& from,const btVector3& to,bool faceNormal,
+                                                               btCollisionWorld::RayResultCallback* resultCallback, btCollisionObject* collisionObject,btTriangleMeshShape* triangleMesh):
+                                                               btTriangleRaycastCallback(from,to,faceNormal),
                                                                        m_resultCallback(resultCallback),
                                                                        m_collisionObject(collisionObject),
                                                                        m_triangleMesh(triangleMesh)
@@ -272,6 +276,7 @@ void        btCollisionWorld::objectQuerySingle(const btConvexShape* castShape,const bt
                                                                btCollisionWorld::LocalShapeInfo        shapeInfo;
                                                                shapeInfo.m_shapePart = partId;
                                                                shapeInfo.m_triangleIndex = triangleIndex;
+                                                               shapeInfo.m_triangleShape = m_triangleMesh;
                                                                
                                                                btCollisionWorld::LocalRayResult rayResult
                                                                (m_collisionObject, 
@@ -287,7 +292,7 @@ void        btCollisionWorld::objectQuerySingle(const btConvexShape* castShape,const bt
                                                };
 
 
-                                               BridgeTriangleRaycastCallback   rcb(rayFromLocal,rayToLocal,&resultCallback,collisionObject,triangleMesh);
+                                               BridgeTriangleRaycastCallback   rcb(rayFromLocal,rayToLocal,faceNormal,&resultCallback,collisionObject,triangleMesh);
                                                rcb.m_hitFraction = resultCallback.m_closestHitFraction;
 
                                                btVector3 rayAabbMinLocal = rayFromLocal;
@@ -313,7 +318,7 @@ void        btCollisionWorld::objectQuerySingle(const btConvexShape* castShape,const bt
                                                                        collisionObject,
                                                                        childCollisionShape,
                                                                        childWorldTrans,
-                                                                       resultCallback, collisionFilterMask);
+                                                                       resultCallback, collisionFilterMask, faceNormal);
 
                                                        }
 
@@ -323,7 +328,7 @@ void        btCollisionWorld::objectQuerySingle(const btConvexShape* castShape,const bt
                        }
 }
 
-void   btCollisionWorld::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback,short int collisionFilterMask)
+void   btCollisionWorld::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback,short int collisionFilterMask, bool faceNormal)
 {
 
        
@@ -350,11 +355,17 @@ void      btCollisionWorld::rayTest(const btVector3& rayFromWorld, const btVector3& r
                        btVector3 hitNormal;
                        if (btRayAabb(rayFromWorld,rayToWorld,collisionObjectAabbMin,collisionObjectAabbMax,hitLambda,hitNormal))
                        {
-                               rayTestSingle(rayFromTrans,rayToTrans,
-                                       collisionObject,
-                                               collisionObject->getCollisionShape(),
-                                               collisionObject->getWorldTransform(),
-                                               resultCallback);
+                               // before testing this object, verify that it is not filtered out
+                               if (resultCallback.NeedRayCast(collisionObject))
+                               {
+                                       rayTestSingle(rayFromTrans,rayToTrans,
+                                               collisionObject,
+                                                       collisionObject->getCollisionShape(),
+                                                       collisionObject->getWorldTransform(),
+                                                       resultCallback,
+                                                       collisionFilterMask,
+                                                       faceNormal);
+                               }
                        }       
                }
        }
index b6d80233ab7acbd927774aed8dc188e5e90f5e92..ed41232ece3fe091158e658bcb55ff2c5f9629ac 100644 (file)
@@ -125,8 +125,8 @@ public:
        {
                int     m_shapePart;
                int     m_triangleIndex;
-               
-               //const btCollisionShape*       m_shapeTemp;
+               // needed in case of compound shape
+               const btCollisionShape* m_triangleShape;
                //const btTransform*    m_shapeLocalTransform;
        };
 
@@ -166,6 +166,10 @@ public:
                        :m_closestHitFraction(btScalar(1.))
                {
                }
+               virtual bool        NeedRayCast(btCollisionObject* object)
+               {
+                       return true;
+               }
                virtual btScalar        AddSingleResult(LocalRayResult& rayResult) = 0;
        };
 
@@ -209,7 +213,7 @@ public:
 
        /// rayTest performs a raycast on all objects in the btCollisionWorld, and calls the resultCallback
        /// This allows for several queries: first hit, all hits, any hit, dependent on the value returned by the callback.
-       void    rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback, short int collisionFilterMask=-1);
+       void    rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback, short int collisionFilterMask=-1, bool faceNormal=false);
 
        /// rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest.
        /// In a future implementation, we consider moving the ray test as a virtual method in btCollisionShape.
@@ -218,14 +222,18 @@ public:
                                          btCollisionObject* collisionObject,
                                          const btCollisionShape* collisionShape,
                                          const btTransform& colObjWorldTransform,
-                                         RayResultCallback& resultCallback, short int collisionFilterMask=-1);
+                                         RayResultCallback& resultCallback, 
+                                         short int collisionFilterMask=-1,
+                                         bool faceNormal=false);
 
        /// objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest.
        static void     objectQuerySingle(const btConvexShape* castShape, const btTransform& rayFromTrans,const btTransform& rayToTrans,
                                          btCollisionObject* collisionObject,
                                          const btCollisionShape* collisionShape,
                                          const btTransform& colObjWorldTransform,
-                                         RayResultCallback& resultCallback, short int collisionFilterMask=-1);
+                                         RayResultCallback& resultCallback, 
+                                         short int collisionFilterMask=-1,
+                                         bool faceNormal=false);
 
        void    addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=1,short int collisionFilterMask=1);
 
index 31b914677777ed643da4ff85a41f93080b2c4c52..68ac93ec3cca5e204550ab344441927ac5e63508 100644 (file)
@@ -16,10 +16,11 @@ subject to the following restrictions:
 
 #include "btRaycastCallback.h"
 
-btTriangleRaycastCallback::btTriangleRaycastCallback(const btVector3& from,const btVector3& to)
+btTriangleRaycastCallback::btTriangleRaycastCallback(const btVector3& from,const btVector3& to,bool faceNormal)
        :
        m_from(from),
        m_to(to),
+       m_faceNormal(faceNormal),
        m_hitFraction(btScalar(1.))
 {
 
@@ -84,8 +85,7 @@ void btTriangleRaycastCallback::processTriangle(btVector3* triangle,int partId,
                                        
                                        if ( (btScalar)(cp2.dot(triangleNormal)) >=edge_tolerance) 
                                        {
-
-                                               if ( dist_a > 0 )
+                                               if (m_faceNormal || dist_a > 0)
                                                {
                                                        m_hitFraction = reportHit(triangleNormal,distance,partId,triangleIndex);
                                                }
index a0bbc9f8fe95f45f7dc128a8f14d7f88e9fc7675..71ed9fead49fd3c3cd1a2da849b5d31f80ab4f53 100644 (file)
@@ -27,10 +27,11 @@ public:
                //input
        btVector3 m_from;
        btVector3 m_to;
+       bool m_faceNormal;
 
        btScalar        m_hitFraction;
 
-       btTriangleRaycastCallback(const btVector3& from,const btVector3& to);
+       btTriangleRaycastCallback(const btVector3& from,const btVector3& to,bool faceNormal);
        
        virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex);