Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

ArrayList<Ball> ballList = new ArrayList<Ball>();
void setup()
{
  size(600, 800);
  ballList.add(new Ball(mouseX, mouseY, 20));
  ballList.add(new Ball(width/2, height/2, 40));
}
void draw()
{
  background(255);
  ballList.get(0).update(mouseX, mouseY);
  for(int i = 0; i<ballList.size(); i++) {
    ballList.get(i).draw(); 
  }
  
   ballList.get(0).checkCollision(ballList.get(1)._position, ballList.get(1)._radius);
}
class Ball {
  PVector _position = new PVector();
  int _radius;
  int colour = 250;
  public Ball(int x, int y, int r) {
    _position.set(x, y, 0);
    _radius = r;
  }
  void update(int x, int y) {
    _position.set(x, y, 0);
  }
  
  void draw() {
    pushMatrix();
    fill(colour,0,0);
    translate(_position.x, _position.y);
    ellipse(0, 0, _radius*2, _radius*2);
    popMatrix();
  }
  
  void checkCollision(PVector position, int radius) {
    float distance = PVector.dist(_position, position);
    if (distance <= radius +_radius) {
       println("hit");
        colour = 60;
    } else {
      colour = 255;
    }
  }
}