Game Development Community

Point in Polygon test

by DIAG · in Torque Game Engine · 03/25/2004 (4:16 am) · 1 replies

Hey guys,
i was just wondering. Is there a facilty in the Torque code which allows for point in polygon tests?
Cheers

#1
03/26/2004 (8:14 am)
I don't remember finding a built-in routine. Here is a piece of code from my current Torque game project that you should be able to adapt for your needs.

// What this does is goes along each side of the polygon and for each edge it creates
// a new polygon that is perpendicular to the original polygon.  Then we do a check
// with the point to see which side of the polygon it is on.  If the point is on the
// wrong side of any of the polys then it is not within the polygon.
//
bool GolfBall::pointWithinPoly(const ConcretePolyList::Poly &poly, const Point3F &point)
{

   Point3F v2 = polyList.mVertexList[polyList.mIndexList[poly.vertexStart + poly.vertexCount - 1]];

   for (U32 i = 0; i < poly.vertexCount; i++)
   {
      Point3F v1 = polyList.mVertexList[polyList.mIndexList[i + poly.vertexStart]];

      // build a plane normal for this polygon edge
      PlaneF p(v1 + poly.plane, v1, v2);
      v2 = v1;
      F32 dist = p.distToPlane(point);
      if (dist < 0)
         return(false);
   }

   return(true);
}