Circles Collision

edited January 2017 in Questions about Code

hi i need help, i have some circles and when they collision i need the bigger one eat the small and the small disappear. Like the game agario, but i dont know how to do it.

//Draggable draggable;
PFont p;
NullDraggableObject nullDraggableObject;
ArrayList draggables;
PImage font;

void setup(){
    size(420,500);
    smooth();
    nullDraggableObject = new NullDraggableObject();
    draggables = new ArrayList();
    for (int i=0; i < 10   ;i++){
      draggables.add(new Circle(random(width), random(height/2)));
    }
    font = loadImage("universe.jpeg");
    p = createFont("Arial",18);
    textFont(p);
}

void draw(){
   image(font,0,0);
   stroke(255);
   fill(0,255,0);
   text("Objects",10,height-40);
   noFill();
   strokeWeight(3);
   draggable = nullDraggableObject;
   for(int i=0; i < draggables.size() ;i++){
       Draggable d = (Draggable)draggables.get(i);
       d.draw();
       if( d.isMouseOverFigure() )
          draggable = d;
   }
}

void mousePressed(){
   draggable.mousePressed();
}

void mouseDragged(){
   draggable.mouseDragged();
}

void mouseReleased(){
   draggable.mouseReleased();
}
// public class Circle  implements Draggable {
     float x;
     float y;
     float radio;
     boolean drag;
     float dragX;
     float dragY;
         Circle(float a, float b){
         this.x = a;
         this.y = b;
         this.radio = random(20,50);
         drag = false;
         dragX = 0;
         dragY = 0;
     }
     
     boolean isMouseOverFigure(){
         return in(mouseX, mouseY);
     }
     
     boolean in(float ix, float iy){
         return ( dist(this.x, this.y, ix, iy) < this.radio );
     }
     
     void draw(){
        ellipseMode(CENTER);
        ellipse(x,y,2*radio,2*radio);
        
     }
     
     void mousePressed(){
         drag = in(mouseX, mouseY);
         if(drag){
             dragX = mouseX - x;
             dragY = mouseY - y;
         }
     }
     void mouseDragged(){
        if(drag){
           x = mouseX -dragX;
           y = mouseY - dragY;
        }
     }
     
     void mouseReleased(){
        drag = false;
     }
     
     
}     
interface Draggable{
     boolean isMouseOverFigure();
     boolean in(float ix, float iy);
     void draw();
     void mousePressed();
     void mouseReleased();
     void mouseDragged();
}

class NullDraggableObject implements Draggable{
     boolean isMouseOverFigure(){
         return false;
     }
     boolean in(float ix, float iy){
         return false;
     }
     void draw(){}
     void mousePressed(){}
     void mouseReleased(){}
     void mouseDragged(){}
   
}
Tagged:

Answers

Sign In or Register to comment.