Code

/*
particle swimmers
press space to see the flow field
--Max
*/

particle[] parr;
final float damp = 0.1;
final float AMP = 3.0;
float z = 0.0;
float z_step = 0.005;
boolean reveal_slopes = false;

void setup()
{
  size(640,480);
  colorMode(RGB,1.0);
  
  parr = new particle[100];
  for (int i=0; i<100; i++)
    parr[i] = new particle(random(0,width),random(0,height),0.0,0.0,color(random(1.0),random(1.0),random(1.0)));
  background(1.0);
  smooth();
  strokeWeight(0.4);
  background(1.0,1.0,1.0);
  ellipseMode(CENTER);
}

void draw()
{
  for (int i=0; i<100; i++)
  {
    parr[i].applyField();
    parr[i].update();
  }
  noStroke();
  fill(1.0,1.0,1.0,0.04);
  rect(0,0,width,height);
  noFill();
  
  if (reveal_slopes)
  {
    stroke(0.0,1.0,0.0);
    for (int x=0; x<=width; x+=20)
    for (int y=0; y<=height; y+=20)
    {
      ellipse(x,y,5,5);
      line(x,y,x+10.0*magAt(x,y)*cos(angleAt(x,y)),y+10.0*magAt(x,y)*sin(angleAt(x,y)));
    }
  }
    
  z+=z_step;
}

void keyPressed()
{
  if (key == ' ')
    reveal_slopes = !reveal_slopes;
}

float angleAt(float x, float y)
{
  return TWO_PI*fn(noise(0.005*x,0.005*y,z));
}

float magAt(float x, float y)
{
  return AMP*(noise(0.005*x+1000,0.005*y+1000,z));
}

float fn(float t)
{
  return 0.5*(sq(cos(TWO_PI*t))+1.0);
}

class particle
{
  float x,y,vx,vy;
  color c;
  
  particle(float u, float v, float du, float dv, color w)
  {
    x=u; y=v; vx=du; vy=dv; c=w;
  }
  
  void applyField()
  {
    float a = angleAt(x,y);
    float m = magAt(x,y);
    vx += m*cos(a);
    vy += m*sin(a);
    vx *= (1.0-damp);
    vy *= (1.0-damp);
  }
  
  void update()
  {
    stroke(c);
    line(x,y,x+vx,y+vy);
    x += vx;
    y += vy;
    if (x < -10)            x+=(width+20);
    else if (x > width+10)  x-=(width+20);
    if (y < -10)            y+=(height+20);
    else if (y > height+10) y-=(height+20);
  }
}

050 -- Dynamic Simulations I: Individual Particles: Due 11/1

Statement:Create an interactive system in which multiple instances of a single type of unconnected particles are influenced by forces that are (at least partially) governed by user input. By "unconnected", I mean that there are no interparticle forces of any kind, e.g. no springs, interparticle collisions, flocking forces, etc. However, you can have particles influenced by forces from features of their environment, such as the cursor, walls, static objects, or a priori gradient fields.

For some possible inspiration, you could look into some projects by Look into works by Casey Reas, Lia, and Marius Watz; but you need not make works which are specifically accretive, as many of their examples are.

For assignments numbered 05x, you are welcome to use code from the examples that I showed in class. You may also find the following examples helpful:
-- magnet forces
-- wall collisions

Swimmers!

hide statement