//Kade Stewart
//Section B
//kades@andrew.cmu.edu
//Project-11
//variables storing each of the ants, the repellent values,
//and the foot variables
var ants = [];
var rpx;
var rpy;
var rpc = 0;
var foot = false;
var time = 0;
function setup() {
createCanvas(480, 480);
rectMode(CENTER);
stroke(255);
strokeWeight(6);
strokeJoin(MITER);
strokeCap(PROJECT);
//initialize all of the ants (which are actually each of type turtle)
for (var i = 0; i < 100; i++) {
ants[i] = makeTurtle(random(0, width), random(0, height));
ants[i].face(random(0, 360));
ants[i].penDown();
ants[i].setWeight(4);
ants[i].setColor(255);
}
}
function mousePressed() {
//when the mouse is pressed, start the foot step
foot = true;
time = 120;
rpx = mouseX;
rpy = mouseY;
}
function draw() {
background(0);
//loop through each of the ants in the list
for (var i = 0; i < ants.length; i++) {
var t = ants[i];
//if a foot is coming (if the mouse has been clicked)
//make the ants start to avoid the shadow of the foot
if (foot) {
var d = dist(t.x, t.y, rpx, rpy);
var f = rpc / (Math.pow(d, 2));
var dirx = (t.x - rpx) / d;
var diry = (t.y - rpy) / d;
t.x += dirx * f;
t.y += diry * f;
}
//make the ant wrap around the screen
//because there are so many, it just looks like new ones are being added
if (t.x >= width) {
t.x -= width;
} else if (t.x <= 0) {
t.x += width;
}
if (t.y >= height) {
t.y -= height;
} else if (t.y <= 0) {
t.y += height;
}
//actually move the ant...finally
t.forward(5);
}
//if the foot is still terrorizing the ants (if there is still time),
//decrease the time limit and make the repellent force larger
//otherwise, make sure the foot variable is false
if (time != 0) {
time--;
rpc = map(time, 120, 0, 0, 25000);
} else {
foot = false;
}
//if the foot is more than 1/3 of a second away, draw it's growing shadow
//otherwise, the foot will start to stomp on the ground
if (time >= 20) {
noStroke();
fill(255);
ellipse(rpx, rpy, 100 - map(time, 0, 120, 0, 100), 100 - map(time, 0, 120, 0, 100));
} else if (time > 0 & time < 20) {
fill(210, 180, 140);
noStroke();
push();
translate(rpx, rpy);
scale(abs(- 2.5 + time/4));
angleMode(DEGREES);
rotate(5);
rect(20, 45, 50, 80, 10);
rect(0, 0, 10, 30, 10);
rotate(5);
rect(10, 1, 10, 30, 10);
rotate(5);
rect(20, 2, 10, 30, 10);
rotate(5);
rect(30, 3, 10, 30, 10);
rotate(5);
rect(40, 4, 10, 30, 10);
pop();
angleMode(RADIANS);
}
//the help text
fill(0);
stroke(255);
textSize(20);
text("click to stomp on these poor ants", width/6 + 10, 20);
}
function turtleLeft(d){this.angle-=d;}function turtleRight(d){this.angle+=d;}
function turtleForward(p){var rad=radians(this.angle);var newx=this.x+cos(rad)*p;
var newy=this.y+sin(rad)*p;this.goto(newx,newy);}function turtleBack(p){
this.forward(-p);}function turtlePenDown(){this.penIsDown=true;}
function turtlePenUp(){this.penIsDown = false;}function turtleGoTo(x,y){
if(this.penIsDown){stroke(this.color);strokeWeight(this.weight);
line(this.x,this.y,x,y);}this.x = x;this.y = y;}function turtleDistTo(x,y){
return sqrt(sq(this.x-x)+sq(this.y-y));}function turtleAngleTo(x,y){
var absAngle=degrees(atan2(y-this.y,x-this.x));
var angle=((absAngle-this.angle)+360)%360.0;return angle;}
function turtleTurnToward(x,y,d){var angle = this.angleTo(x,y);if(angle< 180){
this.angle+=d;}else{this.angle-=d;}}function turtleSetColor(c){this.color=c;}
function turtleSetWeight(w){this.weight=w;}function turtleFace(angle){
this.angle = angle;}function makeTurtle(tx,ty){var turtle={x:tx,y:ty,
angle:0.0,penIsDown:true,color:color(128),weight:1,left:turtleLeft,
right:turtleRight,forward:turtleForward, back:turtleBack,penDown:turtlePenDown,
penUp:turtlePenUp,goto:turtleGoTo, angleto:turtleAngleTo,
turnToward:turtleTurnToward,distanceTo:turtleDistTo, angleTo:turtleAngleTo,
setColor:turtleSetColor, setWeight:turtleSetWeight,face:turtleFace};
return turtle;}
In the process of writing my code, I realized that the turtles each look like little ants. So, I made a shadow descend upon the ants, and eventually a foot try and stomp them. Don’t worry – none of them actually die, mostly because that would be annoying to code.