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()
3
  • 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 Commented Feb 6, 2016 at 2:43
  • And it should be in move_mines as you want you check every time something moves. Commented Feb 6, 2016 at 16:03
  • Possible duplicate of How do I create collision detections for my bouncing balls? Commented Feb 6, 2016 at 19:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.