Logo Search packages:      
Sourcecode: blender version File versions

void CollisionWorld::RayTest ( const SimdVector3 rayFromWorld,
const SimdVector3 rayToWorld,
RayResultCallback resultCallback 
)

brute force go over all objects. Once there is a broadphase, use that, or add a raycast against aabb first.

Definition at line 190 of file CollisionWorld.cpp.

References CollisionWorld::RayResultCallback::AddSingleResult(), SimdVector3::length2(), CollisionWorld::RayResultCallback::m_closestHitFraction, ConvexCast::CastResult::m_fraction, ConvexCast::CastResult::m_normal, CollisionWorld::LocalShapeInfo::m_shapePart, CollisionWorld::LocalShapeInfo::m_triangleIndex, SimdVector3::normalize(), and TriangleMeshShape::ProcessAllTriangles().

{

      
      SimdTransform     rayFromTrans,rayToTrans;
      rayFromTrans.setIdentity();
      rayFromTrans.setOrigin(rayFromWorld);
      rayToTrans.setIdentity();
      
      rayToTrans.setOrigin(rayToWorld);

      //do culling based on aabb (rayFrom/rayTo)
      SimdVector3 rayAabbMin = rayFromWorld;
      SimdVector3 rayAabbMax = rayFromWorld;
      rayAabbMin.setMin(rayToWorld);
      rayAabbMax.setMax(rayToWorld);

      SphereShape pointShape(0.0f);

      /// brute force go over all objects. Once there is a broadphase, use that, or
      /// add a raycast against aabb first.
      
      std::vector<CollisionObject*>::iterator iter;
      
      for (iter=m_collisionObjects.begin();
      !(iter==m_collisionObjects.end()); iter++)
      {
            
            CollisionObject*  collisionObject= (*iter);

            //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
            SimdVector3 collisionObjectAabbMin,collisionObjectAabbMax;
            collisionObject->m_collisionShape->GetAabb(collisionObject->m_worldTransform,collisionObjectAabbMin,collisionObjectAabbMax);

            //check aabb overlap

            if (TestAabbAgainstAabb2(rayAabbMin,rayAabbMax,collisionObjectAabbMin,collisionObjectAabbMax))
            {
                  if (collisionObject->m_collisionShape->IsConvex())
                  {
                        ConvexCast::CastResult castResult;
                        castResult.m_fraction = 1.f;//??

                        ConvexShape* convexShape = (ConvexShape*) collisionObject->m_collisionShape;
                        VoronoiSimplexSolver    simplexSolver;
                        SubsimplexConvexCast convexCaster(&pointShape,convexShape,&simplexSolver);
                        //GjkConvexCast   convexCaster(&pointShape,convexShape,&simplexSolver);
                        //ContinuousConvexCollision convexCaster(&pointShape,convexShape,&simplexSolver,0);
                        
                        if (convexCaster.calcTimeOfImpact(rayFromTrans,rayToTrans,collisionObject->m_worldTransform,collisionObject->m_worldTransform,castResult))
                        {
                              //add hit
                              if (castResult.m_normal.length2() > 0.0001f)
                              {
                                    castResult.m_normal.normalize();
                                    if (castResult.m_fraction < resultCallback.m_closestHitFraction)
                                    {
                                          

                                          CollisionWorld::LocalRayResult localRayResult
                                                (
                                                      collisionObject, 
                                                      0,
                                                      castResult.m_normal,
                                                      castResult.m_fraction
                                                );

                                          resultCallback.AddSingleResult(localRayResult);

                                    }
                              }
                        }
                  }
                  else
                  {
                        
                        if (collisionObject->m_collisionShape->IsConcave())
                              {

                                    TriangleMeshShape* triangleMesh = (TriangleMeshShape*)collisionObject->m_collisionShape;
                                    
                                    SimdTransform worldTocollisionObject = collisionObject->m_worldTransform.inverse();

                                    SimdVector3 rayFromLocal = worldTocollisionObject * rayFromTrans.getOrigin();
                                    SimdVector3 rayToLocal = worldTocollisionObject * rayToTrans.getOrigin();

                                    //ConvexCast::CastResult

                                    struct BridgeTriangleRaycastCallback : public TriangleRaycastCallback 
                                    {
                                          RayResultCallback* m_resultCallback;
                                          CollisionObject*  m_collisionObject;
                                          TriangleMeshShape*      m_triangleMesh;

                                          BridgeTriangleRaycastCallback( const SimdVector3& from,const SimdVector3& to,
                                                RayResultCallback* resultCallback, CollisionObject* collisionObject,TriangleMeshShape*    triangleMesh):
                                                TriangleRaycastCallback(from,to),
                                                      m_resultCallback(resultCallback),
                                                      m_collisionObject(collisionObject),
                                                      m_triangleMesh(triangleMesh)
                                                {
                                                }


                                          virtual float ReportHit(const SimdVector3& hitNormalLocal, float hitFraction, int partId, int triangleIndex )
                                          {
                                                CollisionWorld::LocalShapeInfo      shapeInfo;
                                                shapeInfo.m_shapePart = partId;
                                                shapeInfo.m_triangleIndex = triangleIndex;
                                                
                                                CollisionWorld::LocalRayResult rayResult
                                                (m_collisionObject, 
                                                      &shapeInfo,
                                                      hitNormalLocal,
                                                      hitFraction);
                                                
                                                return m_resultCallback->AddSingleResult(rayResult);
                                                
                                                
                                          }
      
                                    };


                                    BridgeTriangleRaycastCallback rcb(rayFromLocal,rayToLocal,&resultCallback,collisionObject,triangleMesh);
                                    rcb.m_hitFraction = resultCallback.m_closestHitFraction;

                                    SimdVector3 rayAabbMinLocal = rayFromLocal;
                                    rayAabbMinLocal.setMin(rayToLocal);
                                    SimdVector3 rayAabbMaxLocal = rayFromLocal;
                                    rayAabbMaxLocal.setMax(rayToLocal);

                                    triangleMesh->ProcessAllTriangles(&rcb,rayAabbMinLocal,rayAabbMaxLocal);
                                                                  
                              }
                              

                  }
            }
      }

}


Generated by  Doxygen 1.6.0   Back to index