sphere and Trianglemesh collision

jjj
Posts: 3
Joined: Fri Apr 18, 2008 6:45 pm

sphere and Trianglemesh collision

Post by jjj »

BULLET is new to me. But can anyone tell me how to detect the collision between an sphere and a triangle mesh? I try to use btGjkPairDetector (as shown in CollisionDemo sample) to do that but it only handles convex to convex shape collision. Both sphere and triangle mesh are static in my case. Thanks.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: sphere and Trianglemesh collision

Post by Erwin Coumans »

You can check the ConcaveConvexcastDemo, and replace the box by a sphere.

If you want more low-level access, you need to create a mesh (btBvhTriangleMeshShape), a sphere (btSphereShape), and then instantiate a btConvexConcaveCollisionAlgorithm, and call the processCollision. Another low-level method is to create a mesh, a btOptimizedBvh, write a callback function that gets called for each triangle, and then call the sphere versus triangle using GJK.

Hope this helps,
Erwin
jjj
Posts: 3
Joined: Fri Apr 18, 2008 6:45 pm

Re: sphere and Trianglemesh collision

Post by jjj »

Thank you for your help, Erwin. I used the code in ConcaveConvexcastDemo and it works pretty well. In case anyone is interested. Here is the code:

bool bCheckSphereCollision(const D3DXVECTOR3& pt1, const D3DXVECTOR3& pt2, float radius, D3DXVECTOR3& norm, D3DXVECTOR3& ptInt)
{
btDiscreteDynamicsWorld* m_pWorld;
// initialize world

btSphereShape sphereShape(radius);
btQuaternion qFrom;
btQuaternion qTo;
qFrom.setRotation (btVector3(1.0, 0.0, 0.0), 0.0);
qTo.setRotation (btVector3(1.0, 0.0, 0.0), 0.0);
btVector3 source(pt2.x, pt2.y, pt2.z);
D3DXVECTOR3 delta = (pt1 - pt2) * 0.001f;
btVector3 dest(pt2.x + delta.x, pt2.y + delta.y, pt2.z + delta.z);
btTransform from(qFrom, source);
btTransform to(qTo, dest);
btCollisionWorld::ClosestConvexResultCallback cb(source, dest);
m_pWorld->convexSweepTest (&sphereShape, from, to, cb);
if (cb.HasHit ())
{
btVector3 hit_surface;
btVector3 normal;
hit_surface = cb.m_hitPointWorld;
normal = cb.m_hitNormalWorld;
normal.normalize ();
norm = D3DXVECTOR3(normal[0], normal[1], normal[2]);
ptInt = D3DXVECTOR3(hit_surface[0], hit_surface[1], hit_surface[2]);
return true;
}
return false;
}

I am also curious if we can have a function to test intersection between a triangle mesh and a static (not moving) sphere, similar to rayTest() in btDiscreteDynamicsWorld. Also, is there any way to gather triangles contained in a given rectangular box in a large triangle mesh?

Thanks, again.

Junlin Xu