xxxxxxxxxx
//fish physics via Fish_Pond by wendy chu
var fish = []; // fish array
var numFish = 25; // number of fish
var music;
function preload() {
music = loadSound("japanese_music.mp3"); // load music
}
function setup() {
createCanvas(1200,400);
music.setVolume(0.5); // set music volume
music.loop(); // play music on loop
for(var i = 0; i < numFish; i++) {
fish.push(new Fish(random(1,3), random(0,1200), random(0,400), random(0,300), random(2,4))); // create fish
}
}
function draw() {
randomSeed(1);
background(173,224,255);
for(var i = 0; i <fish.length; i++) { // draw fish
fish[i].move();
fish[i].display();
}
}
function mouseClicked() { // pause and play music
if(music.isPlaying() == true) {
music.pause();
}
else {
music.play();
}
}
function Fish(sz, xx, yy, l, sp) {
this.size = sz; // size of fish
this.xOff = xx; // x starting point
this.yOff = yy; // y starting point
this.leng = l; // size of locus
this.speed = sp; // speed
this.direction = 1; // direction of travel
this.Cx = 0; // x center of fish
this.Cy = 0; // y center of fish
this.Vy = this.Cy; // y tail of fish
this.w = 1; // tail wiggle factor
this.move = function() {
this.direct = random(-1,1); // allows fish to move opposite directions
if(this.direct < 0) {
this.direction = -1;
}
else if(this.direct > 0) {
this.direction = 1;
}
}
this.display = function() {
noStroke();
fill(255,119,0);
this.angle = radians(frameCount/this.speed); // angle of movement
this.wiggleRange1 = this.Cx + 4 * this.size; // tail wiggle range
this.wiggleRange2 = this.Cx - 4 * this.size; // tail wiggle range
this.Vx = this.Cx - 20 * this.size; // x tail of fish
push();
translate(this.tx, this.ty);
this.tx = this.xOff + this.leng * cos(this.direction*this.angle); // x location
this.ty = this.yOff + this.leng * sin(this.direction*this.angle); // y location
rotate(this.direction*(this.angle + PI/2));
beginShape();
vertex(this.Cx+18*this.size, this.Cy)
bezierVertex(this.Cx+17*this.size, this.Cy+5*this.size, this.Cx+10*this.size, this.Cy+9*this.size, this.Cx, this.Cy+5*this.size);
bezierVertex(this.Cx-10*this.size, this.Cy+this.size, this.Vx, this.Vy, this.Vx, this.Vy);
bezierVertex(this.Vx, this.Vy, this.Cx-10*this.size, this.Cy-this.size, this.Cx, this.Cy-5*this.size);
bezierVertex(this.Cx+10*this.size, this.Cy-9*this.size, this.Cx+17*this.size, this.Cy-5*this.size, this.Cx+18*this.size, this.Cy);
endShape();
pop();
this.Vy += this.w * 0.8;
if(this.Vy > this.wiggleRange1) {
this.w = -1;
}
else if(this.Vy < this.wiggleRange2) {
this.w = 1;
}
}
}