Example #1
0
    def shadow(self, n):
        '''Retorna as coordenadas da sombra na direção n dada.
        Assume n normalizado.'''

        r = self.radius
        center_pos = dot(self.pos, n)
        return (center_pos - r, center_pos + r)
Example #2
0
    def shadow(self, n):
        '''Retorna as coordenadas da sombra na direção n dada.
        Assume n normalizado.'''

        r = self.radius
        center_pos = dot(self.pos, n)
        return (center_pos - r, center_pos + r)
Example #3
0
def collision_poly(A, B, directions=None, collision_class=Collision):
    """
    Collision detection using SAT.
    """

    # List of directions from normals
    if directions is None:
        if A.num_normals + B.num_normals < 9:
            directions = A.get_normals() + B.get_normals()
        else:
            directions = DEFAULT_DIRECTIONS

    # Test overlap in all considered directions and picks the smaller
    # penetration
    min_overlap = float('inf')
    norm = None
    for u in directions:
        A_coords = [dot(pt, u) for pt in A.vertices]
        B_coords = [dot(pt, u) for pt in B.vertices]
        Amax, Amin = max(A_coords), min(A_coords)
        Bmax, Bmin = max(B_coords), min(B_coords)
        minmax, maxmin = min(Amax, Bmax), max(Amin, Bmin)
        overlap = minmax - maxmin
        if overlap < 0:
            return None
        elif overlap < min_overlap:
            min_overlap = overlap
            norm = u

    # Finds the correct direction for the normal
    if dot(A.pos, norm) > dot(B.pos, norm):
        norm = -norm

    # Computes the clipped polygon: collision happens at its center point.
    try:
        clipped = clip(A.vertices, B.vertices)
        col_pt = center_of_mass(clipped)
    except ValueError:
        return None

    if area(clipped) == 0:
        return None

    return collision_class(A, B, pos=col_pt, normal=norm, delta=min_overlap)
Example #4
0
def collision_poly(A, B, directions=None, collision_class=Collision):
    """
    Collision detection using SAT.
    """

    # List of directions from normals
    if directions is None:
        if A.num_normals + B.num_normals < 9:
            directions = A.get_normals() + B.get_normals()
        else:
            directions = DEFAULT_DIRECTIONS

    # Test overlap in all considered directions and picks the smaller
    # penetration
    min_overlap = float('inf')
    norm = None
    for u in directions:
        A_coords = [dot(pt, u) for pt in A.vertices]
        B_coords = [dot(pt, u) for pt in B.vertices]
        Amax, Amin = max(A_coords), min(A_coords)
        Bmax, Bmin = max(B_coords), min(B_coords)
        minmax, maxmin = min(Amax, Bmax), max(Amin, Bmin)
        overlap = minmax - maxmin
        if overlap < 0:
            return None
        elif overlap < min_overlap:
            min_overlap = overlap
            norm = u

    # Finds the correct direction for the normal
    if dot(A.pos, norm) > dot(B.pos, norm):
        norm = -norm

    # Computes the clipped polygon: collision happens at its center point.
    try:
        clipped = clip(A.vertices, B.vertices)
        col_pt = center_of_mass(clipped)
    except ValueError:
        return None

    if area(clipped) == 0:
        return None

    return collision_class(A, B, pos=col_pt, normal=norm, delta=min_overlap)
Example #5
0
 def shadow(self, n):
     p0 = dot(self.pos, n)
     r = self._radius
     return p0 - r, p0 + r
Example #6
0
 def sat_shadow(self, n):
     if abs(dot(self.tangent(), n)) < 1e-6:
         p = dot(self.p1, n)
         return p, p
     else:
         return [-Inf, Inf]
Example #7
0
 def sat_shadow(self, n):
     if abs(dot(self.tangent(), n)) < 1e-6:
         p = dot(self.p1, n)
         return p, p
     else:
         return [-Inf, Inf]
Example #8
0
 def SAT_shadows(self, n):
     points = [dot(n, p) for p in self.vertices]
     return min(points), max(points)
Example #9
0
    def shadow(self, n):
        '''Retorna as coordenadas da sombra na direção n dada.
        Assume n normalizado.'''

        points = [dot(n, p) for p in self.vertices]
        return min(points), max(points)
Example #10
0
 def SAT_shadows(self, n):
     points = [dot(n, p) for p in self.vertices]
     return min(points), max(points)