# Objects in contact then disappear

**URL:** <https://discourse.processing.org/t/objects-in-contact-then-disappear/36531>\
**Category:** Processing.py\
**Tags:** homework\
**Created:** [April 23, 2022, 3:30am UTC](https://discourse.processing.org/t/objects-in-contact-then-disappear/36531 "2022-04-23T03:30:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maya](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@maya](https://discourse.processing.org/u/maya)\
**Post date:** [April 23, 2022, 3:30am UTC](https://discourse.processing.org/t/objects-in-contact-then-disappear/36531/1 "2022-04-23T03:30:25Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [April 23, 2022, 4:42am UTC](https://discourse.processing.org/t/objects-in-contact-then-disappear/36531/2 "2022-04-23T04:42:21Z")

</div>

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](https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection). If you want to check for [circle-rectangle collisions](https://www.jeffreythompson.org/collision-detection/circle-rect.php), that’s a bit more complicated. It depends on how accurate you want to be, I guess.

The [`dist()`](https://py.processing.org/reference/dist.html) function is useful if you opt for Circle Collisions –

```python
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).

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 16, 2022, 2:47am UTC](https://discourse.processing.org/t/objects-in-contact-then-disappear/36531/3 "2022-05-16T02:47:10Z")

</div>

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

```python
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](https://py.processing.org/reference/PVector.html) instead of `x`, `y`, `x_speed`, `y_speed`, but whatever you’re comfortable with is fine.
