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 & HelpPrograms › Slow down movement of connected "boids"
Page Index Toggle Pages: 1
Slow down movement of connected "boids" (Read 535 times)
Slow down movement of connected "boids"
Mar 25th, 2010, 1:58pm
 
hi,

i tried to introduce a button into my code that controls displaying the network-lines. that worked. the next step is now to slow down movement of connected boids, so that the pattern of the connected boids becomes stable after a while.

any ideas?? THX!




int mode = 0;
int gravitymode=0;

int n = 25;            // amount of boids
int p = 25;
int o = 25;
int s = 25;
int a = 25;
float cDist = 50;      // length of connecting lines
PImage imgred;

 float inc = 0.01;

Boids[] boids = new Boids[n];                  // array red


int canvasX = 700;     // canvas size x-direction
int canvasY = 500;     // canvas size y-direction


void keyPressed(){  
 if (key==' '){  
  noLoop();
 }
   if (key=='m'){  
  loop();
}

if (key=='h'){  
 mode=1;
}

if (key=='s'){  
 mode=0;
}
if (key=='g'){  
 gravitymode=1;
}

if (key=='r'){  
 gravitymode=0;
}
}



void setup() {
   
size(canvasX, canvasY);
smooth();
 
for (int i = 0; i < n; i++) {      // boids red
 boids[i] = new Boids(random(50,width-50),random(50,height-50));
}


imgred = loadImage("particlered.png");
     
}

void draw() {
background(0);
//stroke(234,45,65);
//strokeWeight(1);
 for (int i = 0; i < n; i++) {      // boids red update - display
  if (mode==0){
      boids[i].drawLine();
   boids[i].update();  
   boids[i].display();
  }else{
     boids[i].drawnoLine();
    boids[i].gravityupdate();    
   boids[i].display();
}
}
}

class Boids{
 

 float ptsX;
 float ptsY;
 float posX;
 float posY;
 float posYY;  
 float posXX;
 float ptsYY;
 float ptsXX;

 float inc = 0.001;     // move increasement every loop
 float inc2 =0.00001;
 

 
Boids (float _ptsX, float _ptsY){
 
 ptsX = _ptsX;
 ptsY = _ptsY;
 posX = ptsX;
 posY = ptsY;
 
 
// posXX = ptsXX;
 //posYY = ptsYY;

}

void update(){

posX = noise(ptsX) * canvasX;
posY = noise(ptsY) * canvasY;
ptsX+=inc;
ptsY+=inc;
}
 
void gravityupdate(){
for (int i = 0; i < n-1; i++) {
Boids b=boids[i];
    if (dist(posX, posY, b.posX, b.posY) > cDist) {
        posX = noise(ptsX) * canvasX;
        posY = noise(ptsY) * canvasY;
        ptsX+=inc;
        ptsY+=inc;
        } else {
          posX=300;
          posY=300;          
          //posX =posXX;
          //posY =posYY;
          //posXX+=inc2;
          //ptsYY+=inc2;
//posXX=posXX+1;
//posYY=posYY+1;
}
   }
  }

 

void display() {             // rendering settings for boids
  stroke (255);
  strokeWeight(1);
     imageMode(CENTER);
 image(imgred, posX, posY);
  //noStroke();
 // ellipseMode(CENTER);
 // fill(random (240,250), random (70,80), random (10,20),200);
//  ellipse(posX, posY, 15,15);  
}


void drawLine() {

 for (int i = 0; i < n-1; i++) {
  Boids b=boids[i];
     if (dist(posX, posY, b.posX, b.posY) < cDist) {
       line(posX, posY, b.posX, b.posY);

     }
   }
 }
 
void drawnoLine() {
}

}
Re: Slow down movement of connected "boids"
Reply #1 - Mar 25th, 2010, 2:41pm
 
Check this out (i altered your code a bit for the image):

Code:
int mode = 0; 
int gravitymode=0;

int n = 65; // amount of boids
int p = 25;
int o = 25;
int s = 25;
int a = 25;
float cDist = 50; // length of connecting lines
PImage imgred;

float inc = 0.01;

Boids[] boids = new Boids[n]; // array red


int canvasX = 700; // canvas size x-direction
int canvasY = 500; // canvas size y-direction


void keyPressed(){
if (key==' '){
noLoop();
}
if (key=='m'){
loop();
}

if (key=='h'){
mode=1;
}

if (key=='s'){
mode=0;
}
if (key=='g'){
gravitymode=1;
}

if (key=='r'){
gravitymode=0;
}
}



void setup() {

size(canvasX, canvasY);
smooth();

for (int i = 0; i < n; i++) { // boids red
boids[i] = new Boids(random(50,width-50),random(50,height-50));
}


imgred = loadImage("particlered.png");

}

void draw() {
background(0);
//stroke(234,45,65);
//strokeWeight(1);
for (int i = 0; i < n; i++) { // boids red update - display
if (mode==0){
boids[i].drawLine();
boids[i].update();
boids[i].display();
}else{
boids[i].drawnoLine();
boids[i].gravityupdate();
boids[i].display();
}
}
}

class Boids{


float ptsX;
float ptsY;
float posX;
float posY;
float posYY;
float posXX;
float ptsYY;
float ptsXX;

float inc = 0.005; // move increasement every loop
float inc2 =0.00001;



Boids (float _ptsX, float _ptsY){

ptsX = _ptsX;
ptsY = _ptsY;
posX = ptsX;
posY = ptsY;


// posXX = ptsXX;
//posYY = ptsYY;

}

void update(){

posX = noise(ptsX) * canvasX;
posY = noise(ptsY) * canvasY;
ptsX+=inc;
ptsY+=inc;
//inc*=1.04;
}

void gravityupdate(){
for (int i = 0; i < n-1; i++) {
Boids b=boids[i];
if (dist(posX, posY, b.posX, b.posY) > cDist) {
posX = noise(ptsX) * canvasX;
posY = noise(ptsY) * canvasY;
ptsX+=inc;
ptsY+=inc;
} else {
posX=300;
posY=300;
//posX =posXX;
//posY =posYY;
//posXX+=inc2;
//ptsYY+=inc2;
//posXX=posXX+1;
//posYY=posYY+1;
}
}
}



void display() { // rendering settings for boids
stroke (255);
strokeWeight(1);
fill(255,0,0);
ellipse( posX, posY,5,5);
//imageMode(CENTER);
//image(imgred, posX, posY);
//noStroke();
// ellipseMode(CENTER);
// fill(random (240,250), random (70,80), random (10,20),200);
// ellipse(posX, posY, 15,15);
}


void drawLine() {

for (int i = 0; i < n-1; i++) {
Boids b=boids[i];
// Loop should also test whether b is not this boid.
if (dist(posX, posY, b.posX, b.posY) < cDist && b!=this) {
line(posX, posY, b.posX, b.posY);
inc*=.98;//If we're close, reduce the speed.
}
}
}

void drawnoLine() {
}

}


You could prolly optimize your loops a bit cause now everything is performed twice: say you got 2 boids, b1 and b2: b1 will at somepoint perform the loop for p2 but then when p2's turn comes, it will perform the loop for p1, which is not necessary most of the times.

But it's a cool example. It reminds me a bit of that Diffusion Limited Aggregation, which produces complex forms.
Re: Slow down movement of connected "boids"
Reply #2 - Mar 25th, 2010, 4:25pm
 
Wow, this is really helpful! Thanks a lot!!!
Page Index Toggle Pages: 1