How to make an object disappear on collision

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

//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.

when i use:

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

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.

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

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

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();

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.

HERE working version



//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
//

1 Like

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

Obstacle obstacle ;

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

1 Like

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

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

would it be

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

yeh sorry i was only making a quick example,

when you call it from setup()

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

1 Like

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

Good idea!

Keep working on it!

1 Like

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

/ 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

your obstacle is falling upwards

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

:wink:

1 Like

Absolutely, go ahed

It’s a good idea

See you tomorrow

1 Like

idk this feels off, are you being sarcastic?

thank you :slight_smile:

No, I think this is a good idea

No sarcasm involved

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

1 Like