I am trying to set up an interaction between AntD and the trail it leaves behind. I want the trail to move up in the Z direction every time the AntD come within a certain distance of the trail. Right now i have just added more trail into the arrayList trail, but what I want to do is move the trail that is already being left behind. Currently i have set up the Void collideTrail for this but it doesn’t seem to be working properly. Any help would be greatly appreciated.
import peasy.*;
PeasyCam cam;
AntD [] armyD = new AntD[20];
void setup()
{
size(900, 900, P3D);
smooth();
frameRate(24);
cam = new PeasyCam (this, 1000);
cam.setMinimumDistance(50);
cam.setMaximumDistance(1000);
for (int i=0; i<armyD.length; i++)
{
armyD [i] = new AntD();
}
}
void draw()
{
background (0);
for (int i=0; i<armyD.length; i++)
{
armyD[i].heading();
armyD[i].draw();
armyD[i].transmit();
armyD[i].drawTrail();
//armyD[i].drawFood();
armyD[i].collideTrail();
}
}
}
class AntD
{
int[][]coordinates = {{0, 2}, {2, 2}, {2, 0}, {2, -2}, {0, -2}, {-2, -2}, {-2, 0}, {-2, 2}, {0, 5}, {5, 5}, {5, 0}, {5, -5}, {0, -5}, {-5, -5}, {-5, 0}, {-5, 5}};
PVector positionD;
PVector headingD;
float dia;
ArrayList trail;
ArrayList fsource;
int dir;
//List<PVector> foodA = new ArrayList<PVector>();
AntD()
{
dia = 2;
float a = random(16); // picking a random value
int b = int(a); // converting random value from float to integer
headingD = new PVector(random(width)+coordinates [b][0], random(height)+coordinates [b][1], 0);
trail = new ArrayList();
fsource = new ArrayList();
}
void heading()
{
float a = random(-2, 2);
int b = int(a);
dir = dir+b;
if (dir == -1)
{
dir = 15;
}
if (dir == 16)
{
dir = 0;
}
headingD.x = (headingD.x+width)%width;
headingD.y = (headingD.y+height)%height;
headingD.z = (headingD.z+width)%width;
}
void draw()
{
heading();
positionD = headingD.get();
headingD = new PVector(positionD.x+coordinates[dir][0], positionD.y+coordinates[dir][1], 0);
//stroke (0, 255, 0);
// line (positionD.x, positionD.y, headingD.x, headingD.y);
//pushMatrix();
stroke (0);
fill (255);
ellipse (headingD.x, headingD.y, 5, 5);
//popMatrix();
}
void transmit() {
for (int i=0; i<trail.size (); i++) {
if (trail.size() != 0) {
droppings t = (droppings) trail.get(i);
t.paint();
if ( trail.size() > 700) {
trail.remove(trail.get(i--)); // reduces the lenght of the trail, to run code smoother
}
}
}
}
void drawTrail() {
trail.add(new droppings(new PVector(positionD.x, positionD.y, positionD.z), 2));
}
void collideTrail() {
PVector droppingspos;
for (int i=0; i<trail.size(); i++) {
droppings drop = (droppings) trail.get(i);
droppingspos = drop.loc.get();
float d = positionD.dist(droppingspos);
if ( d< dia/2) {
trail.add(new droppings(new PVector(droppingspos.x, droppingspos.y, droppingspos.z+10), 2));
println("movingup");
}
}
}
class droppings {
PVector loc;
float diameter;
PVector moveZ;
droppings (PVector loc_, float dia) {
loc = loc_;
dia = diameter= 2;
}
void paint () {
pushMatrix();
stroke(255);
translate(loc.x, loc.y, loc.z);
ellipse(0,0, diameter,diameter);
popMatrix();
}