【问题标题】:Python: Collisions with OvalsPython:与椭圆的碰撞
【发布时间】:2016-08-20 05:09:33
【问题描述】:

我以前从未用 python 编写过,但我正在尝试进行碰撞检测,因为当两个椭圆发生碰撞时,其中一个椭圆(气泡/我的)将被删除。

def delete_bubble(k):
    bubble_id[k].remove
    bubble_r[k].remove

def get_dist(mine,sub):
    x = c.coords(mine)
    a = c.coords(sub)
    #compare coordinates and if same, return 0

def collide():
    for k in range(len(bubble_id)):
        x = get_dist(bubble_id[k],ship_c)
        if x == 0:
            delete_bubble(k)

我如何计算两个椭圆之间的距离,我的和 sub?如果 x == a 则返回 0?或者我需要写一个距离公式来计算,还是我需要找到每个椭圆的中心并进行比较?我也有每个椭圆的半径,但我对如何写这个感到困惑。 由于这是交互式游戏的一部分,我需要不断检查碰撞,我将如何在 main 中实现它:

#main game loop
for x in range(10):
    create_mines(c)
window.after(40, move_mines, c)
window.after(10, collide) #does this work?
window.mainloop()

【问题讨论】:

  • 你需要距离:毕达哥拉斯a^2 + b^2 = c^2,其中c是距离,a = x1-x2b = y1-y2。然后您可以比较c <= r1+r2c^2 <= (r1+r2)^2 而不必使用square root
  • 它应该在 move_mines 中,因为您希望每次移动时都检查一下。

标签: python collision-detection


【解决方案1】:

我在python中做了一个可以感知碰撞检测的程序,这里是:

if oval 1 x < oval 2 x + oval 1 width and oval 1 x + oval 2 width > oval 2 x and oval 1 y < oval 2 y + oval 1 height and oval 2 height + oval 1 y > oval 2 y:
    #put the code that deletes the oval here

这是通过将椭圆放入“假想盒子”中并检测第一个“假想盒子”的任何边缘是否接触到第二个“假想盒子”的任何边缘来实现的。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-01-15
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多