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
}
}