Search code examples
c++distancebulletphysics

Miminum distance between objects in Bullet Physics


Is it possible to calculate the minimum distance between two CollisionObjects in Bullet Phyics?

How about concave objects? Perhaps it requires Convex Decomposition?

Even point-to-object distance calculator would be useful.


Solution

  • As author of the library informs, there is no positive distance information in Bullet link. It would be easy to extract this information from collision tests, but you would need to modify Bullet collision detection on your own. There are many collision algorithms for different pairs of shapes, but every one of them processes the information about distance at some stage.

    However, there is another recommended way of obtaining the approximate distance between two objects. You can do it by performing a ray test from the center of the first object to the center of the second one, store the collision point (it should be located on the second object's hull). Next you perform the reverse test (from second object to the first one) and calculate the distance between two stored points. Of course you need to check whether the hit object is actually the one you want to calculate the distance to. It won't work if there is an obstacle between them.

    To perform the ray test, you need a rayTest(const btVector3 &rayFromWorld, const btVector3 &rayToWorld, RayResultCallback &resultCallback) method of btCollisionWorld. The example of usage can be found here.

    If you want it to be more accurate, instead of casting a single ray, you can perform swept sphere test using convexSweepTest method.

    You need to note, that these methods provide only approximate sollution because there is no guarantee, that the hit point is the closest one. However they are sufficient for most applications.