New to bullet, need convex hull help!!!

damanb
Posts: 3
Joined: Mon Jun 24, 2013 1:32 am

New to bullet, need convex hull help!!!

Post by damanb »

Hello all,

As I said I am VERY new to bullet and having some trouble with a project that I was hoping to use bullet for.

I have a set of points which define a convex hull. I need to check that hull against another point. I need to check if that other point is inside or outside the hull (possibly returned as a bool). I saw that btConvexHullShape has a method isInside but in the cpp it is not defined and returns false so I cannot use it as it is....

After determining if a point is inside or outside, I need to find a distance. If inside, the penetration depth, and if outside the distance to the closest point on the hull. I know gjk is an algorithm to find distance if point is outside but not for penetration depths.

I've seen that bullet has a calcPenDepth function in the GJK pair detector but I don't understand the arguments passed to it.

Is there a tutorial to do something like I'm looking for or if someone has some examples it would be GREATLY appreciated.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: New to bullet, need convex hull help!!!

Post by Basroil »

Try the convex hull distance demo, it shows you how to make the hull and call the distance detection methods so you can see the closest distance(s). Negative distance means inside the hull, so quite easy to detect that way (though significantly slower than easier assumptions).
damanb
Posts: 3
Joined: Mon Jun 24, 2013 1:32 am

Re: New to bullet, need convex hull help!!!

Post by damanb »

Thanks, after taking a look at that demo I tried a little snippet of code on my own but am getting strange results....

Code: Select all

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#include "LinearMath/btQuaternion.h"
#include "LinearMath/btTransform.h"
#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
#include "BulletCollision/CollisionShapes/btConvexHullShape.h"
#include "BulletCollision/CollisionShapes/btCylinderShape.h"

#include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
#include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
#include "BulletCollision/NarrowPhaseCollision/btPointCollector.h"
#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
#include "BulletCollision/NarrowPhaseCollision/btConvexPenetrationDepthSolver.h"
#include "LinearMath/btIDebugDraw.h"
#include "LinearMath/btConvexHullComputer.h"

// In Main
btConvexShape* shapePtr[4];
btTransform tr[2];

btMatrix3x3 basisA; basisA.setIdentity();
btMatrix3x3 basisB; basisB.setIdentity();
tr[0].setBasis(basisA); tr[1].setBasis(basisB);
tr[0].setOrigin(btVector3(0.0,0.0,0.0)); 
tr[1].setOrigin(btVector3(0.0,0.0,0.0));

btVector3 testPoint(4.0,0.0,0.0);
btConvexHullShape hullA; hullA.addPoint(testPoint);

btVector3 hullPoints[4] = {btVector3(0.0,0.0,0.0),btVector3(2.0,0.0,0.0),btVector3(2.0,2.0,0.0),btVector3(0.0,2.0,0.0)};
btConvexHullShape hullB;	hullB.addPoint(hullPoints[0]); hullB.addPoint(hullPoints[1]);
					hullB.addPoint(hullPoints[2]); hullB.addPoint(hullPoints[3]);

shapePtr[0] = &hullA; shapePtr[1] = &hullB;

static btVoronoiSimplexSolver sGjkSimplexSolver;
btSimplexSolverInterface& gGjkSimplexSolver = sGjkSimplexSolver;

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

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

convexConvex.getClosestPoints(input, gjkOutput,0);
std::cout << "Has result = " << gjkOutput.m_hasResult << "\n";
std::cout << "Distance = " << gjkOutput.m_distance << "\n";
int dummy; std::cin >> dummy;
In short, I tried making a hull that is a 2d square with vertices at (0,0), (2,0), (2,2), (0,2) and a test point at (4,0). The result from gjkOutput.m_distance is 1.92.

I also tried testing a point in the hull at (1,1) which should have a distance of -sqrt(2) but m_distance was -0.07(...)
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: New to bullet, need convex hull help!!!

Post by Basroil »

Bullet defaults to 0.04 unit margins on hulls and such, so sounds spot on actually, at least for external point. As for the internal point, I think you were thinking the distance checks work only on vertices, but it works between vertices as well, so it wouldn't be -sqrt(2).