1
Graphics / Re: Collision detection with concave vertex array (sf::TriangesFan)
« on: October 25, 2015, 05:52:38 pm »
I had a look at Boost and it looks great! A bit OTT for what I need though. I have taken the advice of Satus and separated the shape into smaller triangles (simply done as it was already an sf::TrianglesFan) and it works perfectly.
Looping through the triangle segments with the function below can quickly tell if the point is within it.
Thanks for the advice.
Looping through the triangle segments with the function below can quickly tell if the point is within it.
bool pointInTriangle(Point t1, Point t2, Point t3, Point p)
{
float d = ((t2.y-t3.y)*(t1.x-t3.x) + (t3.x-t2.x)*(t1.y-t3.y));
float a = ((t2.y-t3.y)*(p.x-t3.x) + (t3.x-t2.x)*(p.y-t3.y)) / d;
float b = ((t3.y-t1.y)*(p.x-t3.x) + (t1.x-t3.x)*(p.y-t3.y)) / d;
float c = 1 - a - b;
return 0 <= a && a <= 1 && 0 <= b && b <= 1 && 0 <= c && c <= 1;
}
{
float d = ((t2.y-t3.y)*(t1.x-t3.x) + (t3.x-t2.x)*(t1.y-t3.y));
float a = ((t2.y-t3.y)*(p.x-t3.x) + (t3.x-t2.x)*(p.y-t3.y)) / d;
float b = ((t3.y-t1.y)*(p.x-t3.x) + (t1.x-t3.x)*(p.y-t3.y)) / d;
float c = 1 - a - b;
return 0 <= a && a <= 1 && 0 <= b && b <= 1 && 0 <= c && c <= 1;
}
Thanks for the advice.