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.
Page Index Toggle Pages: 1
array help 2 (Read 693 times)
array help 2
Nov 14th, 2009, 9:34am
 
here i am again, not able to figure out those arrays. i've been trying to solve this thing the whole day. i've got the boids drifting about nicely with the noise function - now i want to connect the boids with lines, if (dist < 50).

i can't seem to get it to do that .. it's probably just one line of code that is placed wrong - it always seems to be that. any ideas would be hugely appreciated!

thanks.

Code:
int boids = 50;         // amount of boids
int canvasX = 800;      // canvas size x-direction
int canvasY = 600;      // canvas size y-direction
float inc = 0.003;      // move increasement every loop
float pX, pY, nX, nY;

float[] ptsX = new float[boids];       // declare + create array X
float[] ptsY = new float[boids];       // declare + create array Y

void setup() {
 size(canvasX, canvasY);
 smooth();
 stroke(0, 170, 250);
 strokeWeight(1);
 background(255);
 ellipseMode(CENTER);

 for (int i = 0; i < boids; i++) {
   ptsX[i] = random(0, canvasX);   // write start pts to arrayX
   ptsY[i] = random(0, canvasY);   // write start pts to arrayY
 }
}

void draw() {
 background(0);

 for (int i = 0; i < boids-1; i++) {
   nX = noise(ptsX[i]) * canvasX;     // add noise value to nX
   nY = noise(ptsY[i]) * canvasY;     // add noise value to nY
   ellipse(nX, nY, 3, 3);             // insert ellipse/pts
   
   /* // PROBLEM! ---------
   for (int j = 0; i < boids; i++) {
     if (dist(prev pos X, prev pos Y, pos X, pos Y) < 50) {
       draw line(prev pos X, prev pos Y, pos X, pos Y);
     }      
   }
   */ // PROBLEM! ---------
   
   ptsX[i] = ptsX[i] + inc;           // increase X with inc
   ptsY[i] = ptsY[i] + inc;           // increase Y with inc    
 }
}
Re: array help 2
Reply #1 - Nov 14th, 2009, 7:25pm
 
should be a simple fix, yep: you need to tell your boid class what prevPosX and posX (and y, etc) are, and update them each frame.  You also can't have spaces in a variable name, because the compiler reads that as a separate reference.

Edit: sorry, noticed you don't have a boids class, just the two position arrays.  You could save a second pair of arrays as the "prevPos" ... or look into making a boid class, with its own update() and render() functions.
Re: array help 2
Reply #2 - Nov 15th, 2009, 4:21am
 
thanks for the reply. sorry, if the code was a bit misleading, i didn't intent for it to look like actual variables, i was just trying to explain what i wanted it to do.

but thanks again, i was actually wondering, if that wasn't thing i was missing. creating prePosX[], prePosY[], posX[], posY[].

i'll try that ... but then; what about the first loop? there's only a previous position, when the it has been looping once? maybe the program will ignore that.
Re: array help 2
Reply #3 - Nov 15th, 2009, 4:23am
 
There is some confusion in that you say
Quote:
now i want to connect the boids with lines,
but in the code
Code:
    /* // PROBLEM! ---------
   for (int j = 0; i < boids; i++) {
     if (dist(prev pos X, prev pos Y, pos X, pos Y) < 50) {
       draw line(prev pos X, prev pos Y, pos X, pos Y);
     }      
   }
   */ // PROBLEM! ---------

it suggests that you want to connect the boid to its previous position, I will go with the first option.

Quote:
it's probably just one line of code that is placed wrong - it always seems to be that.

If only that was always true for all of us.

First of all your arrays ptsX[] and ptsY[] are no longer being used to record the position of the boid rather it is holding a position in 2D Perlin space (see http://processing.org/reference/noise_.html) and the only thing that knows where the boid position is nx & ny.
So the first thing is to create a new pair of arrays to hold the position of each boid and in draw update the boid position, then draw the connecting lines like this.
Code:
int boids = 50;         // amount of boids
int canvasX = 800;      // canvas size x-direction
int canvasY = 600;      // canvas size y-direction
float inc = 0.003;      // move increasement every loop
float pX, pY, nX, nY;

float[] ptsX = new float[boids];       // declare + create array X
float[] ptsY = new float[boids];       // declare + create array Y

float[] posX = new float[boids];       // declare + create array X
float[] posY = new float[boids];       // declare + create array Y

void setup() {
 size(canvasX, canvasY);
 smooth();
 stroke(0, 170, 250);
 strokeWeight(1);
 background(255);
 ellipseMode(CENTER);

 for (int i = 0; i < boids; i++) {
   ptsX[i] = random(0, canvasX);   // write start pts to arrayX
   ptsY[i] = random(0, canvasY);   // write start pts to arrayY
   posX[i] = ptsX[i];
   posY[i] = ptsY[i];
 }
}

void draw() {
 background(0);
 fill(0,128,255);
 stroke(128,255,128, 80);
 strokeWeight(1);  

// Update th boids positions
 for (int i = 0; i < boids; i++) {
   posX[i] = noise(ptsX[i]) * canvasX;  // Update the screen position
   posY[i] = noise(ptsY[i]) * canvasY;
   ptsX[i] = ptsX[i] + inc;  // Update position in Perlin noise space
   ptsY[i] = ptsY[i] + inc;
 }

 // Draw the connecting lines
 for (int i = 0; i < boids - 1; i++) {
   for (int j = i; j < boids; j++) {
     if (dist(posX[j], posY[j], posX[i], posY[i]) < 100) {
       line(posX[j], posY[j], posX[i], posY[i]);
     }
   }
 }
 // draw the boids last so they are notmasked by lines
 for (int i = 0; i < boids; i++) {
   ellipse(posX[i], posY[i], 3, 3);    // insert ellipse/pts
 }
}

Smiley
Re: array help 2
Reply #4 - Nov 15th, 2009, 4:39am
 
thanks, quark. you're absolutely right. i must've been staring too long at the code. i had it all wrong.

cheers!
Page Index Toggle Pages: 1