//Sihan Dong
//sihand@andrew.cmu.edu
//Section B
//Week 11: Project turtle
var gravity = 0.3; // downward acceleration
var springy = 0.7; // how much velocity is retained after bounce
var drag = 0.0001; // drag causes particles to slow down
var np = 50; // how many particles
var turtleColor = [];
function particleStep() {
this.age++;
this.x += this.dx;
this.y += this.dy;
stroke(this.dr, this.dg, this.db);
strokeWeight(this.dw);
if (this.x > width) { // bounce off right wall
this.x = width - (this.x - width);
this.dx = -this.dx * springy;
} else if (this.x < 0) { // bounce off left wall
this.x = -this.x;
this.dx = -this.dx * springy;
}
if (this.y > height) { // bounce off bottom
this.y = height - (this.y - height);
this.dy = -this.dy * springy;
} else if (this.y < 80) { // bounce off top
this.y = 80 - (this.y - 80);//-this.y;
this.dy = -this.dy * springy;
}
this.dy = this.dy + gravity; // force of gravity
// drag is proportional to velocity squared
// which is the sum of the squares of dy and dy
var vs = Math.pow(this.dx, 2) + Math.pow(this.dy, 2);
// d is the ratio of old velocty to new velocity
var d = vs * drag;
// d goes up with velocity squared but can never be
// so high that the velocity reverses, so limit d to 1
d = min(d, 1);
// scale dx and dy to include drag effect
this.dx *= (1 - d);
this.dy *= (1 - d);
}
function particleDraw() {
point(this.x, this.y);
}
// create a "Particle" object with position and velocity
function makeParticle(px, py, pdx, pdy, pr, pg, pb, pw) {
p = {x: px, y: py,
dx: pdx, dy: pdy,
dr: pr, dg: pg, db: pb,
dw: pw,
age: 0,
step: particleStep,
draw: particleDraw
}
return p;
}
var particles = [];
function setup() {
createCanvas(500, 500);
frameRate(8);
turtleSlope = new makeTurtle(0, 0);
}
function draw() {
background(184, 197, 245);
//draw the chains
for (i = 0; i < 25; i++) {
turtleSlope.penUp();
turtleSlope.right(30);
turtleSlope.setColor(255);
tSlope(20*i, 70); //1
turtleSlope.setColor(0);
tSlope(width/2, height/2-20); //2
turtleColor[i] = color(random(100, 255), random(100, 255), random(100, 255));
turtleSlope.setColor(turtleColor[i]);
tSlope(100*i, 370); //3
fill(0);
}
if (mouseIsPressed) {
var newp = makeParticle(mouseX, mouseY,
random(-10, 10), random(-10, 0),
255, 255, 255,
random(10, 30));
particles.push(newp);
}
newParticles = [];
for (var i = 0; i < particles.length; i++) { // for each particle
var p = particles[i];
p.step();
p.draw();
if (p.age < 200) {
newParticles.push(p);
}
}
particles = newParticles;
}
function tSlope(lx, ly){
turtleSlope.goto(lx, ly);
turtleSlope.setWeight(4);
turtleSlope.right(90);
turtleSlope.forward(random(7));//the amount of fluctuation
turtleSlope.penDown();
turtleSlope.left(60);
turtleSlope.forward(10);
turtleSlope.right(60);
turtleSlope.forward(10);
turtleSlope.right(30);
turtleSlope.forward(17.3);
turtleSlope.right(120);
turtleSlope.forward(17.3);
turtleSlope.right(30);
turtleSlope.forward(10);
turtleSlope.right(60);
turtleSlope.forward(10);
turtleSlope.right(30);
turtleSlope.penUp();
}
//========== TURTLE GRAPHICS ===============//
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 turtleSetColor(c) {
this.color = c;
}
function turtleSetWeight(w) {
this.weight = w;
}
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,
setColor: turtleSetColor, setWeight: turtleSetWeight,
};
return turtle;
}
I’ve wanted to experiment with how turtle moves so I did so with this project. The turnout is not exactly ideal and I will continue practicing with turtles.
I also experimented with the particles because I think their movements are fascinating.