Circle in Triangle intersection

Started by
11 comments, last by fax 17 years, 8 months ago
Quote:If you're referring to Wasting Time's post, than yes it will work; it is exactly the correct solution.

Wasting Time's solution will not work in some cases, firstly a triangle has hundreds of different centers but if you chose any fixed point inside the triangle then the circle can be positioned in such a way that it is intersecting the tringle but the algorithm will not return true, for example: if you had a long thin triangle (longer than the diameter of the circle) and the circle was close to the further away vertex.

Quote:The standard implementation of point-to-triangle distance assumes the triangle is a "solid". You are treating the triangle as if it is a wireframe. In the standard implementation, if the point is inside (or on) the triangle, the distance is zero.

I dont actually suggest using the distance from the center of the circle to the triangle for the collision detection test.
What I was describing takes first a point/triangle test then a circle/wireframe triangle test which in combination handles every case.

Since some code would probably make more sense than my explanation heres my implementation of what I was describing from line 12 onwards:

	int SphereIntersectsTriangle(CVector sphere_center, float radius, CVector A, CVector B, CVector C, CVector N, float d) {		float k = d-sphere_center.Dot(N);		CVector center_projected_onto_triangle = sphere_center+N*k;				float radius_at_projected_point_squared = radius*radius-(sphere_center-center_projected_onto_triangle).Norm();				// Check that the sphere intersects the triangles plane		if(radius_at_projected_point_squared < 0)			return 0;						////////////////// From here on its just circle a triangle intersection test:				// Check if the point is inside the triangle		if(BPPointInTriangle(center_projected_onto_triangle, A, B, C))			return 1;				// check if the projected point is close enough to any of the triangles edges		k = ClosestPointOnLineToPoint(A, B, center_projected_onto_triangle);		if(k < 0)			k = 0;		else if(k > 1)			k = 1;		if((A + (B-A)*k - center_projected_onto_triangle).Norm() < radius_at_projected_point_squared)			return 1;				k = ClosestPointOnLineToPoint(B, C, center_projected_onto_triangle);		if(k < 0)			k = 0;		else if(k > 1)			k = 1;		if((B + (C-B)*k - center_projected_onto_triangle).Norm() < radius_at_projected_point_squared)			return 1;				k = ClosestPointOnLineToPoint(C, A, center_projected_onto_triangle);		if(k < 0)			k = 0;		else if(k > 1)			k = 1;		if((C + (A-C)*k - center_projected_onto_triangle).Norm() < radius_at_projected_point_squared)			return 1;				return 0;	}
Advertisement
fax, you still are not getting it.

Treating the triangles as a solid (= interior + edges), the distance from a point to the triangle will always give you enough information to determine if the circle and triangle overlap. A triangle might have "hundreds of different centers" but none of these have anything to do with computing the distance from a point to the triangle. When the point is inside the triangle, the distance algorithm returns zero, which is effectively a point-in-triangle test.

Your posted code involves 3D calculations. You did notice that "Pipo DeClown" asked about a 2D problem?

You wrote: "I dont actually suggest using the distance from the center of the circle to the triangle for the collision detection test.
What I was describing takes first a point/triangle test then a circle/wireframe triangle test which in combination handles every case."

Checking if a point is inside the triangle and, if it is not, computing the "circle/wireframe" test *IS* equivalent to computing the distance from the center of the circle to the triangle.
I believe that the original post said:
Quote:Original post by Wasting Time
Compute the distance from the circle to the center of the triangle. If that distance is smaller than the circle radius, then the circle and triangle intersect.

but was fixed after I pointed out the mistake, sorry for the misunderstanding.

Quote:Original post by Wasting Time
Your posted code involves 3D calculations. You did notice that "Pipo DeClown" asked about a 2D problem?

I realise this which is why I said to read from line 12 onwards, I just left the rest in for context, but the 3D code is applicable to 2D (just set the z component of all the vectors to 0).

Quote:Original post by Wasting Time
Checking if a point is inside the triangle and, if it is not, computing the "circle/wireframe" test *IS* equivalent to computing the distance from the center of the circle to the triangle.

Its only equivalent to doing that and checking the radius is greater than or equal to that distance, because distance is never calculated unless required in the method I suggested.

This topic is closed to new replies.

Advertisement