public bool Contains(ref Vector2 point)
{
// basic idea: draw a line from the point to a point known to be outside the body. count the number of
// lines in the polygon it intersects. if that number is odd, we are inside. if it's even, we are outside.
// in this implementation we will always use a line that moves off in the positive X direction from the point
// to simplify things.
Vector2 endPt = new Vector2();
endPt.X = aabb.max.X + 0.1f;
endPt.Y = point.Y;
// line we are testing against goes from pt -> endPt.
bool inside = false;
Vector2 edgeSt = pointmass_list[0].position;
Vector2 edgeEnd = new Vector2();
for (int i = 0; i < count; i++)
{
// the current edge is defined as the line from edgeSt -> edgeEnd.
if (i < (count - 1))
edgeEnd = pointmass_list[i + 1].position;
else
edgeEnd = pointmass_list[0].position;
// perform check now...
if (((edgeSt.Y <= point.Y) && (edgeEnd.Y > point.Y)) || ((edgeSt.Y > point.Y) && (edgeEnd.Y <= point.Y)))
{
// this line crosses the test line at some point... does it do so within our test range?
float slope = (edgeEnd.X - edgeSt.X) / (edgeEnd.Y - edgeSt.Y);
float hitX = edgeSt.X + ((point.Y - edgeSt.Y) * slope);
if ((hitX >= point.X) && (hitX <= endPt.X))
inside = !inside;
}
edgeSt = edgeEnd;
}
return inside;
}