Example #1
0
 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 test_angleB(self):
     result = tda.vector_angle(x=0, y=0)
     self.assertEqual(result, 0)
Example #3
0
        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_angle9(self):
     result = tda.vector_angle(x=-10, y=10)
     self.assertEqual(result, 135)
 def test_angleA(self):
     result = tda.vector_angle(x=-10, y=-10)
     self.assertEqual(result, 225)
 def test_angle7(self):
     result = tda.vector_angle(x=-10, y=0)
     self.assertEqual(result, 180)
 def test_angle8(self):
     result = tda.vector_angle(x=10, y=-10)
     self.assertEqual(result, 315)
 def test_angle6(self):
     result = tda.vector_angle(x=0, y=-5)
     self.assertEqual(result, 270)
 def test_angle4(self):
     result = tda.vector_angle(x=0, y=3.5)
     self.assertEqual(result, 90)
 def test_angle3(self):
     result = tda.vector_angle(x=3, y=3)
     self.assertEqual(result, 45)
 def test_angle2(self):
     result = tda.vector_angle(x=2, y=4)
     self.assertEqual(result, 63)
 def test_angle1(self):
     result = tda.vector_angle(x=2, y=1)
     self.assertEqual(result, 26)