We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Are you trying to copy an Array?
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?
You need to
I'd recommend using arrayCopy after your for loop rather then doing index based assignment to old inside your for loop.
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!
Some problems to fix: