pygame,检测旋转矩形的碰撞

时间:2020-01-01 14:01:07

标签: python pygame

我绘制了一个旋转的矩形,我需要检查它是否碰撞。 整个班级:

class Laser:
def __init__(self, player_x, player_y):
    self.x = player_x
    self.y = player_y
    self.original_image = pygame.Surface((2, 1000))
    self.original_image.set_colorkey( (0,0,0) )
    self.original_image.fill( (255,0,0) )
    self.copy_image = self.original_image.copy()
    self.copy_image.set_colorkey( (0,0,0) )
    self.rect = self.copy_image.get_rect()
    self.new_image = pygame.Surface((2, 1000))
    self.angle = 0

def continueDrawLaser(self):
    if laser_bool:
        screen.blit(self.new_image, self.rect)

def rotate(self):
    # get rectangle of player and laser, as if the angle would be 0
    player_rect = player1.original_player_image.get_rect(topleft=(player1.x, player1.y))
    laser_rect = self.original_image.get_rect(midbottom=player_rect.midtop)
    self.angle = player1.angle
    pivotPos = [player_rect.centerx - laser_rect.x, player_rect.centery - laser_rect.y]

    # calcaulate the axis aligned bounding box of the rotated image
    w, h = self.original_image.get_size()
    box = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
    box_rotate = [p.rotate(self.angle) for p in box]
    min_box = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
    max_box = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])

    # calculate the translation of the pivot
    pivot = pygame.math.Vector2(pivotPos[0], -pivotPos[1])
    pivot_rotate = pivot.rotate(self.angle)
    pivot_move = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (laser_rect.x + min_box[0] - pivot_move[0], laser_rect.y - max_box[1] + pivot_move[1])  #x,y

    # get a rotated image
    self.new_image = pygame.transform.rotate(self.original_image, self.angle)

    # get new rectangle
    self.rect = self.new_image.get_rect(topleft=origin)

这是碰撞函数:

#check if rock collides with laser
def collisionRockLaser(self, laser1):
    laser_rect = laser1.rect
    rock_rect = pygame.Rect(self.x, self.y, rock_width, rock_height)
    if laser_rect.colliderect(rock_rect):
        rocks.pop(rocks.index(rock2))
        global score
        score += 1

这就是我得到的:

enter image description here

我认为这足以通过self.rect,因为它每次都会随着旋转的位置进行更新以检测碰撞,但是似乎我必须使用分离轴定理,您能帮我吗?

1 个答案:

答案 0 :(得分:1)

一种选择是创建与直线和矩形相交的算法。

首先创建一个与2个线段相交的算法:

P     ... point on the 1. line
R     ... normalized direction of the 1. line

Q     ... point on the 2. line
S     ... normalized direction of the 2. line

alpha ... angle between Q-P and R
beta  ... angle between R and S

X     ... intersection point
t     ... distance between P and X
u     ... distance between Q and X

gamma  =  180° - alpha - beta

t  = | Q - P | * sin(gamma) / sin(beta)
u  = | Q - P | * sin(alpha) / sin(beta)

t  =  dot(Q-P, (S.y, -S.x)) / dot(R, (S.y, -S.x))  =  determinant(mat2(Q-P, S)) / determinant(mat2(R, S))
u  =  dot(Q-P, (R.y, -R.x)) / dot(R, (S.y, -S.x))  =  determinant(mat2(Q-P, R)) / determinant(mat2(R, S))

X  =  P + R * t  =  Q + S * u

您必须评估距离tu是否为正且小于相应留置线段的长度:
(该算法使用pygame.math.Vector2

def collideLineLine(l1_p1, l1_p2, l2_p1, l2_p2):

    # normalized direction of the lines and start of the lines
    P  = pygame.math.Vector2(*l1_p1)
    line1_vec = pygame.math.Vector2(*l1_p2) - P
    R = line1_vec.normalize()
    Q  = pygame.math.Vector2(*l2_p1)
    line2_vec = pygame.math.Vector2(*l2_p2) - Q
    S = line2_vec.normalize()

    # normal vectors to the lines
    RNV = pygame.math.Vector2(R[1], -R[0])
    SNV = pygame.math.Vector2(S[1], -S[0])

    # distance to the intersection point
    QP  = Q - P
    t = QP.dot(SNV) / R.dot(SNV)
    u = QP.dot(RNV) / R.dot(SNV)

    return t > 0 and u > 0 and t*t < line1_vec.magnitude_squared() and u*u < line2_vec.magnitude_squared()

要测试线段是否与矩形相交,必须测试其是否与矩形的4个边相交:

def colideRectLine(rect, p1, p2):

    return (collideLineLine(p1, p2, rect.topleft, rect.bottomleft) or
            collideLineLine(p1, p2, rect.bottomleft, rect.bottomright) or
            collideLineLine(p1, p2, rect.bottomright, rect.topright) or
            collideLineLine(p1, p2, rect.topright, rect.topleft))

根据角度在激光上设置一条线:

angle = player1.angle
if angle < 0:
    angle += 360

if angle < 90 or (angle > 180 and angle < 270):
    laserline = [laser1.rect.topleft, laser1.rect.bottomright]
else:
    laserline = [laser1.rect.bottomleft, laser1.rect.topright]

在该行中求值与pygame.Rect enemy_rect相交:

collide = colideRectLine(enemy_rect, *laserline)