# Detect Proximity algorithm?

Hi all - i’m working on a function to detect proximity of one mesh to another mesh. I’m wondering if there’s a handy way to do this that all of you pros know about offhand?

I’ve got it working in the PG below, but it’s using the center points of each mesh to detect proximity, which doesn’t work when one mesh is much larger than another.

I’m thinking that i need something like intersectsMesh, but where I can say `intersectsMesh, but extend both bounding boxes by a given distance`

Have tried a number of ways to do that, but can’t nail it. And since this function runs every frame, am conscious of keeping it performant (eg: i don’t want to make a clone of each mesh that is larger by distance and then run intersectsMesh on those clones).

Pro tips?

This is my current is proximate function

`````` CollideProximity.prototype.isWithinDistance = function (mesh1, mesh2, distance) {
// Calculate the bounding box for both meshes
mesh1.computeWorldMatrix(true);
var boundingBox1 = mesh1.getBoundingInfo().boundingBox;

mesh2.computeWorldMatrix(true);
var boundingBox2 = mesh2.getBoundingInfo().boundingBox;

// Calculate the center points of each bounding box
var center1 = boundingBox1.centerWorld;
var center2 = boundingBox2.centerWorld;

// Calculate the distance between the two center points
var diff = center1.subtract(center2);
var distanceBetween = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.y, 2) +
Math.pow(diff.z, 2));

// Return true if the distance is less than the specified distance
this.mlog.log('CollideProximity-centerIsWithinDistance', {'center1':center1, 'center2.name':center2, 'mesh2.name':mesh2.name, 'distanceBetween':distanceBetween}, 6);
return distanceBetween <= distance;
}
``````

Hey there!

There are a few ways to do that using bounding spheres and bounding boxes. For the spheres, the distance is just the distance between centers - the radius of the two spheres. The boxes case is a bit more involved because it needs to consider all the different sides box - Minimum distance between two axis-aligned boxes in n-dimensions - Stack Overflow
Calculating the shortest distance between two rectangles · Weixuan (weixuanz.github.io)

Thanks. I did try that first technique, but since the bounding boxes are not axis aligned, the character constantly falls in and out of proximity as it moves near the target (esp, when target is also moving).

That second post looks like it could work. Will give it a go and post a PG if i figure it out. Thx.

Does a secondary “proximity” collider work parented to the character?

Not sure about Havok, but Babylon can cope well with changing scales. So proximity can be dynamic.

That’s a good idea. Assuming i can remove other physics interactions from a physics shape. Will give that a try as well.

When it comes to simplicity and performance, you simply can’t go wrong with a basic distance check.
Take a look at how collision detection of two spheres is done. You only need the distance between the two spheres as well as the radii.