Calculate 3D distance with Bullet Engine

MrOizo
Posts: 6
Joined: Wed Aug 18, 2010 6:44 pm

Calculate 3D distance with Bullet Engine

Post by MrOizo »

Hi!

I'd like to use bullet engine to calculate distances between convex shapes. The problem is that I'd like to use it under C#.

Could you give me some idea how to use Bullet with WPF?

Thanks in advance!

Regards,
Zoli
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Calculate 3D distance with Bullet Engine

Post by Erwin Coumans »

The btCollisionWorld::contactTest / contactPairTest can be modified to compute the closest distance for convex objects (using GJK for positive distance and EPA for penetration depth internally).

This can be wrapped for C# usage with C++/CLI.

We'll try to provide a code sample soon,
Thanks,
Erwin
MrOizo
Posts: 6
Joined: Wed Aug 18, 2010 6:44 pm

Re: Calculate 3D distance with Bullet Engine

Post by MrOizo »

Erwin Coumans wrote:The btCollisionWorld::contactTest / contactPairTest can be modified to compute the closest distance for convex objects (using GJK for positive distance and EPA for penetration depth internally).

This can be wrapped for C# usage with C++/CLI.

We'll try to provide a code sample soon,
Thanks,
Erwin
Thanks, for the name of the function.

A sample code will be a very big help for me, how to use the wrapper.

Thanks in advance!

Regards,
Zoli
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Calculate 3D distance with Bullet Engine

Post by Erwin Coumans »

See the latest trunk version (revision r2184 or later) in bullet-2.77\Demos\ConvexHullDistance\ConvexHullDistanceDemo.cpp for two different ways to compute the closest points between two convex objects:

Directly using GJK:

Code: Select all

btGjkEpaPenetrationDepthSolver epa;
	btGjkPairDetector	convexConvex(shapePtr[0],shapePtr[1],&sGjkSimplexSolver,&epa);

	btVector3 seperatingAxis(0.00000000f,0.059727669f,0.29259586f);
	convexConvex.setCachedSeperatingAxis(seperatingAxis);

	btPointCollector gjkOutput;
	btGjkPairDetector::ClosestPointInput input;
	input.m_transformA = tr[0];
	input.m_transformB = tr[1];

	convexConvex.getClosestPoints(input ,gjkOutput,0);

	if (gjkOutput.m_hasResult)
	{
		btVector3 endPt = gjkOutput.m_pointInWorld +
			gjkOutput.m_normalOnBInWorld*gjkOutput.m_distance;

		 glBegin(GL_LINES);
		glColor3f(1, 0, 0);
		glVertex3d(gjkOutput.m_pointInWorld.x(), gjkOutput.m_pointInWorld.y(),gjkOutput.m_pointInWorld.z());
		glVertex3d(endPt.x(),endPt.y(),endPt.z());
		glEnd();

	}
or using the btCollisionWorld:

Code: Select all

struct	MyContactResultCallback : public btCollisionWorld::ContactResultCallback
	{
		virtual	btScalar	addSingleResult(btManifoldPoint& cp,	const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1)
		{
			 glBegin(GL_LINES);
			glColor3f(1, 0, 0);
			glVertex3d(cp.m_positionWorldOnA.getX(),cp.m_positionWorldOnA.getY(),cp.m_positionWorldOnA.getZ());
			glVertex3d(cp.m_positionWorldOnB.getX(),cp.m_positionWorldOnB.getY(),cp.m_positionWorldOnB.getZ());
			glEnd();

			return 1.f;
		}
	};

	btDefaultCollisionConfiguration collisionConfiguration;
	btCollisionDispatcher				dispatcher(&collisionConfiguration);
	btDbvtBroadphase pairCache;
	btCollisionWorld world (&dispatcher,&pairCache,&collisionConfiguration);
	world.getDispatchInfo().m_convexMaxDistanceUseCPT = true;
	MyContactResultCallback result;
	btCollisionObject obA;
	obA.setCollisionShape(shapePtr[0]);
	obA.setWorldTransform(tr[0]);
	btCollisionObject obB;
	obB.setCollisionShape(shapePtr[1]);
	obB.setWorldTransform(tr[1]);
	world.contactPairTest(&obA,&obB,result);
This needs to be wrapped in an C++/CLI interface.

Thanks,
Erwin
MrOizo
Posts: 6
Joined: Wed Aug 18, 2010 6:44 pm

Re: Calculate 3D distance with Bullet Engine

Post by MrOizo »

Thanks for the example!

I found your examples for XNA, and I tried them, they work fine with the bulletsharp.dll .

By right of your examples I tried to calculate the distance between two boxshapes.

Here is the code:

Code: Select all

ConvexShape closestShape1 = new BoxShape(1, 1, 1);
ConvexShape closestShape2 = new BoxShape(1, 1, 1);

VoronoiSimplexSolver vSimpSolver = new VoronoiSimplexSolver();
ConvexPenetrationDepthSolver epa = new GjkEpaPenetrationDepthSolver();
GjkPairDetector gjkPairDetector = new GjkPairDetector(closestShape1, closestShape2, vSimpSolver, epa);
Vector3 sepAxes = new Vector3(0f, 0.059727669f, 0.29259586f);
gjkPairDetector.CachedSeparatingAxis = sepAxes;

PointCollector output = new PointCollector();
GjkPairDetector.ClosestPointInput closestPointInput = new DiscreteCollisionDetectorInterface.ClosestPointInput();
closestPointInput.TransformA = Matrix.Identity;
closestPointInput.TransformB = Matrix.CreateTranslation(new Vector3(0, 5, 0));

gjkPairDetector.GetClosestPoints(closestPointInput, output, DebugDrawer);
In the output I found a distance field, but the result is wrong.

Could you explain me, what I'm doing wrong?

Anyway, what is this Separating Axes, and why is its value Vector3(0f, 0.059727669f, 0.29259586f) ?

EDIT: I tried to calculate with CollisionWorld, but after this function

Code: Select all

world.contactPairTest(obA,obB,result);
in the result I couldn't find the distance between the shapes. Am I doing something wrong?

I reckon that I'm so close to the solution :) .

Thanks in advance!

Best Regards,
Zoli
MrOizo
Posts: 6
Joined: Wed Aug 18, 2010 6:44 pm

Re: Calculate 3D distance with Bullet Engine

Post by MrOizo »

I found something which is not correctly works:

Code: Select all

GjkPairDetector.ClosestPointInput closestPointInput = new DiscreteCollisionDetectorInterface.ClosestPointInput();

closestPointInput.TransformA = Matrix.Identity;
closestPointInput.TransformB = Matrix.CreateTranslation(new Vector3(0, 20, 0));
I debugged the last two lines, and after I set the TransformA or the TransformB, the values of them is the same, before I set them.

So I think, this is the reason, why I get wrong distance in the output, because this transforms my shapes into wrong places, instead of using my transform matrices.

Could You give me some idea, why is it happens? :(

Thanks,
Zoli
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Calculate 3D distance with Bullet Engine

Post by Erwin Coumans »

It is not clear what XNA version you are talking about, there are plenty of ports of the C++ version of Bullet to various languages, most are incomplete or broken.

Only the C++ version of Bullet is supported here.
Thanks,
Erwin
MrOizo
Posts: 6
Joined: Wed Aug 18, 2010 6:44 pm

Re: Calculate 3D distance with Bullet Engine

Post by MrOizo »

Erwin Coumans wrote:It is not clear what XNA version you are talking about, there are plenty of ports of the C++ version of Bullet to various languages, most are incomplete or broken.

Only the C++ version of Bullet is supported here.
Thanks,
Erwin
I'm talking about bulletsharp. It may be unfortunately broken :( .

I'll try to calculate it with another wrapper.

Regards,

Zoli
MrOizo
Posts: 6
Joined: Wed Aug 18, 2010 6:44 pm

Re: Calculate 3D distance with Bullet Engine

Post by MrOizo »

I calculated the distance with BulletX with the same way like I wrote before with BulletSharp, and it works well.

So what I wrote before is a bug in BulletSharp, that is the reason, why I couldn't set the TransfromA and TransformB value.
I notify who created BulletSharp wrapper, and he wrote me he had fixed it.

Thanks for the helps Erwin!

Best Regards,
Zoli