physics.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:pool 作者: max-kov 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号