Original Post
Hey everyone, a little quick question here. How do I test if a circle (2D) is intersecting with a triangle (2D)?
COLLIB2D_VECTOR2D cP = {A->X, A->Y}; // CircleCenter COLLIB2D_VECTOR2D tA = {B->X1, B->Y1}; // Triangle Points COLLIB2D_VECTOR2D tB = {B->X2, B->Y2}; COLLIB2D_VECTOR2D tC = {B->X3, B->Y3}; COLLIB2D_VECTOR2D cp1 = (tB-tA).Cross( cP-tA ); // First check: point on inside? COLLIB2D_VECTOR2D cp2 = (tB-tA).Cross( tC-tA ); if(cp1.Dot(cp2) >= 0) { cp1 = (tC-tB).Cross( cP-tB ); // Second check: point on inside? cp2 = (tC-tB).Cross( tA-tB ); if(cp1.Dot(cp2) >= 0) { cp1 = (tA-tC).Cross( cP-tC ); // Third check: point on inside? cp2 = (tA-tC).Cross( tB-tC ); if(cp1.Dot(cp2) >= 0) { return COLLIB2D_TRUE; } } } COLLIB2D_VECTOR2D v = {x*b.y-y*b.x, b.x*x-b.y*y}; // wrong COLLIB2D_VECTOR2D v = {x*b.y-y*b.x, b.x*y-b.y*x};if(cross(p - a, b - a) < 0.0f) return false;if(cross(p - b, c - b) < 0.0f) return false;if(cross(p - c, a - c) < 0.0f) return false;return true;Vector ab = b - a;float t = dot(p - a, ab) / dot(ab, ab);if(t < 0.0f) t = 0.0f;if(t > 1.0f) t = 1.0f;return t;Quote:If you're referring to Wasting Time's post, than yes it will work; it is exactly the correct solution.
Original post by fax
That actually wont work
Quote:
fax wrote
That actually wont work,
You need to check if the center of the circle is inside the triangle.
if its not then check if the distance from the center of the circle to each of the line segments that makes up the triangle is less than the radius of the circle.
Quote:
Demus79 wrote
The test may not be as trivial as it seems ie. distance of a point to a triangle. There are several things to be considered, distance to each of the edges and then distance to each of the corners.
You can draw a sketch on the paper and see that if you just use the distance to the edge, there are cases where the distance of the circle to the triangle is less than 0 (although the circle might be a mile away).
Quote:
If you're referring to Wasting Time's post, than yes it will work; it is exactly the correct solution.
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.
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; }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.
Quote:
Original post by Wasting Time
Your posted code involves 3D calculations. You did notice that "Pipo DeClown" asked about a 2D problem?
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.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more