Ghost object internal bodies

Post Reply
BertS
Posts: 2
Joined: Wed Mar 14, 2012 2:52 pm

Ghost object internal bodies

Post by BertS »

I'm using a ghost object to detect overlapping objects using the following code:

Code: Select all

m_World->getDispatcher()->dispatchAllCollisionPairs(pairCache, m_World->getDispatchInfo(), m_World->getDispatcher());
		const unsigned int numPairs = pairCache->getNumOverlappingPairs();
		btManifoldArray	manifoldArray;

		for(unsigned int i = 0; i< numPairs;++i)
		{
			const btBroadphasePair* cPair = &pairCache->getOverlappingPairArray()[i];
			{
				manifoldArray.resize(0);
				if (cPair->m_algorithm)
					cPair->m_algorithm->getAllContactManifolds(manifoldArray);

				for (int j=0;j<manifoldArray.size();j++)
				{
					btPersistentManifold* manifold = manifoldArray[j];
					if(manifold->getNumContacts())
					{
						return true;
					}
					else
					{
						//TODO: determine if we're entirely enclosed by the volume or not

					}
				}

			}
		}
	}
This works fine to detect objects that intersect the ghost body, but it doesn't work when the ghost is completely enclosed (no features touching) by an object. Because in that case no contact points are generated. I only want to detect an overlap when the ghost is intersecting or inside the body, so I can't just check the bounding boxes of both. How would I check that the ghost body is completely enclosed by another body?
Karrok
Posts: 65
Joined: Fri May 13, 2011 1:11 pm

Re: Ghost object internal bodies

Post by Karrok »

you could do axis-aligned ray test from the object outwards.

so 6 ray tests in +/- x, y, z.

If all return the sphere as the first object encountered that it would be fairly safe to assume the object is enclosed in the sphere.

Alternatively:

Once an initial collision with the sphere happens, keep a reference to it.
Once there is no longer a collision between the sphere and your ghost object. Check the reference of it for it's (center)location and its radius.
Having the center (cog) of both objects and the radius of the sphere you could tell if it is within the sphere if the center location of the ghost object is within the radius from the center of the sphere to its perimeter.

In other words:

if ( sqrt ( (sphere.x - ghost.x )^2 + (sphere.x - ghost.y)^2 ) > sphere.radius && checkCollisionPair(ghost, sphere) == 0) {
//ghost is inside the sphere!
}

Then simply keep checking this, and once the first statement becomes false and there is no longer collision, the object will be fully outside of the sphere.
At which point you remove the reference to the sphere from your list (orwhatever)

Hope this rambling helps :)
BertS
Posts: 2
Joined: Wed Mar 14, 2012 2:52 pm

Re: Ghost object internal bodies

Post by BertS »

Thanks for your reply. Unfortunately, the enclosing object is not a sphere or a box, but is either a triangle mesh or a convex hull. So I can't just check bounding boxes or spheres.

I can probably do what I want if I sweep the ghost down a small amount, but I was hoping to do it without having to do traces.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Ghost object internal bodies

Post by Flix »

BertS wrote:This works fine to detect objects that intersect the ghost body, but it doesn't work when the ghost is completely enclosed (no features touching) by an object.
I'm not sure about it, but I don't think that the collision detection "triggered" by ghost objects is any different from the one commonly used in Bullet. If this is true convex hulls should work and triangle meshes shouldn't (although starting with an object completely hidden inside another is something I've never tried). So you could try using convex hulls (or convex decompositions of triangle meshes through HACD) instead of btBvhTriangleMeshShapes or btGImpactMeshShapes and see if it works.
Post Reply