2

I've never written in python before but I'm trying to do collision detection for when two ovals collide, one of the ovals (the bubble/mine) will be deleted.

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)

How do I calculate the distance between the two ovals, mine and sub? if x == a then return 0? Or do I need to write a distance formula to calculate, or do I need to find the center of each oval and compare? I have the radius of each oval as well but I'm confused as to how to write this. Since this is part of an interactive game, I need to continuously check for collisions, how would I implement that in 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()
Integrating Stuff
  • 5,253
  • 2
  • 33
  • 39
muditae
  • 21
  • 2
  • you need distance: Pythagoras `a^2 + b^2 = c^2` where `c` is distance and `a = x1-x2`, `b = y1-y2`. And then you can compare `c <= r1+r2` or `c^2 <= (r1+r2)^2` and you don't have to use `square root` – furas Feb 06 '16 at 02:43
  • And it should be in move_mines as you want you check every time something moves. –  Feb 06 '16 at 16:03
  • Possible duplicate of [How do I create collision detections for my bouncing balls?](http://stackoverflow.com/questions/780169/how-do-i-create-collision-detections-for-my-bouncing-balls) – Tersosauros Feb 06 '16 at 19:46

1 Answers1

0

I made a program that can sense collision detection in python, here it is:

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

this works by putting the ovals in an "imaginary box" and senses if any of the edges of the first "imaginary box" are touching any of the edges of the second "imaginary box". I hope this helped.

pi guy
  • 106
  • 1
  • 3
  • 13