我的碰撞检测无法正常工作
My collision detection is not working properly
我在 python 中使用 pygame 和数学模块编写游戏。我写了这些代码来进行碰撞检测(我设置了 5 个障碍物,我希望我的玩家与之碰撞)但问题是在玩游戏的过程中,有时有效,有时无效。
这些是我定义的碰撞函数
def collide1(binX, binY, playerX, playerY):
distance = math.sqrt(math.pow(binX - playerX, 2) + math.pow(binY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide2(snowX, snowY, playerX, playerY):
distance = math.sqrt(math.pow(snowX - playerX, 2) + math.pow(snowY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide3(glacierX, glacierY, playerX, playerY):
distance = math.sqrt(math.pow(glacierX - playerX, 2) + math.pow(glacierY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide4(boardX, boardY, playerX, playerY):
distance = math.sqrt(math.pow(boardX - playerX, 2) + math.pow(boardY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide5(iceX, iceY, playerX, playerY):
distance = math.sqrt(math.pow(iceX - playerX, 2) + math.pow(iceY - playerY, 2))
if distance == 27:
return True
else:
return False
在 while 循环中
# Collision Detection
collision1 = collide1(binX, binY, playerX, playerY)
collision2 = collide2(snowX, snowY, playerX, playerY)
collision3 = collide3(glacierX, glacierY, playerX, playerY)
collision4 = collide4(boardX, boardY, playerX, playerY)
collision5 = collide5(iceX, iceY, playerX, playerY)
if collision1:
print("You have collided!")
elif collision2:
print("You have collided!")
elif collision3:
print("You have collided!")
elif collision4:
print("You have collided!")
elif collision5:
print("You have collided!")
请告诉我哪里做错了。
现在只有当距离恰好为 27 时才会发生碰撞。如果你假设是球体,如果它们的距离小于 27 像素,你仍然可以认为它们在“碰撞”。
所以用 if distance <= 27:
替换所有距离,注意小于或等于。
另外需要注意的是计算平方根的速度非常慢。检查 distance_squared <= 27 * 27
是否比 math.pow(distance_squared, 0.5) <= 27
.
要快得多
实际上,您只是在检查玩家是否碰到了障碍物,但是如果玩家与障碍物相交,您将错过碰撞。您必须评估 distance <= 27
而不是 distance == 27
。
此外,碰撞测试实现1个功能就完全足够了。
def collide(x1, y1, x2, y2):
distance = math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2))
if distance <= 27:
return True
else:
return False
函数可以进一步简化:
def collide(x1, y1, x2, y2):
distance = math.hypot(x1 - x2, y1 - y2)
return distance <= 27
使用循环做碰撞测试:
obstacles = [(binX, binY), (snowX, snowY), (glacierX, glacierY), (boardX, boardY), (iceX, iceY)]
for x, y in obstacles:
collision = collide(x, y, playerX, playerY)
if collision:
print("You have collided!")
我在 python 中使用 pygame 和数学模块编写游戏。我写了这些代码来进行碰撞检测(我设置了 5 个障碍物,我希望我的玩家与之碰撞)但问题是在玩游戏的过程中,有时有效,有时无效。
这些是我定义的碰撞函数
def collide1(binX, binY, playerX, playerY):
distance = math.sqrt(math.pow(binX - playerX, 2) + math.pow(binY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide2(snowX, snowY, playerX, playerY):
distance = math.sqrt(math.pow(snowX - playerX, 2) + math.pow(snowY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide3(glacierX, glacierY, playerX, playerY):
distance = math.sqrt(math.pow(glacierX - playerX, 2) + math.pow(glacierY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide4(boardX, boardY, playerX, playerY):
distance = math.sqrt(math.pow(boardX - playerX, 2) + math.pow(boardY - playerY, 2))
if distance == 27:
return True
else:
return False
def collide5(iceX, iceY, playerX, playerY):
distance = math.sqrt(math.pow(iceX - playerX, 2) + math.pow(iceY - playerY, 2))
if distance == 27:
return True
else:
return False
在 while 循环中
# Collision Detection
collision1 = collide1(binX, binY, playerX, playerY)
collision2 = collide2(snowX, snowY, playerX, playerY)
collision3 = collide3(glacierX, glacierY, playerX, playerY)
collision4 = collide4(boardX, boardY, playerX, playerY)
collision5 = collide5(iceX, iceY, playerX, playerY)
if collision1:
print("You have collided!")
elif collision2:
print("You have collided!")
elif collision3:
print("You have collided!")
elif collision4:
print("You have collided!")
elif collision5:
print("You have collided!")
请告诉我哪里做错了。
现在只有当距离恰好为 27 时才会发生碰撞。如果你假设是球体,如果它们的距离小于 27 像素,你仍然可以认为它们在“碰撞”。
所以用 if distance <= 27:
替换所有距离,注意小于或等于。
另外需要注意的是计算平方根的速度非常慢。检查 distance_squared <= 27 * 27
是否比 math.pow(distance_squared, 0.5) <= 27
.
实际上,您只是在检查玩家是否碰到了障碍物,但是如果玩家与障碍物相交,您将错过碰撞。您必须评估 distance <= 27
而不是 distance == 27
。
此外,碰撞测试实现1个功能就完全足够了。
def collide(x1, y1, x2, y2):
distance = math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2))
if distance <= 27:
return True
else:
return False
函数可以进一步简化:
def collide(x1, y1, x2, y2):
distance = math.hypot(x1 - x2, y1 - y2)
return distance <= 27
使用循环做碰撞测试:
obstacles = [(binX, binY), (snowX, snowY), (glacierX, glacierY), (boardX, boardY), (iceX, iceY)]
for x, y in obstacles:
collision = collide(x, y, playerX, playerY)
if collision:
print("You have collided!")