def line_ball_collision_check(line, ball):
# checks if the ball is half the line length from the line middle
if distance_less_equal(line.middle, ball.pos, line.length / 2 + config.ball_radius):
# displacement vector from the first point to the ball
displacement_to_ball = ball.pos - line.line[0]
# displacement vector from the first point to the second point on the
# line
displacement_to_second_point = line.line[1] - line.line[0]
normalised_point_diff_vector = displacement_to_second_point / \
np.hypot(*(displacement_to_second_point))
# distance from the first point on the line to the perpendicular
# projection point from the ball
projected_distance = np.dot(normalised_point_diff_vector, displacement_to_ball)
# closest point on the line to the ball
closest_line_point = projected_distance * normalised_point_diff_vector
perpendicular_vector = np.array(
[-normalised_point_diff_vector[1], normalised_point_diff_vector[0]])
# checking if closest point on the line is actually on the line (which is not always the case when projecting)
# then checking if the distance from that point to the ball is less than the balls radius and finally
# checking if the ball is moving towards the line with the dot product
return -config.ball_radius / 3 <= projected_distance <= \
np.hypot(*(displacement_to_second_point)) + config.ball_radius / 3 and \
np.hypot(*(closest_line_point - ball.pos + line.line[0])) <= \
config.ball_radius and np.dot(
perpendicular_vector, ball.velocity) <= 0
评论列表
文章目录