Hi all… I’ve tried googling for this and what I find is just baffling! Most of the websites assume advanced maths knowledge which I don’t have. Does anyone have to hand a fast Unity/Javascript example of a test for sphere/triangle intersection, given three points for the triangle, and a point+radius for the sphere?
What type of test do you need? Boolean only? Discrete? Continuous? Do you need any additional info, such as the collision point or normal?
Also, how will the test be used?
All I need to know is IF they intersect. I have a triangle shaped wall, and I have a sphere for a blast radius of a bomb, and I want to check if the triangle intersects the sphere, upon which I do more thorough testing to see what damage occurs to the wall. At the moment I’m checking with simple renderer.bounds, using:
if (wall.renderer.bounds.Contains(blastPos)  wall.renderer.bounds.SqrDistance(blastPos) < blastRad ) {dostuff}
which is quick and easy but not accurate  some of the big blasts that should affect walls don’t affect them for some reason.
I’ve just been looking into the Plane class, but I’m unstuck when I come to:
ray = Ray(pp, pl.normal);
var ent : float = 100.0;
if (plane.Raycast( ray, ent )) {
transform.position = spherepos+plane.normal*ent;
}
For some reason I get the error: "Method not found: ‘UnityEngine.Plane.Raycast’ "?
You may already know this, but Physics.OverlapSphere() will return all physics objects whose bounding volume intersects the query sphere. Even though that doesn’t give you the accuracy you need, you could at least use it as a broad phase query.
If you can’t find a way to get the physics engine to perform the spheretriangle test for you (and I can’t think of a way to do that off the top of my head), a discrete spheretriangle test is easy enough to code manually.
The basic idea is to find the point on the triangle closest to the center of the sphere; if the (squared) distance between these two points is <= the (squared) radius of the sphere, there’s an intersection.
The only tricky part is finding the closest point on a triangle to a point. There’s a nice method based on quadratic minimization, but it’s a little hard to explain in a forum post. Here’s another method, which is a little easier to explain:

Project the query point onto the triangle plane.

If the projected point lies in the triangle, that’s your closest point.

Else find the closest point on each of the three triangle edges to the query point. Whichever of these is closest to the query point is the closest point.
There are few mathematical details to contend with there, but that’s the basic idea.
Thanks! That’s enough to be getting on with, I’ll try to get my head round it.