Collision Detection between a line and a circle in python(tkinter) Collision Detection between a line and a circle in python(tkinter) tkinter tkinter

Collision Detection between a line and a circle in python(tkinter)


To be correct we are not speaking not about lines, but rather segments.

I would suggest the following idea:

Since the ball is moving in some direction, the only points that might collide with something lie on a 180° arc - the part that is moving forward. Meaning at some point of time when you check for collision you have to check whether any of those points collided with something. The more points you check, the better the precision of the collision in time, but worse the complexity.

Checking the collision: you check whether any of the points is in between the extremes of the segment. You can do this by first checking the coordinates (example is given looking at your drawn line, meaning A.x < B.x and A.y > B.y) if (A.x <= point.x <= B.x && A.y >= point.y >= B.y if the condition satisfies, you check whether the 3 points form a line. Since you have already the coordinates of A and B you can deduce the equation of the line and check whether the point satisfies it.

In short: you check if the point satisfies the equation of the line and is inside the rectangle defined by the 2 points.

How to get the points you have to check: assuming 2k+1 is the number of points you want to check at some time, C is your center r the radius and V the vector of motion. Then the number of points from the left side of the direction vector and from the right side will be equal and be k (+1 point at the intersection of the circle and the motion vector). Then 90° / k is one angular division. Since you know the motion vector, you can calculate the angle between it and the horizontal line (let it be angle). You keep adding to go left and decrementing to go right from the motion vector the value of 90° / k exactly k times (let us denote this value by i) and calculate the position of the point by point.x = C.x + sin(i) * r and point.y = C.y + cos(i) * r.

Sry, I don't know python.


The shortest distance from a circle to a line is the shortest distance from its center to that line, minus the radius of the circle. If the distance from the center to the line is less than the radius, the line passes through the circle.

Finding the distance from a point to a line is documented many places, including here.

Sorry for not posting Python code, but it is pretty basic.