问题SAT SAT碰撞检测算法(圆圈)

问题描述 投票:0回答:1
def Axis(A, B): # RETURN A NORMALIZED NORMAL VECTOR (CHECKED)
    dx = A[0] - B[0]
    dy = A[1] - B[1]
    n = [-dy, dx]
    v = math.sqrt(dy ** 2 + dx ** 2)
    return [n[0] / v, n[1] / v]

def projections(A, B, E, r): #Should return projections perpendicular line of the edge of the 2 vertices
    n = Axis(A, B)
    A_proj, B_proj, E_proj = A[:], B[:], E[:]
    a = A_proj[0] * n[0] + A_proj[1] * n[1]
    b = B_proj[0] * n[0] + B_proj[1] * n[1]
    e = E_proj[0] * n[0] + E_proj[1] * n[1]

    F = [E_proj[0] + r * n[0], E_proj[1] + r * n[1]]
    G = [E_proj[0] - r * n[0], E_proj[1] - r * n[1]]

    for i in range(2):
        A_proj[i] = a * n[i]
        B_proj[i] = b * n[i]
    return [A_proj, B_proj, G, F]

def check_overlap(pos):
    # Extraction des points
    (x1, y1), (x2, y2) = pos[0], pos[1]
    (x3, y3), (x4, y4) = pos[2], pos[3]
    # Fonction pour vérifier si deux segments sont alignés et se chevauchent
    def is_between(a, b, c):
        return min(a, b) <= c <= max(a, b)
    # Vérification si les segments sont alignés sur le même axe (horizontal, vertical ou diagonal)
    if (x1 == x2 == x3 == x4):  # vertical alignement
        return is_between(y1, y2, y3) or is_between(y1, y2, y4) or is_between(y3, y4, y1) or is_between(y3, y4, y2)
    return is_between(x1, x2, x3) or is_between(x1, x2, x4) or is_between(x3, x4, x1) or is_between(x3, x4, x2) #if no vertical alignement

def collision_check(vertices,E, r):
    vertices_proj = [v[:] for v in vertices]
    for i in range(len(vertices)):
        if i == (len(vertices)-1) :
            A = vertices_proj[i]
            B = vertices_proj[0]
        else :
            A = vertices_proj[i]
            B = vertices_proj[(i + 1)]
        pos = projections(A, B, E, r) #points = [[xa,ya],[xb,yb],[xf,yf],[xg,yg]]
        if check_overlap(pos) :
            return True
    return False
我正在做一个高尔夫游戏,所以我试图为多边形和圆圈制作碰撞检测算法(SAT)。仅当形状大致在水平轴上时,该代码才能检测到碰撞(在这里工作正常)。但是,在垂直轴上,无论距离如何,它们都被认为是在触摸(只要一个形状的像素在另一个形状之间是在另一个形状之间)。 我正在尝试更改check_overlaps中的条件,但是现在我看不到任何问题,所以我不知道它在哪里

我认为重叠的条件是x在x中重叠,在y中重叠。
python vector graphics simulation collision-detection
1个回答
0
投票

4个浮点数非常不可能是相同的 您想在所有情况下检查Y条件

我没有测试案例可以尝试,但我建议尝试尝试

    def check_overlap(pos): (x1, y1), (x2, y2) = pos[0], pos[1] (x3, y3), (x4, y4) = pos[2], pos[3] def is_between(a, b, c): return min(a, b) <= c <= max(a, b) y_overlap = is_between(y1, y2, y3) or is_between(y1, y2, y4) or is_between(y3, y4, y1) or is_between(y3, y4, y2) x_overlap = is_between(x1, x2, x3) or is_between(x1, x2, x4) or is_between(x3, x4, x1) or is_between(x3, x4, x2) return y_overlap and x_overlap
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.