An array of moving items is not updated as expected during each draw loop


#1

I’ve been working on this for a while, but I’ve become stuck while trying to get each of the members of this class to move. Here’s the code

Table table;

void setup() {
  size(500, 500);
  //noStroke();
  table = loadTable("goodBorrowedData.tsv", "header, tsv");

  //  for (int i = 0; i < table.getRowCount(); i++){
  //    myDancers[i] = new tinyDancer();
  //  }
}

void draw() {
  background(0, 157);
  tinyDancer[] myDancers = new tinyDancer[table.getRowCount()];
  for (int r=0; r < table.getRowCount(); ++r) {
    TableRow row = table.getRow(r);
    //String name = row.getString(0);
    float size = row.getFloat(1);
    float xVal = row.getFloat(2);
    float yVal = row.getFloat(4);
    float extraVal = row.getFloat(3);
    //println("cntry: "+name+". size: "+size+". xComp: "+xVal+". yComp:"+yVal+". extra:"+extraVal+".");
    myDancers[r] = new tinyDancer(r, extraVal, size, xVal, yVal);
    myDancers[r].display();
    //myDancers[r].moveit();
  }
  for (int i=0; i < myDancers.length; i++) {
    myDancers[i].moveit();
  }
}

and here is the class itself

class tinyDancer {
  float size;
  float xPos, yPos;
  float xRate, yRate;


  tinyDancer(float iXPos, float iYPos, float iSize, float iXRate, float iYRate) {
    xPos = map (iXPos, 0, 143, 0, width);
    yPos = map (iYPos, 0, 100, 0, height); //electric access
    size = map (iSize, 231, 74593901, 5, 250); //pop
    xRate = map (iXRate, 0, 7005671.2, 1, 50); //methane
    yRate = map (iYRate, 0.00912679, 40.310084, 1, 50); //CO2
  }

  void display() {
    //fill(
    ellipse(xPos, yPos, size, size);
  }

  void moveit() {
    xPos += xRate;
    println(xRate);
    yPos += yRate;

    if (xPos+size > width || xPos<0) {
      xRate *= -1;
    }
    if (yPos+size > height || yPos < 0) {
      yRate *= -1;
    }
  }
}

any help would be appreciated!


#2

I think this belongs outside of the void draw() loop as a “global” variable above void setup(). You don’t want to recreate the array of dancers every time, just the first time in the setup(). Once you move it up there, your moveit() functions will modify the global dancers, not just the new set of dancers created every time the draw loop is called.


#3

unfortunately I can’t think that to work since I can’t define the length of myDancers until after I’ve loaded my table into “table”. Could I loadTable before setup() maybe?

this is the closest I have gotten, but they still won’t update

Table table;
tinyDancer[] myDancers;

void setup(){
  size(500, 500);
  //noStroke();
  table = loadTable("goodBorrowedData.tsv", "header, tsv");
  myDancers = new tinyDancer[table.getRowCount()];
  
//  for (int i = 0; i < table.getRowCount(); i++){
//    myDancers[i] = new tinyDancer();
//  }
}

#4

can you just put it below this in your void setup() loop?


#5

I edited my last post to include that exact idea, but it still isn’t working as expected.
Edit:
I fixed it by moving the loop that creates the objects into the setup, then just creating a for loop to display and move the objects in draw.


#6

So it looks something like this?

Table table;

void setup() {
  size(500, 500);
  //noStroke();
  table = loadTable("goodBorrowedData.tsv", "header, tsv");

  for (int r=0; r < table.getRowCount(); ++r) {
    TableRow row = table.getRow(r);
    //String name = row.getString(0);
    float size = row.getFloat(1);
    float xVal = row.getFloat(2);
    float yVal = row.getFloat(4);
    float extraVal = row.getFloat(3);
    //println("cntry: "+name+". size: "+size+". xComp: "+xVal+". yComp:"+yVal+". extra:"+extraVal+".");
    myDancers[r] = new tinyDancer(r, extraVal, size, xVal, yVal);
  }
}

void draw() {
  background(0, 157);
  for (int i=0; i < myDancers.length; i++) {
    myDancers[i].display();
    myDancers[i].moveit();
  }
}

#7

yup, that’s my solution!


#8

Cool. Very curious to see what this looks like … :smile:


#9

Is there a bool holdMeClose(tinyDancer dancer); method I’m missing?


#10

that’s coming in the beta