Skip to content
Zorro edited this page Nov 6, 2021 · 7 revisions

Point collisions

Point versus AABB

To check if a point is within an axis-aligned bounding box is straight forward. To collide it must be within the shape, and hence within the boundaries of the rectangle.

bool PointInsideAABB(double x1 , double y1 , Rectangle r) {
   return (!(x1 < r.x ||
             y1 < r.y ||
             x1 > r.rx ||
             y1 > r.by));
}

Point versus circle

To check if a point is within a circle is simple. All you do is check whether the distance between the point and the center of the circle is less than or equal to the radius of the circle. To simplify the calculation we use the distance squared versus the radius squared.

bool PointInsideCircle(double x1 , double y1 , double cx , double cy , double cradius) {
   const double dsq = (cx - x1)*(cx - x1) + (cy - y1)*(cy - y1);
   return dsq <= cradius*cradius;
}

Point versus triangle

Clone this wiki locally