xxxxxxxxxx
// class, array, PVector, random, sin, cos, frameCount, rotate.
Creature c;
int numA = 20;
void setup () {
size(800, 600);
strokeCap(SQUARE);
c = new Creature(width/2-100, height/2+100);
}
void draw () {
background(0);
noFill();
stroke(#04740A);
rect(0,0,width-1, height-1);
c.run();
}
//%%%%%%%%%%% classes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class Creature {
Antenna [] antennae = new Antenna[numA];
PVector pos, vel;
float ang, x, y, ang2, max, min, incr;
int edge, dir, which;
Creature(float xin, float yin) {
max = 1.5;
min = 1;
edge = 150;
pos = new PVector(xin, yin);
vel = new PVector(random(-1, 1), random(-1, 1));
vel.normalize();
vel.mult(2);
dir = random(2) > 1 ? 1 : -1;
incr = random(.01, .02);
for (int i = 0; i < antennae.length; i++) {
if (i == 0) which = 1;
else which = 2;
antennae [i] = new Antenna(which);
}
}
void run() {
update();
boundsCheck();
display();
}
void update() {
pos.add(vel);
ang2 += incr;
x = pos.x + 10 * cos(ang)*dir;
y = pos.y + 20 * sin(ang);
}
void boundsCheck() {
if (pos.x > width-edge) vel.x = -random(min, max);
else if (pos.x < edge) vel.x = random(min, max);
if (pos.y > height-edge)vel.y = -random(min, max);
else if (pos.y < edge)vel.y = random(min, max);
}
void display () {
pushMatrix();
translate(pos.x, pos.y);
ang = sin(frameCount * 0.0005);
rotate(ang);
for (int i = 0; i < antennae.length; i++) {
rotate(TWO_PI/numA);
antennae[i].run();
}
popMatrix();
}
}
class Antenna {
float ang, incr, startLen, motion;
int id;
Antenna(int idIn) {
id = idIn;
incr = random(0.003, 0.006);
if (id == 1) startLen = 20;
else startLen = random(10, 15);
}
void run () {
ang += PI * incr;
arm(-startLen, 0);
ellipse(0, 0, 3, 3);
}
void drawTail(float cx, float cy) {
//pushMatrix();
translate(cx, cy);
rotate(frameCount * 0.05);
for (float a = 0; a < TWO_PI - 0.5; a += PI/6) {
float x = 40 * cos(a);
float y = 40 * sin(a);
stroke(0, 250, 105, 60);
strokeWeight(10);
line(x, y, 0, 0);
stroke(255);
strokeWeight(1);
line(x, y, 0, 0);
noStroke();
fill(0, 250, 105, 60);
ellipse(x, y, 20, 20);
fill(255);
ellipse(x, y, 5, 5);
ellipse(0, 0, 2, 2);
}
//popMatrix();
}
void arm (float len, float angIncr) {
motion = 0.1 * sin(ang + angIncr);
//stroke(0, 150, 255, 60);
stroke(0, 250, 105, 60);
strokeWeight(18);
line(0, 0, 0, len);
stroke(255);
strokeWeight(1);
line(0, 0, 0, len);
if (len > -startLen/30) {
noStroke();
fill(0, 250, 105, 40);
ellipse(0, len, 30, 30);
fill(255);
ellipse(0, len, 5, 5);
}
if (len > 0 && id == 1) {
drawTail(0, len);
}
if (len < 0) {
pushMatrix();
translate(0, len);
rotate(motion);
arm(len+startLen/30, angIncr-0.1);
popMatrix();
}
}
}