所以我有一个障碍物,我希望我的精灵与之碰撞,并且它处于特定的角度。在这种情况下,我们从正x轴测量到矩形顶部,在这种情况下,相对于正x轴为333.02度或相对于负y轴为63.02度。所以我的问题是如何设置pygame精灵以正确地与角度矩形障碍物碰撞?据我所知,pygame精灵矩形没有旋转属性,我不能只说,“嘿,当我的精灵的右上角与顶部碰撞时”,因为缺乏旋转。我的碰撞在水平甚至垂直表面上运作良好,但我卡在如何与倾斜的障碍物碰撞上。
这是我现在的碰撞代码。它使用向量并独立检查x和y是否有任何东西。下面是我想要在Tile Map Editor中创建的对象的图片。就像我之前提到的那样,它以333.02度的角度放置。我还包括了坐标轴的草图,以防有关联。
def update(self):
self.animate()
self.acc = vec(0, PLAYER_MASS * GRAVITY)
self.move()
# Equations of Motion
self.acc.x += self.vel.x * PLAYER_FRICTION
self.vel += self.acc
# Collision check in all 4 directions
self.pos.x += (
self.vel.x + 0.5 * self.acc.x * self.game.dt
) # Update x component (Frame-independent motion)
if abs(self.vel.x) < PLAYER_VELX_EPSILON:
self.vel.x = 0
self.rect.x = self.pos.x
hits = pg.sprite.spritecollide(self, self.game.platforms, False)
for hit in hits: # Horizontal collision
if self.vel.x > 0: # Rightward motion
self.rect.right = hit.rect.left
elif self.vel.y < 0: # Leftward motion
self.rect.left = hit.rect.right
self.pos.x = self.rect.x # Update true postion
self.pos.y += self.vel.y + 0.5 * self.acc.y * self.game.dt # Update y component
self.rect.y = self.pos.y + 5
# This prevents double jumping
if self.vel.y > 0:
self.onGnd = False
hits = pg.sprite.spritecollide(self, self.game.platforms, False)
for hit in hits: # Vertical Collision
if self.vel.y > 0: # Downward motion
self.rect.bottom = hit.rect.top
self.vel.y = 0
self.onGnd = True
elif self.vel.y < 0: # Upward motion
self.rect.top = hit.rect.bottom
self.vel.y = 0
self.pos.y = self.rect.y # Update true postion
# Limit Player's movement
if self.rect.bottom > HEIGHT:
self.vel.y = 0
self.rect.bottom = HEIGHT
self.pos.y = self.rect.y
任何对这个问题的帮助将不胜感激!