def check_ball_ball_contact(ball1: Ball, ball2: Ball) -> [bool, int]: contactDistance = ball1.xRelation + ball2.xRelation # xRelation is radius [x, y] = tda.vectorize(point1=ball1.get_position(), point2=ball2.get_position()) ballDistance = tda.vector_length(x, y) if not ballDistance > contactDistance: return [True, 90 + tda.vector_angle(x, y)] return [False, 0]
def check_ball_vertex_contact(centre, vertex, radius) -> bool: [x, y] = tda.vectorize(point1=centre, point2=vertex) distance = int(tda.vector_length(x=x, y=y)) return not (distance > radius)
def check_ball_block_contact(ball: Ball, block: Block) -> [bool, int]: def check_ball_vertex_contact(centre, vertex, radius) -> bool: [x, y] = tda.vectorize(point1=centre, point2=vertex) distance = int(tda.vector_length(x=x, y=y)) return not (distance > radius) def check_ball_edge_contact(centre, radius, linePoint1, linePoint2): distance = abs( tda.distance_point_line(point=centre, linePoint1=linePoint1, linePoint2=linePoint2)) return not (distance > radius) contactDetected = False normalToSurface = 0 [referencePoint, oppositePoint] = block.get_limits() center = ball.get_position() ranges = ball.get_ranges() ballRadius = ranges[0] blockVertex = [ referencePoint, [oppositePoint[0], referencePoint[1]], oppositePoint, [referencePoint[0], oppositePoint[1]] ] if referencePoint[0] <= center[0] <= oppositePoint[0]: contactDetected = ( check_ball_edge_contact(centre=center, radius=ballRadius, linePoint1=blockVertex[3], linePoint2=blockVertex[2]) or check_ball_edge_contact(centre=center, radius=ballRadius, linePoint1=blockVertex[1], linePoint2=blockVertex[0])) if contactDetected: [x, y] = tda.vectorize(point1=blockVertex[0], point2=blockVertex[1]) normalToSurface = tda.vector_angle(x=x, y=y) return [True, normalToSurface] elif referencePoint[1] <= center[1] <= oppositePoint[1]: contactDetected = ( check_ball_edge_contact(centre=center, radius=ballRadius, linePoint1=blockVertex[0], linePoint2=blockVertex[3]) or check_ball_edge_contact(centre=center, radius=ballRadius, linePoint1=blockVertex[1], linePoint2=blockVertex[2])) if contactDetected: [x, y] = tda.vectorize(point1=blockVertex[1], point2=blockVertex[2]) normalToSurface = tda.vector_angle(x=x, y=y) return [True, normalToSurface] else: for point in blockVertex: if check_ball_vertex_contact(centre=center, vertex=point, radius=ballRadius): [x, y] = tda.vectorize(point1=center, point2=point) normalToSurface = 90 + tda.vector_angle(x=x, y=y) return [True, normalToSurface] return [contactDetected, normalToSurface]
def test_vectorize3(self): result = tda.vectorize([3, 3], [1, 1]) self.assertEqual(result, (-2, -2))
def test_vectorize2(self): result = tda.vectorize([1, 1], [3, 3]) self.assertEqual(result, (2, 2))
def test_vectorize1(self): result = tda.vectorize([0, 0], [2, 2]) self.assertEqual(result, (2, 2))