0

Below is the code that I have so far. How could I have it so when I collide the mouse with one circle it's removed? I'm sure I can use things.remove(0); but I'm not sure where to put it.

Any help will be great, thanks!

ArrayList<Thing> things = new ArrayList();
Player person;

boolean testMode = true;

void setup() {
size(600, 600);

things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());

person = new Player();
}

void draw() {
background(255);
noStroke();
noCursor();

person.display();

 for (Thing t : things) {    
t.display();
if (collision(person, t) == true) {
text("OUCH!", person.x, person.y-30);
  things.remove(0);
}
}
}

/////////////////////////////////////

boolean collision (Player p, Thing t) {
float d = dist(p.x, p.y, t.xPos, t.yPos); 
if (p.radius + t.radius > d) {
// we have a collision
return true;
} else {
return false;
}
}

////////////////////////

class Thing {
//fields
float xPos;
float yPos;
float radius = 30;

//constructor
Thing() {
xPos = random(500); 
yPos = random(500);
}

//methods
void display() {

float objDist = dist(xPos, yPos, mouseX, mouseY);
if ( objDist < radius ) {
  fill(0, 0, 255, 128);
} else {   
  fill(0, 255, 0, 70);
}

ellipse(xPos, yPos, radius, radius);
}
}

//////////////////////////////////

class Player {
float x;
float y;
float radius = 30;

void display() {
fill(255,0,0,70);
ellipse(mouseX,mouseY,radius, radius);
}
}

1 Answer 1

1

You're code is almost there, but there are a few gotchas that seemed to have slipped by:

  1. you're drawing the player at the mouse coordinates (ellipse(mouseX, mouseY, radius, radius);), but you don't update the player's x,y coordinates used in collision detection. This keeps your distance calculation constant and therefore the collision code returns false
  2. if the collision returned true, the existing code was removing the first element (things.remove(0);), but it should remove the current thing colliding
  3. the collision test is using the sum of the two objects' radii, but the objects are drawn using the radius as a diamter: either check collision with half of the radius, or draw the ellipses using radius * 2

Here's what I mean in code:

ArrayList<Thing> things = new ArrayList();
Player person;

boolean testMode = true;

void setup() {
  size(600, 600);
  //for loops can help you avoid repeating yourself in code
  for(int i = 0 ; i < 8; i++){
    things.add(new Thing());
  }
//  things.add(new Thing());
//  things.add(new Thing());
//  things.add(new Thing());
//  things.add(new Thing());
//  things.add(new Thing());
//  things.add(new Thing());
//  things.add(new Thing());

  person = new Player();
}

void draw() {
  background(255);
  noStroke();
  noCursor();

  person.x = mouseX;
  person.y = mouseY;
  person.display();

  for (int i = 0 ; i < things.size(); i++) {//regular for loop to avoid ConcurrentModificationException (which is through usin using for : )
    Thing t = things.get(i);    
    t.display();

    if (collision(person, t) == true) {
      //text("OUCH!", person.x, person.y-30); //this will probably appear for a slit second
      println("OUCH!");
      //things.remove(0);//don't remove the first, remove the thing that collided 
      things.remove(t);
    }
  }
}

/////////////////////////////////////

boolean collision (Player p, Thing t) {
  float d = dist(p.x, p.y, t.xPos, t.yPos);
  if ((p.radius + t.radius) * .5 > d) {//.5 is the same as /2 -> although you named the properties of player and thing radius, you are drawing the diameter (twice the size)
    // we have a collision
    return true;
  } else {
    return false;
  }
}

////////////////////////

class Thing {
  //fields
  float xPos;
  float yPos;
  float radius = 30;

  //constructor
  Thing() {
    xPos = random(500); 
    yPos = random(500);
  }

  //methods
  void display() {

    float objDist = dist(xPos, yPos, mouseX, mouseY);
    if ( objDist < radius ) {
      fill(0, 0, 255, 128);
    } else {   
      fill(0, 255, 0, 70);
    }

    ellipse(xPos, yPos, radius, radius);
  }
}

//////////////////////////////////

class Player {
  float x;
  float y;
  float radius = 30;

  void display() {
    fill(255, 0, 0, 70);
    //ellipse(mouseX, mouseY, radius, radius);//you're drawing the player at it's updated positions but the x,y properties used in collision detection aren't updated  
    ellipse(x, y, radius, radius);
  }
}
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.