missing detection using collisionWorld::rayTestSingle

Post Reply
guernika
Posts: 18
Joined: Fri May 07, 2010 2:20 pm

missing detection using collisionWorld::rayTestSingle

Post by guernika »

dear group,

I am trying to implement a ray cast based collision detection.
The case study is quite simple:
it is a rectangular shape falling over a cylinder.
It works fine if I use btGjkConvexCast,
but it doesn't work using collisionWorld::rayTestSingle.
It seems that collisionWorld::rayTestSingle misses a lot of collisions,
even if the rays are the same for both the methods.
Can you explain me why?
What can I do if I want detect collisions against non convex shapes?

Regards,
Francesco

Code: Select all

void ClothSimulator::solveCollision(REAL dt)
{
	BT_PROFILE("solveCollision");
	//TODO: Handle multiple rigid objects
	
	//.. find rigid object
	btCollisionObject* cyl_obj = m_world->getCollisionObjectArray()[1];
	btTransform t = cyl_obj->getWorldTransform();
	btConvexShape* cyl_shape = (btConvexShape*) cyl_obj->getCollisionShape();
	
	btVector3 aabbMin, aabbMax;
	cyl_shape->getAabb(t,aabbMin,aabbMax);	
	
	btVoronoiSimplexSolver	simplexSolver;

	const unsigned npoints = m_particle.m_p.size();
	for ( size_t i=0; i<npoints; i++) 
	{
		bool hasHit = false;
		btScalar closestHitResults = 1.f;
		btConvexCast::CastResult rayResult;
		btSphereShape pointShape(0.0f);
 
                vec3 from =......
                vec3 to = .......

		btTransform rayFromTrans;
		btTransform rayToTrans;
		
		rayFromTrans.setIdentity();
		rayFromTrans.setOrigin( btVector3(from.x,from.y,from.z) );
		
		rayToTrans.setIdentity();
		rayToTrans.setOrigin(btVector3(to.x,to.y,to.z));
		
		btScalar hitLambda = 1.f;
		btVector3 hitNormal;

		btVector3 rayFrom = btVector3(from.x,from.y,from.z);
		btVector3 rayTo = btVector3(to.x,to.y,to.z);

                this is the the code for simple ray test:
		btCollisionWorld::ClosestRayResultCallback cb(rayFrom, rayTo);
		m_world->rayTestSingle(rayFromTrans,rayToTrans, cyl_obj, cyl_obj->getCollisionShape(), t, cb);
		//m_world->rayTest(rayFrom, rayTo, cb );

		if (cb.hasHit()) 
		{
                       ..............
		}
		
                this is the code for convex cast ray test:		
		if (btRayAabb(rayFrom, rayTo, aabbMin, aabbMax, hitLambda, hitNormal))
		{
			btTransform transform;
			//btSubsimplexConvexCast convexCaster(&pointShape, cyl_shape,&simplexSolver);
			btGjkConvexCast convexCaster(&pointShape, cyl_shape,&simplexSolver);
			if (convexCaster.calcTimeOfImpact(rayFromTrans,rayToTrans,t,t,rayResult))
			{
				if (rayResult.m_fraction < closestHitResults)
				{
					closestHitResults = rayResult.m_fraction;
	
					hasHit = true;
					if (hasHit)
					{
	                                    .................
					}
				}
			}	
		}
		
		//-- try to discover difference between convex caster and simple ray test
		if (cb.hasHit() && hasHit)
		{
			btVector3 delta = cb.m_hitPointWorld - rayResult.m_hitPoint;
			printf("delta=(%f,%f,%f)\n",delta.getX(),delta.getY(),delta.getZ());
			
		}
		if (hasHit && !cb.hasHit()) 
		{
			printf("missing detection by simple ray cast\n");
		}
		if (cb.hasHit() && !hasHit) 
		{
			printf("missing detection by convex ray cast\n");
		}
	}
}

Post Reply