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

Point collisions

Point versus point

If two points are the same, they collide. You are probably looking for something with a little more give, like a circle vs circle collision.

See Circle Collision 2D

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 line

Point versus triangle

Return to Collision Detection 2D

Clone this wiki locally