Public feed #893025

Someone

public text v1 · immutable
Raw Edit
·published 2008-02-05 21:00 UTC
rendered paste body
def _is_intersecting(objA, objB, positionA, positionB):
    for axis in objA.axises:
        normal_x, normal_y = (axis.point1.y-axis.point2.y,
                              axis.point2.x-axis.point1.x)
        # Normalize
        dist = hypot(normal_x, normal_y)
        normal_x /= dist
        normal_y /= dist
        
        # Project polygon A
        points = objA.points
        minA = maxA = (normal_x*points[0].x + normal_y*points[0].y)
        for point in points[1:]:
            temp = (normal_x*point.x + normal_y*point.y)
            if temp > maxA:
                maxA = temp
            elif temp < minA:
                minA = temp
        
        # Project polygon B
        points = objB.points
        minB = maxB = (normal_x*points[0].x + normal_y*points[0].y)
        for point in points[1:]:
            temp = (normal_x*point.x + normal_y*point.y)
            if temp > maxB:
                maxB = temp
            elif temp < minB:
                minB = temp
        
        # Offset object A's values to match the axis
        offsetA = normal_x*positionA.x + normal_y * positionA.y
        minA, maxA = minA+offsetA, maxA+offsetA
        
        # Offset object B's values
        offsetB = normal_x*positionB.x + normal_y * positionB.y
        minB, maxB = minB+offsetB, maxB+offsetB
        
        if ( maxA < minB ) or ( minA > maxB ):
            return False # Found a separating axis!
    return True

def is_colliding(objA, objB):
    offsetA = objA.state.position
    offsetB = objB.state.position
    return (_is_intersecting(objA, objB, offsetA, offsetB) and
            _is_intersecting(objB, objA, offsetB, offsetA))