Objects in contact then disappear

Hi, I would like for when the ball comes in contact with the dog, the dog disappears. Any ideas as to how I would do this? This is what I have so far:

dogs = []

def setup():
    size(500, 500)
    global x, y, speed, acceleration, dogs, dog_img
    x, y = -50, -50
    speed = 2
    acceleration = 0.4
    dog_img = loadImage("dog.png")
    first_dog = {'x':350, 'y':0} 
    dogs.append(first_dog)
    
def mouseReleased():
    global x, y, speed
    x = mouseX
    y = mouseY
    speed = 1
    
def draw():
    background(0)
    global x, y, speed, acceleration, dogs
    for dog in dogs:
            image(dog_img, dog['x'], dog['y'])
            dog['y'] += 50
            dogs.append({'x':random(height), 'y':0})
    ellipse(x, y, 50, 50)
    fill(0, 255, 0)
    y = y + speed
    speed = speed + acceleration
    if y > height:
        speed = speed * -1
        y = height
        
    if y = dog['y']:
        dog['y'] = 0

You’ll need to program some type of collision detection. AABB or Circle Collision are probably the easiest – https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection. If you want to check for circle-rectangle collisions, that’s a bit more complicated. It depends on how accurate you want to be, I guess.

The dist() function is useful if you opt for Circle Collisions –

def setup():
    size(500, 500)
    
def draw():
    background(0)
    dog_x, dog_y, dog_r = width/2, height/2, 100
    ball_x, ball_y, ball_r = mouseX, mouseY, 50
    
    # draw 'dog'
    fill('#0000FF')  # dog is always blue
    circle(dog_x, dog_y, dog_r*2)
    
    # draw ball
    if dist(dog_x, dog_y, ball_x, ball_y) < dog_r + ball_r:
        fill('#00FF00')  # ball is green touching dog ...
    else:
        fill('#FF0000')  # else ball is red
    circle(mouseX, mouseY, ball_r*2)

Of course, this checks for a single dog and ball collision. For multiple dogs (and a single ball), you’ll have to loop through the entire dogs list to detect if any have collided with the ball, then remove those dogs that have (i.e. remove an item from a Python list).

1 Like

Removing dogs from the list means you’ll have to remove items while iterating – so a reversed() function will prove handy:

dogs = [
  {'x':0, 'y':100, 'r':10, 'x_speed':0.1},
  {'x':0, 'y':200, 'r':20, 'x_speed':0.2},
  {'x':0, 'y':300, 'r':30, 'x_speed':0.3}
]

def setup():
    size(500, 500)

def draw():
    background(0)
    # green ball at mouse x-y
    ball_r = 25
    fill('#00FF00')
    circle(mouseX, mouseY, ball_r*2)
    # draw and move red 'dogs', and check for collisions
    for dog in reversed(dogs):  
        fill('#FF0000')
        circle(dog['x'], dog['y'], dog['r']*2)
        dog['x'] += dog['x_speed']
        if dist(mouseX, mouseY, dog['x'], dog['y']) < dog['r'] + ball_r:
            # remove any dog (from the list) that collides with ball
            dogs.remove(dog)

It’s probably better to use PVectors instead of x, y, x_speed, y_speed, but whatever you’re comfortable with is fine.