We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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(){}
}
Answers
@Murata --
It sounds like you want a tutorial on collision detection with an example of the case of two circles colliding.
One approach:
Give your class Circle a method that accepts another Circle as an argument and checks for collision, based on Jefferey Thompson's example. If it returns true, then implement
eat()
separately -- e.g. grow the eating Circle based on the size of the eaten Circle. Then remove the eaten circle from the list (or whatever your game requires).