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;
}