Quantcast
Channel: Kinect - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 530

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

$
0
0

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]);

    }

  }
}

Viewing all articles
Browse latest Browse all 530

Trending Articles