How can I save the previous state of an array, to recall in the next loop?

edited September 2017 in Kinect

I am trying to connect lines between two x and y values - one current set of values, and one previous set of values. These x/y values are determined by centre of mass using a kinect lib, and so I can sometimes receive multiple x/y values at once (which is why i'm storing them in an array in each loop).

Currently I am getting an array which looks something like this:

storeX[]
[0]5672
[1]4352
[2]4262

which is what I want.

But what I now need to do, is save this for one loop, so that in the next loop, I have the new array, plus this array from the previous loop. (So I can then draw a line between the x/y values for each user detected.)

I have tried to replicate the process I used to save the current x/y values (shown in code below) but this is giving me some crazy results, and sometimes values in the array just set to 0.0.

Is it possible to somehow copy the original array, but delay it for one loop?

//imports Kinect lib import SimpleOpenNI.*;

//defines variable for kinect object
SimpleOpenNI kinect;

//declare variables for mapped x y values
float x, y, pX, pY;

//declare variable for number of people 
float numPeople = 0;

//initialise other variables
int userId;
float inches;
int count = 0;

void setup() {

  //set the display size to full screen
  size(displayWidth, displayHeight);

  //declares new kinect object
  kinect = new SimpleOpenNI(this);

  //enable depth image
  kinect.enableDepth();

  //enable user detection
  kinect.enableUser();

 // frameRate(1);
 background(255);
}

void draw() {

  //updates depth image
  kinect.update();

  //access all users currently available to us 
  IntVector userList = new IntVector();
  kinect.getUsers(userList);

  numPeople = userList.size();

  //CREATE ARRAYS TO STORE CURRENT X AND Y VALUES 
  float[] storeX = new float[int(numPeople)];
  float[] storeY = new float[int(numPeople)];


  //CREATE ARRAYS TO STORE PREV X AND Y VALUES
  float[] pastX = new float[int(numPeople)];
  float[] pastY = new float[int(numPeople)];

  //for every user detected, do this 
  for (int i = 0; i<userList.size (); i++) {

    userId = userList.get(i);

    //declare PVector position to store position 
    PVector position = new PVector();

    //get the position
    kinect.getCoM(userId, position);
    kinect.convertRealWorldToProjective(position, position);



    //SET THE PREV X AND Y VAL TO THE CURRENT VAL OF X AND Y
    pX = x;
    pY = y;
    //LOAD THESE INTO ARRAY
    pastX[i] = pX;
    pastY[i] = pY;

    //map x and y coordinates
    x = map(position.x, 0, 640, 0, displayWidth);
    y = map(position.y, 0, 480, 0, displayHeight);

    //store current values in arrays
    storeX[i] = x;
    storeY[i] = y; 
    storeDepth[i] = inches;  



   //for the amount of x/y values we have, draw a circle on each one based on the array values
   for (int j = 0; j < storeX.length; j++) {

      line(pastX[j],pastY[j],storeX[j],storeY[j]);

    }

  }
}

Answers

  • Essentially yes, but I need to save the previous state of the array, so pretty much copy it and then update it instantly. When I try and do this though, values in the copied array are just setting to 0.0 and I'm not sure why.

    Any pointers?

  • I need to save the previous state of the array

    You need to

    1. create 2 arrays, old and new
    2. In the loop
      1. update new based on old
      2. When updating is done, copy new to old

    I'd recommend using arrayCopy after your for loop rather then doing index based assignment to old inside your for loop.

  • float x;
    float peopleNum;
    float[] newVals;
    float[] oldVals;
    
    int count = 0;
    
    void setup() {
    
      size(500, 500);
      frameRate(1);
    }
    
    void draw() {
    
      //these values as "past values" are correct - need to copy these values into an array
      println("old values");
      println(oldVals);
    
      //it won't let me copy into an array like below - but if I move the array initialisation to above, the array values are ALL 0.0
      //arrayCopy(oldVals,newVals);
    
       //gives us random values
      peopleNum = int(random(1, 6));
    
      //create arrays
      oldVals = new float[int(peopleNum)];
      newVals = new float[int(peopleNum)];
    
      //for the amount of people present, DO THIS 
      for (int i = 0; i < peopleNum; i++) {    
    
        //create a random number
        x = int(random(500));
    
        //put that random number into the array corresponding to people no
        oldVals[i] = x;
    
      }
    
    
      println("new values");
      println(oldVals);
    
      //if i do arraycopy here the values of each array are always the same 
      arrayCopy(oldVals,newVals);
    
    
     }
    

    I've made a code which simulates what my main code is doing to try and figure this out. I'm still having problems. The code above stores a random no of random values in an array. Ideally, I would copy the array at the start of draw, but only on the second loop. However, when I copy the array, I either get a nullpointer error or the values in both arrays print as the same (I've included comments which indicate this).

    Am I getting close? This is driving me crazy!

  • edited September 2017 Answer ✓

    Some problems to fix:

    1. create your arrays once in setup, don't recreate them every second in draw (destroying the old contents)
    2. you can't use arraycopy at the beginning because you haven't created your array yet! do that in setup!
    3. your "put that random number into the array" line is loading data into oldvals. Don't do that!
      1. begin draw - old and new data are the same
      2. load data into newVals -- now they are different
      3. use the old and new data to draw lines -- points are different
      4. save the data into oldVals -- now they are the same
      5. end draw
Sign In or Register to comment.