We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › 2D Array of PVector
Page Index Toggle Pages: 1
2D Array of PVector (Read 753 times)
2D Array of PVector
Mar 15th, 2010, 8:30am
 
I don't get it. I want to store 6 mouse movements of 30 points inside a 2D array of PVectors. I only want a next point to be stored if the mouse has moved far enough. Why does the distance calculation go wrong? It seems like my array is updated without explicit instruction?

Code:
boolean drawing = false;
boolean started = false;
PVector[][] shapes = new PVector[6][30];
PVector mousePos;
int curshape, curstep;

void setup() {
size(400,400);
curshape = 0;
frameRate(30);
mousePos = new PVector(0,0);
}

void draw() {
background(160);
mousePos.x = mouseX;
mousePos.y = mouseY;
if (mousePressed) {
if (!drawing) {
//starting a new shape
drawing = true;
curstep = 0;

shapes[curshape][curstep] = mousePos; //store mouseposition

rect(25, 25, 100, 100); //for debugging purposes
}
else {
//continuing shape
//println(mousePos.x);
//println(shapes[curshape][curstep].x);
println(shapes[curshape][curstep].dist(mousePos));// and WHY is the distance always 0? I never update the array?

if (shapes[curshape][curstep].dist(mousePos) > 10) {
curstep++;
shapes[curshape][curstep] = mousePos;
}

//draw what we have so far
beginShape();
for (int i =0; i <=curstep; i++) {
curveVertex(shapes[curshape][curstep].x,shapes[curshape][curstep].y);
}
endShape();
rect(25, 25, 20, 20); //for debugging purposes
}
}
else {
//mouse is not pressed. animate!
drawing = false;
if (started) { //check if we've drawn sth so far
beginShape();
for (int i =0; i <=curstep; i++) {
curveVertex(shapes[curshape][curstep].x,shapes[curshape][curstep].y);
}
endShape();
}
rect(25, 25, 50, 50); //for debugging purposes
}

}



Re: 2D Array of PVector
Reply #1 - Mar 15th, 2010, 9:03am
 
You made a wrong optimization (or assumption).
All your shapes entries hold a reference to the only mousePos object you created, and they are all updated simultaneously on each draw() loop...
You should create a new object when you store the mouse position.
Page Index Toggle Pages: 1