Using ArrayList -- Only One Object Being Displayed

I want to have a variable number of objects in an ArrayList and display them all at a time, but my code only displays 1 regardless of the number of objects I have in the list. I think the problem is in my draw function…

void draw() {
  background(0);
  noFill();
  stroke(255);
  for (int i = 0; i < plist.size(); i++) {
    
    Curly p = plist.get(i);
    p.display();
  }
}

Thanks.

1 Like

Your code is correct. Can you print the size of the list to the console to verify the items you claim ought to be there are in fact there.

1 Like

yes, I can even println() and see the code looping through the different objects… I’m wondering if it could be an issue with my display function?

void display(){
    resetMatrix();
    translate(width / 2, height / 2);
    tail1.display();
    resetMatrix();
    translate(width / 2, height / 2);
    tail2.display();  
  }

not sure resetMatrix() is equivalent to push() pop() that might be the issue.

As per the reference

pushMatrix()

Pushes the current transformation matrix onto the matrix stack. Understanding pushMatrix() and popMatrix() requires understanding the concept of a matrix stack. The pushMatrix() function saves the current coordinate system to the stack and popMatrix() restores the prior coordinate system. pushMatrix() and popMatrix() are used in conjunction with the other transformation functions and may be embedded to control the scope of the transformations.

these are quite different in their functionality likelihood is you are drawing the items but the coordinate system is no longer centered around the canvas.

The weird thing is it was working (showing multiple objects) until I added an ArrayList… shall I just include the whole code?

sure… might help us to understand the problem

int total = 4;
ArrayList<Curly> plist = new ArrayList<Curly>();
float time = random(0, 1);
float size = random(300, 600);
PVector location = new PVector(random(-20, 20), random(-20, 20));
PVector velocity = new PVector (random(-.5, .5), random(-.5, .5));
float angle = velocity.heading();

void setup() {
   size(1280, 1280);
  for (int i = 0; i < total; i++) {
    plist.add(new Curly(size, location, velocity, time, angle));
  
  }
    

}

void draw() {
  background(0);
  noFill();
  stroke(255);
  for (int i = 0; i < plist.size(); i++) {
    //println("the number is " + i);
    Curly p = plist.get(i);
    p.display();
  }
  //saveFrame("output/creatures_####.png");
}

class Curly{
  float s;
  PVector location;
  Tail tail1;
  Tail tail2;
  Curly(float size, PVector loc, PVector vel, float t, float a){
    s = size;
    tail1 = new Tail(size, loc, vel, 1, t, a);
    tail2 = new Tail(size, loc, vel, -1, t, a); 
  }
  
  void display(){
    resetMatrix();
    translate(width / 2, height / 2);
    tail1.display();
    resetMatrix();
    translate(width / 2, height / 2);
    tail2.display();  
  }

}

class Tail{
  float time = 0;
  PVector velocity;
  PVector location; 
  float maxspeed;
  int steps;
  float lineLength;
  int sign;
  float x;
  float y;
  float sizeWeight;
  float angle;
  float v_a;
  float v_x;
  float v_y;
  
  Tail(float size, PVector loc, PVector vel, int s, float t, float a){ // acceleration, velocity
    time = t;
    lineLength = size;
    location = loc;
    steps = 300; // resolution
    sign = s;
    x = loc.x;
    y = loc.y;
    sizeWeight = size/1000;
    angle = a;
    v_a = 0.01;
    v_x = vel.x;
    v_y = vel.y;
    
  }

  void display(){
  translate(x,y);
    
    rotate(angle);
    
    float segmentLength = (float) lineLength / steps;
    float timeCurvature = (cos(time)+1) / 2.0; 
    for (int i = 0; i < steps; i++) { // i = 0
    // O to 1 value indicating how far we are from the origin of the curve
    float farFromOrigin = (float) i / (steps - 1);
    strokeWeight((1 - farFromOrigin/1.5) * 3* sizeWeight + 2); // strokeWeight(3);//
    // dividing into segments
    line(0, 0, segmentLength, 0);       
    // Rotate the coordinate system (it keeps the rotations since we are not calling push/pop) 
    rotate(exp(farFromOrigin / 2.0) * timeCurvature * TWO_PI / (4*50.0 * (1 - farFromOrigin/1.5 + 0.1)));
    // Then translate it
    translate(sign*segmentLength, 0);
    }
    time += 0.01;
    x = x + v_x;
    y = y + v_y;
    angle = angle + v_a;
  }    
}

currently your constructor initializes all the elements in the same way, with the same values, so your program is drawing all of them, but they are all drawn one on top of the other.

int total = 4;
ArrayList<Curly> plist = new ArrayList<Curly>();
float time = random(0, 1);
float size = random(300, 600);
PVector location = new PVector(random(-20, 20), random(-20, 20));
PVector velocity = new PVector (random(-.5, .5), random(-.5, .5));
float angle = velocity.heading();

void setup() {
   size(1280, 1280);
  for (int i = 0; i < total; i++) {
    plist.add(new Curly(size, location, velocity, time+0.1*i, angle));
  }
};

void draw() {
  background(0);
  noFill();
  stroke(255);
  for (int i = 0; i < plist.size(); i++) {
    //println("the number is " + i);
    Curly p = plist.get(i);
    p.display();
  }
  //saveFrame("output/creatures_####.png");
};

class Curly{
  float s;
  PVector location;
  Tail tail1;
  Tail tail2;
  Curly(float size, PVector loc, PVector vel, float t, float a){
    s = size;
    tail1 = new Tail(size, loc, vel, 1, t, a);
    tail2 = new Tail(size, loc, vel, -1, t, a); 
  }
  
  void display(){
    resetMatrix();
    //pushMatrix();
    translate(width / 2, height / 2);
    tail1.display();
    //popMatrix();
    resetMatrix();
    //pushMatrix();
    translate(width / 2, height / 2);
    tail2.display();  
    //popMatrix();
    
  }

};

class Tail{
  float time = 0;
  PVector velocity;
  PVector location; 
  float maxspeed;
  int steps;
  float lineLength;
  int sign;
  float x;
  float y;
  float sizeWeight;
  float angle;
  float v_a;
  float v_x;
  float v_y;
  
  Tail(float size, PVector loc, PVector vel, int s, float t, float a){ // acceleration, velocity
    time = t;
    lineLength = size;
    location = loc;
    steps = 300; // resolution
    sign = s;
    x = loc.x;
    y = loc.y;
    sizeWeight = size/1000;
    angle = a;
    v_a = 0.01;
    v_x = vel.x;
    v_y = vel.y;
    
  }

  void display(){
  translate(x,y);
    
    rotate(angle);
    
    float segmentLength = (float) lineLength / steps;
    float timeCurvature = (cos(time)+1) / 2.0; 
    for (int i = 0; i < steps; i++) { // i = 0
    // O to 1 value indicating how far we are from the origin of the curve
    float farFromOrigin = (float) i / (steps - 1);
    strokeWeight((1 - farFromOrigin/1.5) * 3* sizeWeight + 2); // strokeWeight(3);//
    // dividing into segments
    line(0, 0, segmentLength, 0);       
    // Rotate the coordinate system (it keeps the rotations since we are not calling push/pop) 
    rotate(exp(farFromOrigin / 2.0) * timeCurvature * TWO_PI / (4*50.0 * (1 - farFromOrigin/1.5 + 0.1)));
    // Then translate it
    translate(sign*segmentLength, 0);
    }
    time += 0.01;
    x = x + v_x;
    y = y + v_y;
    angle = angle + v_a;
  };    
};
1 Like

Ahh so even though they were random values it wasn’t creating values for each object. Think I had a brain fart. Thank you!

1 Like