I coded it in Processing. What I want to do is that The Eater follows a food particle, but I don't know why it always goes to the (0,0) coordinate.
I'm trying to do an "Ecosystem Simulator" in processing, so what this code does is simulates an ecosystem but a very small scale. What I want to do is that the Eater follows a food particle, but I dont know why it always goes to the (0,0) coordinate.
This is my Main Tab:
int cantidadcom = 20;
int Pob_b1 = 1;
float targx;
float targy;
Comida[] comi = new Comida[cantidadcom];
Bio[] Bio = new Bio[cantidadcom];
int RandFood = int(random(0,comi.length));
void setup(){
size(800,800);
frameRate(60);
for(int i = 0; i<cantidadcom; i++){
comi[i] = new Comida();
}
for(int i = 0; i<Pob_b1; i++){
Bio[i] = new Bio();
}
}
void draw(){
PVector TargPos = new PVector(comi[RandFood].loc.x,comi[RandFood].loc.y);
float targx = TargPos.x;
float targy = TargPos.y;
background(255);
for(int i = 0; i<cantidadcom; i++){
comi[i].draw();
}
for(int i = 0; i<Pob_b1; i++){
Bio[i].draw();
}
println(TargPos.x,TargPos.y);
}
This is my "Food" tab:
class Comida{
PVector loc;
PVector vel;
PVector acc;
PVector target;
float x;
float y;
float diam;
float colour;
int topspeed =5;
Comida(){
x = random(0,width);
y = random(0,height);
loc = new PVector(x,y);
vel = new PVector(0,0);
diam = 10;
colour = random(0,200);
}
void update(){
target = new PVector(random(0,width),random(0,height));
acc = PVector.sub(target,loc);
acc.normalize();
float factor = random(1,3);
acc.mult(factor);
vel.add(acc);
//vel.add(random(-4,4),random(-4,4));
vel.limit(topspeed);
loc.add(vel);
}
void check(){
if ((loc.x<diam)||(loc.x>width-diam)){
vel.x = vel.x*-1;
}
if ((loc.y<diam)||(loc.y>height-diam)){
vel.y = vel.y*-1;
}
}
void display(){
fill(colour);
ellipse(loc.x,loc.y,diam,diam);
}
void draw(){
display();
update();
check();
}
}
class Comidatotal{
Comida[] c=new Comida[cantidadcom];
Comidatotal(){
for (int i = 0; i<c.length;i++){
c[i]= new Comida();
}
}
void display(){
for(int i = 0; i<c.length;i++){
c[i].draw();
}
}
}
And this is my "eater" tab:
class Bio{
PVector loc;
PVector vel;
PVector acc;
PVector target;
float x;
float y;
float diam;
int colour;
int topspeed =5;
float tempx = comi[RandFood].loc.x;
float tempy = comi[RandFood].loc.y;
Bio(){
x = random(0,width);
y = random(0,height);
loc = new PVector(x,y);
vel = new PVector(0,0);
diam = 32;
}
void update(){
target=new PVector(targx,targy);
acc= PVector.sub(target,loc);
acc.normalize();
float factor=random(1,3);
acc.mult(factor);
//vel.add(random(-2,2),random(-2,2));
vel.add(acc);
vel.limit(topspeed);
loc.add(vel);
}
void check(){
if ((loc.x<diam)||(loc.x>width-diam)){
vel.x = vel.x*-1;
}
if ((loc.y<diam)||(loc.y>height-diam)){
vel.y = vel.y*-1;
}
}
void display(){
//fill(0,125,0);
noFill();
ellipse(loc.x,loc.y,diam,diam);
}
void draw(){
display();
update();
check();
}
}
What I want is that the "Bio" object follows a random "Comida/food" object, but I don't know why it always goes to the (0,0) coordinate.