- Home /
Collidiers intersection
Is there a way to get point where colliders intersect. For example it would be two points for two circle colliders. To write it better, points where lines of colliders boundary intersect.
I need that for my project and can't find a way to do this
i'm not sure if this is possible with unity colliders, but you could always just use math.
for example the math to find the intersections of two circles is pretty well-known. Check out "Intersection of two circles" here. note that two circles can have either infinite, 2, 1, or 0 points of intersection. with 1 and 0 points of intersection there two cases: either one circle is inside the other, or they're separate. infinite is obviously if the two circles are identical.
if your colliders are all either simple polygons or circles, you could do the math for those cases. eg, polygon-vs-polygon can be done by testing line-segment intersection for each pair of line segments in the two polys. .. a good optimization here is to pre-test with a bounding circle. for polygon-vs-circle, you can do line-segment vs. circle for each line-segment in the polygon, and so on.
Answer by the_genius · May 01, 2018 at 07:15 PM
I am assuming you have two 3D mesh colliders (or just meshes) colliding.
I doubt you will find an efficient solution to this. Even approximating it might be hard. I'll suggest two simple methods:
Method 1: (exact, inefficient, simple)
Loop through pairs of triangles. One triangle on one mesh, the other triangle on the other. (You could improve this by finding bounding boxes of each triangle and only going further when they overlap. This will improve performance)
Using the normals to the triangles and the coordinates of one vertex on each, you can treat them as a planes. Then you need to find the intersection of the two planes. (This is simple mathematically with 3 cases:
1. The planes lie exactly on each other - Then every point on the two planes are points of intersection. But you only want the points of intersection which lie in both triangles. In this case you can reduce it to finding points of intersection of two triangles in 2D.
2. The planes are parrallel - No points of intersection if we are not in case 1 - You can easily check this case by checking if the normals are the same (or only differ by sign) and the reference point on one plane does not lie on the other.
3. The planes have a line of intersection - Its easy to find the line of intersection (do a google search). You'll want to parameterise the line of intersection and then find in which intervals the parametrisation is on each triangle (if at all). If these intervals overlap that is where you'll find your points of intersection.
It looks like there is a lot to implement here but if you know your maths its really not that hard. Bear in mind this method will be very inefficient for large meshes.
Method 2: (approximation, probably faster, might not always work)
Firstly you need some kind of initial guess for a point of intersection. Maybe halfway between the centers of the two meshes.
Use something like Collider.ClosestPoint or Physics.ClosestPoint to find the closest point to your initial guess on mesh 1. From this point find the nearest point on mesh 2. From this point find the nearest point on mesh 1 ..... Keep repeating and pray your points converge to a point of intersection.
You'll need to do this multiple times with different initial guesses if you want to find more than one point of intersection.
Neither of these Methods are particularily good. If you find a better solution it would be great if you could share it!
I'm using only 2D colliders. $$anonymous$$ostly circles at the moment.
Your answer
Follow this Question
Related Questions
Why when i move the player object through the door the ontriggerenter/exit event are not fire ? 2 Answers
Multiple Cars not working 1 Answer
Reset a collider during runtime to lose previous Physics.IgnoreCollision() calls 1 Answer
Distribute terrain in zones 3 Answers
Multiple Colliders On A Single Object Detecting Each Collider In OnTriggerEnter() 1 Answer