Find out if the centre of a shape is within other shape pygame

Question:

I am trying to make a realistic holes, for a pool game. What I would like to do is find out if the center of a ball intersects with the holes shape (which is also a circle) and so count it as potted. I can find the center of a ball using ball.position, but can’t figure out a way of finding out if it that is within the holes shape. How would I do that?

Answers:

I’m not well versed in pygame, but the math is the same regardless. What you want to do is to check if the distance between the the center of the ball and the center of the hole is less than the radius of the hole. Here’s some pseudocode as an example:

# the standard (euclidian) distance formula
def distance(a, b):
    return sqrt((b.x - a.x)**2 + (b.y - a.y)**2)

if distance(ball.pos, hole.pos) < hole.radius:
    # the ball is inside the hole
Answered By: Michael M.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.