# How to make an object disappear on collision

**URL:** <https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593>\
**Category:** Coding Questions\
**Created:** [November 21, 2020, 4:39pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593 "2020-11-21T16:39:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 4:39pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/1 "2020-11-21T16:39:30Z")

</div>

i have a very simple code as I’m tryna learn collision detection as I want to use it in the future.

```auto
//Falling Game
Faller player; //declare instance variable
Obstacle thing;

void setup() 
{
  size(400, 600);
  player = new Faller(width/2, 20); //top of screen
  thing = new Obstacle(width/2,height, -2); //bottom of screen
}

void draw()
{
  background(0);
  player.render();
  thing.move();
  thing.render();
  
  
 // has the obstacle crashed into the faller instance
// do something
if (thing.crashed(player))
{
  player = null; //i get an error here!!
}

}
class Obstacle
{
  int x, y, speedY;
  //SpeedY is -ve value

  Obstacle(int x, int y, int speedY)
  {
    this.x = x;
    this.y = y;
    this.speedY = speedY;
  }

  void render()
  {
    fill(255, 0, 0); //red
    ellipse(this.x, this.y, 30, 30);
  }

  void move() //moved up screen
  {
    this.y = this.y + this.speedY;
  }

  boolean crashed(Faller otherObject) //reports on status of this obstacle
  {
    if ( abs(otherObject.x - this.x) < 10)
    {
      if (abs(otherObject.y - this.y) < 10)
      { 
        return true;
      }
    }
    return false;
    //return false if no crash with player
    //return true if distance to player is very close - faller x,y
  }
}//end of class
class Faller
{
  int x, y;

  Faller(int x, int y)
  {
    this.x = x;
    this.y = y;
  }

  void render()
  {
    fill(0,255,0);
    rect(x, y, 10, 30);
  }
} //end of class

```

i want to make my object disappear on collision and I thought maybe using =null would solve this for me but it doesnt.

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 4:41pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/2 "2020-11-21T16:41:39Z")

</div>

when i use:

```auto
if (thing.crashed(player))
{
print("crashed");
}

```

i can see the ball crashing into my rectangle and printing crashed. but how would I make the rectangle disappear when the ball has crashed

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 21, 2020, 6:23pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/3 "2020-11-21T18:23:17Z")

</div>

You should try with

`if (player != null) player.render();;`

It’s weird it does not complain of NullPointerException if you set player to null and still call a method on it later.

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 7:59pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/4 "2020-11-21T19:59:31Z")

</div>

im sorry i dont understand should i do this code and then say if crashed then move to x=300 or something to test?

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 8:04pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/5 "2020-11-21T20:04:14Z")

</div>

if i do x=300, the x changes i dont know how would i do that.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 9:45pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/6 "2020-11-21T21:45:30Z")

</div>

> [@TheGullible1](#):
>
> ```auto
> player = null; //i get an error here!!
> 
> ```

You wrote, you get an error here.

Not correct!

You get the error here: `player.render();`

(Remark: The error is just caused by the line you referred to: player = null; )

AND

start of boolean crashed(

if (otherObject==null)  
return false;

AND

start of move()

```
if (player==null) 
  return; 

```

To avoid the error say: `if (player != null) player.render();

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 9:49pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/7 "2020-11-21T21:49:08Z")

</div>

ive made a mess of my collision stuff and now I’m tryna make an arraylist so I can sort this object stuff out. lol. I’m gonna go cry.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 9:50pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/8 "2020-11-21T21:50:41Z")

</div>

> [@TheGullible1](#):
>
> lol. I’m gonna go cry.

HERE working version

```auto

//Falling Game
Faller player; //declare instance variable
Obstacle thing;

void setup() 
{
  size(400, 600);
  player = new Faller(width/2, 20); //top of screen
  thing = new Obstacle(width/2, height, -2); //bottom of screen
}

void draw()
{
  background(0);
  if (player != null) 
    player.render();
  thing.move();
  thing.render();

  // has the obstacle crashed into the faller instance
  // do something
  if (thing.crashed(player))
  {
    player = null; //i get an error here!!
  }
}

// =========================================================================================

class Obstacle
{
  int x, y, speedY;
  //SpeedY is -ve value

  Obstacle(int x, int y, int speedY)
  {
    this.x = x;
    this.y = y;
    this.speedY = speedY;
  }

  void render()
  {
    fill(255, 0, 0); //red
    ellipse(this.x, this.y, 30, 30);
  }

  void move() //moved up screen
  {
    if (player==null) 
      return; 
    this.y = this.y + this.speedY;
  }

  boolean crashed(Faller otherObject) //reports on status of this obstacle
  {
    if (otherObject==null) 
      return false; 
    if ( abs(otherObject.x - this.x) < 10)
    {
      if (abs(otherObject.y - this.y) < 10)
      { 
        return true;
      }
    }
    return false;
    //return false if no crash with player
    //return true if distance to player is very close - faller x,y
  }
}//end of class

// =========================================================================================

class Faller
{
  int x, y;

  Faller(int x, int y)
  {
    this.x = x;
    this.y = y;
  }

  void render()
  {
    fill(0, 255, 0);
    rect(x, y, 10, 30);
  }
} //end of class
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 9:54pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/9 "2020-11-21T21:54:38Z")

</div>

In general not a good idea to say `player = null...`

Instead you can also set a global boolean **GameOver** to true

and keep on displaying the player and just display an additional message like “You Lost” when the variable `GameOver` is true.

When you have multiple obstacles, use an `ArrayList`

**your wording**

I am not to happy with your wording

`Faller player;`

really? The word `Faller` could also indicate an obstacle that is falling, like a dangerous asteroid the player has to avoid

Why not say Player player and

```auto
Obstacle obstacle ;

```

Both are distinguished because the class Obstacle is written with a Capital Letter, the object obstacle is not

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 9:55pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/10 "2020-11-21T21:55:00Z")

</div>

do you know how to add random x and y to a constructor eg

```auto
Coin()
  {
    this.x = random(200);
    this.y = random(400);

```

would it be

```auto
Coin(int random(x), int random(y))

```

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 9:56pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/11 "2020-11-21T21:56:01Z")

</div>

yeh sorry i was only making a quick example,

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 9:57pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/12 "2020-11-21T21:57:03Z")

</div>

> [@TheGullible1](#):
>
> `Coin(int random(x), int random(y))`

when you call it from setup()

`Coin(int random(1,6), int random(1,6))`

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 9:57pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/13 "2020-11-21T21:57:34Z")

</div>

i was using an arraylist for eg  
hit object1 and object 2 will appear and score +1. thats my idea behind it

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 9:57pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/14 "2020-11-21T21:57:52Z")

</div>

Good idea!

Keep working on it!

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 9:58pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/15 "2020-11-21T21:58:39Z")

</div>

because if i dont do an arraylist i just have a messy code eg

```auto
/ object1.render();
 // object2.render();
// object3.render();
// object4.render();
// object5.render();
// object6.render();

// if (object1.crashed(player1))
// {
// object2.render();
// score = score + 1;
// }
/* } else if (object2.crashed(player1))
  {
    object3.render();
    score = score + 1;
  } else if (object3.crashed(player1))
  {
    object4.render();
    score = score + 1;
  } else if (object4.crashed(player1))
  {
    object5.render();
    score = score + 1;
  } else if (object5.crashed(player1))
  {
    object6.render();
    score = score + 1;
  } 

```

so an arraylist here would be better

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 9:59pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/16 "2020-11-21T21:59:29Z")

</div>

> [@Chrisir](#):
>
> ` this.y = this.y + this.speedY;`

your obstacle is falling upwards

and you player is from a class called Faller but not moving at all…

😉

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 10:00pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/17 "2020-11-21T22:00:01Z")

</div>

> [@TheGullible1](#):
>
> so an arraylist here would be better

Absolutely, go ahed

It’s a good idea

See you tomorrow

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 10:01pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/18 "2020-11-21T22:01:38Z")

</div>

idk this feels off, are you being sarcastic?

---

<div class="post-metadata">

**Author:** ![TheGullible1](https://avatars.discourse-cdn.com/v4/letter/t/7bcc69/32.png) [@TheGullible1](https://discourse.processing.org/u/TheGullible1)\
**Post date:** [November 21, 2020, 10:02pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/19 "2020-11-21T22:02:06Z")

</div>

thank you 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 21, 2020, 10:02pm UTC](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593/20 "2020-11-21T22:02:53Z")

</div>

No, I think this is a good idea

No sarcasm involved

just improve the wording of faller / obstacle / player, imho

[Next page](https://discourse.processing.org/t/how-to-make-an-object-disappear-on-collision/25593.md?page=2)
