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 › Crowd simulation
Page Index Toggle Pages: 1
Crowd simulation (Read 693 times)
Crowd simulation
Nov 15th, 2009, 9:00pm
 
Hey all, I'm brand new to programming in general (including processing) and I'm currently working on an architecture project. We're attempting to define a 2D space, "populate" it with some entities, and then have the define points on a plane that attract or repulse the otherwise flock-like entities, as they interact with simple boundaries. I was wondering if anyone had any helpful ideas or comments. Thanks in advance!
Re: Crowd simulation
Reply #1 - Nov 18th, 2009, 4:31am
 
Quote:
int WIDTH = 200;
int HEIGHT = 200;
int ENTITIES_COUNT = 200;

class Entity {
  int myX;
  int myY;
  int myMood;
  color myColor;
  
  Entity(){
    myX = int(random( width ));
    myY = int(random( height ));
    myMood = 1;
    myColor = color( random(255),random(255),random(255) );
  }

  void simulate(){
    if( get(myX, myY) != color(0) ){
      myMood=7;
    } 
    else if( myMood > 1) {
      myMood--;
    } 
    else {
      myMood = 1; 
    }
    strokeWeight( myMood );
    stroke(myColor);
    point( myX, myY );
    myX+=int(random(3));
    myX--;
    myY+=int(random(3));
    myY--;
  }
}

Entity[] Entities;

void setup() {
  size( WIDTH, HEIGHT );
  Entities = new Entity[ENTITIES_COUNT];
  for( int i = 0; i< Entities.length; i++) {
    Entities[i] = new Entity();
  }
}

void draw() {
  background(0);
  for( int i = 0; i< Entities.length; i++) {
    Entities[i].simulate();
  }
}



They don't flock, but they do float about randomly.
They also get excited if they bump into something.
It's a start, at least.
Re: Crowd simulation
Reply #2 - Nov 18th, 2009, 5:12am
 
Hi, check out the steering behaviors by Shiffman

http://www.shiffman.net/teaching/nature/steering/

Also highly recomended, check out the Advance Actionscript 3 by Keith Peters, it is all explain there and the code is easy to translate

You can do what you need, by using the evade, seek, wander behaviors explained in the articles

Cheers
rS
Page Index Toggle Pages: 1