// the larger creature coexists with a trail of buzzing mosquitos
// when the mosquito gets too close, the larger creature inflates
Jitter jitter1;
Jitter jitter2;
void setup() {
size(400,400);
background(0);
smooth();
//Initialize our objects
// shape, xposition, yposition, size, random frequency, motion amplitude)
jitter1 = new Jitter("circle", 0, 0, 50, .05, 5);
jitter2 = new Jitter("square", 400, 400, 10, 5, 20);
}
void draw() {
fill(0, 50); // trail hack
rect(0, 0, width, height);
//update location of the first object (move function) and draw it onto screen (display function)
jitter1.move();
jitter1.display();
//update location of the second object (move function) and draw it onto screen (display function)
float d = dist(jitter1.x, jitter1.y, jitter2.x, jitter2.y);
jitter1.grow(d);
jitter2.move();
jitter2.display();
}
class Jitter {
float x = width/2;
float y = height/2;
float speed, dx, dy, xsize;
String shape;
float randomFreq;
float motionAmplitude;
Jitter (String xshape, float xpos, float ypos, float shapeSize, float freq, float amp) {
x = xpos;
y = ypos;
shape = xshape;
randomFreq = freq;
motionAmplitude = amp;
xsize = shapeSize;
dx = random(-motionAmplitude, motionAmplitude);
dy = random(-motionAmplitude, motionAmplitude);
}
void move() {
if (random(0.0, 1.0) < randomFreq) {
dx = random(-motionAmplitude, motionAmplitude);
dy = random(-motionAmplitude, motionAmplitude);
}
x += dx; // add dx constantly to x
y += dy;
x = constrain(x, 0, width - xsize);
y = constrain(y, 0, height - xsize);
//println("This object moved to " + x);
}
void grow(float d) {
if (d <= 50) {
xsize = xsize + d/10;
}
if (xsize > 200) {
xsize = 50;
}
}
void display() {
if(shape == "circle") {
fill(200);
ellipseMode(CORNER);
ellipse(x, y, xsize, xsize);
}
else if(shape == "square") {
fill(80);
noStroke();
rect(x, y, xsize, xsize);
}
}
}
Scott - K
on Wednesday, Nov 5, 2008 – 3:33 am