Remove Trail, can't figure out how (bot follows a random walker)

Hello, I have two random walkers. One of them(AntC) leaves a trail and the other one(AntD) is suppose to pick it up. How can i do this.

AntC [] armyC = new AntC[1];
AntD [] armyD = new AntD[1];

void setup()
{
  size(900, 900);
  smooth();


  for (int i=0; i<armyC.length; i++)
  {
    armyC [i] = new AntC();
  }
  for (int i=0; i<armyD.length; i++)
  {
    armyD [i] = new AntD();
  }
}


void draw()
{
  background (0);

  for (int i=0; i<armyC.length; i++)
  {
    armyC[i].heading();
    armyC[i].display();
    armyC[i].transmit();
    armyC[i].drawTrail();
  }
  for (int i=0; i<armyD.length; i++)
  {
    armyD[i].heading();
    armyD[i].draw();
    armyD[i].pickTrail();
    //armyD[i].transmit();
  }
}
class AntC
{

  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 position;
  PVector heading;
  ArrayList trail;
  int dir;

  AntC()
  {
    //size (600, 600);
    //background(255);

    float a = random(16);  // picking a random value
    int b = int(a);       // converting random value from float to integer

    heading = new PVector(width/2+coordinates [b][0], height/2+coordinates [b][1]);
    trail = 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;
    }
    heading.x = (heading.x+width)%width;
    heading.y = (heading.y+height)%height;
  }

  void display()
  {

    heading();
    position = heading.get();
    heading = new PVector(position.x+coordinates[dir][0], position.y+coordinates[dir][1]);

    //stroke (204, 102, 0);
    //line (position.x, position.y, heading.x, heading.y);
    stroke (255, 255, 0);
    fill(255, 255, 0);
    ellipse (heading.x, heading.y, 5, 5);
  }
  
   void transmit() {
    for (int i=0; i<trail.size (); i++) {
      if (trail.size() != 0) {
        droppings t = (droppings) trail.get(i);
        t.paint();
      }
    }
  }
  void drawTrail() {

    trail.add(new droppings(new PVector(position.x, position.y), 2));
  }
}
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;
  ArrayList trail;
  float dia;
  int dir;

  AntD()
  {
    //size (600, 600);
    //background(255);
    dia = 2;
    float a = random(16);  // picking a random value
    int b = int(a);       // converting random value from float to integer
    trail = new ArrayList();
    headingD = new PVector(random(width)+coordinates [b][0], random(height)+coordinates [b][1]);
  }

  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;
  }

  void draw()
  {

    heading();
    positionD = headingD.get();
    headingD = new PVector(positionD.x+coordinates[dir][0], positionD.y+coordinates[dir][1]);


    stroke (0, 255, 0);
    line (positionD.x, positionD.y, headingD.x, headingD.y);
    stroke (0, 255, 0);
    fill (0, 255, 0);
    ellipse (headingD.x, headingD.y, 2, 2);
  }

  void pickTrail()
  {
    for (int i=0; i<trail.size(); i++) {
      droppings Pn = (droppings) trail.get(i);
      Pn.paint();
      if (dist(headingD.x, headingD.y, 10, 10) <dia) {
        trail.remove(i);
        dia=dia+1;
      }
    }
  }
}
class droppings {
  PVector loc;
  float diameter;
  
  
  droppings (PVector loc_, float dia) {
    loc = loc_;
    dia = diameter= 2;
  }

void paint () {
    fill (255); 
    noStroke();
    ellipse(loc.x, loc.y, diameter,diameter);   
  }
  
  
}

Hello,

and welcome to the forum!

Great to have you here!

Nice idea with one bot following the trail of the other.

I like it!

there are a few things to be criticized with your code but here is a new version that should get you started

Warm regards,

Chrisir


// One of them(AntC/AntPrey) leaves a trail and the other one (AntD / AntHunter) is suppose to pick it up. How can i do this.


AntPrey [] armyC = new AntPrey[1];
AntHunter [] armyD = new AntHunter[1];

void setup()
{
  size(900, 900);
  smooth();


  for (int i=0; i<armyC.length; i++)
  {
    armyC [i] = new AntPrey();
  }
  //for (int i=0; i<armyD.length; i++)
  //{
  armyD [0] = new AntHunter();
  //}
}


void draw()
{
  background (0);

  // println("----"); 
  for (int i=0; i<armyC.length; i++)
  {
    armyC[i].heading();
    if (i==armyC.length-1)
      armyC[i].display( false );
    else armyC[i].display( false );

    armyC[i].transmit();
    armyC[i].drawTrail();
    //  if (keyPressed)
    armyC[i].trail.size();
  }
  // println("----");

  //------------------------------------

  //for (int i=0; i<armyD.length; i++)
  int i=0; 
  {
    armyD[i].heading();
    armyD[i].draw();
    armyD[i].pickTrail();
    //armyD[i].transmit();
  }
  // noLoop();
}

// ===========================================================================

class AntPrey
{

  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 position;
  PVector heading;
  ArrayList trail;
  int dir;

  AntPrey()
  {
    //size (600, 600);
    //background(255);

    float a = random(16);  // picking a random value
    int b = int(a);       // converting random value from float to integer

    heading = new PVector(width/2+coordinates [b][0], height/2+coordinates [b][1]);
    // coordinates=null; 

    trail = 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;
    }
    heading.x = (heading.x+width)%width;
    heading.y = (heading.y+height)%height;
  }

  void display( boolean showHighlighted )
  {

    heading();
    position = heading.get();
    heading = new PVector(position.x+coordinates[dir][0], position.y+coordinates[dir][1]);

    //stroke (204, 102, 0);
    //line (position.x, position.y, heading.x, heading.y);
    stroke (255, 255, 0);
    if (showHighlighted) {
      fill(255, 0, 0);//RED 
      ellipse (heading.x, heading.y, 15, 15);
    } else {
      fill(255, 255, 0);
      ellipse (heading.x, heading.y, 5, 5);
    }
  }

  void transmit() {
    for (int i=0; i<trail.size(); i++) {
      if (trail.size() != 0) {
        Droppings t = (Droppings) trail.get(i);

        //println("here "+trail.size() );
        if (i==0) 
          t.paint( true );
        else  t.paint( false );
      }
    }
  }
  void drawTrail() {

    trail.add(new Droppings(new PVector(position.x, position.y), 2));
  }
}

// ================================================================================================================

class AntHunter
{

  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;
  //  ArrayList trail;
  float dia=7;
  int dir;

  float easing = 0.2988; 

  AntHunter()
  {
    //size (600, 600);
    //background(255);
    dia = 2;
    float a = random(16);  // picking a random value
    int b = int(a);       // converting random value from float to integer
    // trail = new ArrayList();
    headingD = new PVector(random(width)+coordinates [b][0], random(height)+coordinates [b][1]);
  }

  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;
     */

    Droppings t = (Droppings) armyC[0].trail.get(0); 

    //println(t.loc.x );

    headingD.x += ( t.loc.x -  headingD.x ) * easing; 
    headingD.y += ( t.loc.y -  headingD.y ) * easing;
  }

  void draw()
  {

    // heading();
    // positionD = headingD.copy();
    // headingD = new PVector(positionD.x+coordinates[dir][0], positionD.y+coordinates[dir][1]);


    //  stroke (0, 255, 0);
    // line (positionD.x, positionD.y, headingD.x, headingD.y);
    stroke (0, 255, 0);
    fill (0, 255, 0);
    ellipse (headingD.x, headingD.y, 
      2, 2);
  }

  void pickTrail()
  {

    //for (int i=0; i<trail.size(); i++) {
    Droppings Pn = (Droppings) armyC[0].trail.get(0);
    // Pn.paint();
    if (dist(headingD.x, headingD.y, Pn.loc.x, Pn.loc.y) <dia) {
      println("Eat");
      armyC[0].trail.remove(0);
      // dia=dia+1;
    }
    // }
  }//method 
  //
}//class

// ================================================================================================================

class Droppings {
  PVector loc;
  float diameter;

  Droppings (PVector loc_, float dia) {
    loc = loc_;
    dia = diameter= 2;
  }

  void paint ( boolean cc1) {
    fill (255); 
    noStroke();
    if (cc1) 
      ellipse(loc.x, loc.y, 
        diameter*4, diameter*4);
    else 
    ellipse(loc.x, loc.y, 
      diameter, diameter);
  }
}

2 Likes

Hello,

Thank you so much, i will have a look at this. I see what I was doing wrong. Any criticism is welcome, anything to help me learn this. Do let me know what your suggestions are?

Regards
Sanwal Ghani

Hi,

I also looked at your code and this is the suggestions that I have :

Since you are using classes in your program, you don’t want to have multiple classes with “approximately” the same behavior like AntC and AntD. You can see that they have a common behavior like heading(), display() or transmit()

In order to solve this problem, you should use inheritance where you would have a base Ant class and derived Ant classes that override their custom methods where the behavior is different

1 Like

These are only suggestion, it’s not a criticism. So I apologize.

First the naming: armyC or armyD is not meaningful. Better Prey and Hunter or follower

Then they are both arrays, but you only use 1 each. Are you planning to use them as arrays? Then it’s okay.

The ArrayList trail; can also be a global object, not in the class AntPrey.

ArrayList trail; is not needed in the class AntHunter

Improvements

The variable easing determines how fast the hunter is

but once he is there he is just there eating continuously. To improve that, he could be not hungry anymore after a few bites and leave and wander around for a random period of time, then get hungry and approach the trail.

also, when he goes over other parts of the trail, the way it is now, he doesn’t eat them. He should.

Chrisir

1 Like

Hello,

Yes I realized that, and I am now the process of doing that. Thank you for the input.

Regards
Sanwal

Hello,

Yes I do intend to eventually intend to use them as arrays. I have just kept them 1 right now so it’s easier for me to manage.

As for the idea that it stops eating after a while, that is brilliant, I will try and work that into my sketch. The idea is that the ant hunter, as you have aptly put, should be eating the trail.

You see I am an architecture student and my eventual aim is to use this as sort of an experimental approach towards making architectural forms.

Regards
Sanwal

1 Like