//***************************************************************************************** // Author: Professor Shieu-Hong Lin, MATH/CS Department, Biola University. 2018. // Permission given to students/educators for adapting the code below // for educational purposes, acknowledging the origin of the source here. //***************************************************************************************** //************************************************************************************************ //Create four new fish objects centered at // (400, 400), (430, 450), (500, 520), and (50, 500)respectively //************************************************************************************************ RigidFish myFish1 = new RigidFish(400, 400); RigidFish myFish2 = new RigidFish(430, 450); RigidFish myFish3 = new RigidFish(500, 520); RigidFish myFish4 = new RigidFish(50, 500); RigidFish myFish5 = new RigidFish(350, 560); RigidFish myFish6 = new RigidFish(250, 160); RigidFish myFish7 = new RigidFish(450, 460); //************************************************************************************************ //Create four movingItem objects centered at different locations //************************************************************************************************ MovingItem myLilyPad2 = new MovingItem(170, 300); MovingItem myBall1 = new MovingItem(470, 500); MovingItem myBall2 = new MovingItem(170, 300); MovingItem myBall3 = new MovingItem(370, 100); MovingItem myBall4 = new MovingItem(470, 400); //******************************************************************************************** //Define the setup function, which will be called once in the beginning // to set up some initial settings of the program //******************************************************************************************** void setup() { //Set the canvas size size(800, 800); //Set the background color of the canvas background(192, 64, 0); //Set the stroke color as white stroke(255, 255, 255); } //******************************************************************************************** //Define the draw function, which will be called repeatedly to draw pictures on the screen //******************************************************************************************** void draw() { //Clear the canvas background(192, 64, 0); //******************************************************************************************** //Draw the fish heading toward the corresponding balls. //******************************************************************************************** //myFish1.drawFishHeadToward(myBall1); if (key == 'a') myFish1.drawFishHeadAwawFrom(myBall1); else if (key == 'b') myFish1.drawFishHeadToward(mouseX, mouseY); else myFish1.drawFishHeadToward(myBall1); myFish2.drawFishHeadToward(myBall2); myFish3.drawFishHeadToward(myBall3); myFish4.drawFishHeadToward(myBall4); myFish5.drawFishHeadToward(myLilyPad2); myFish6.drawFishHeadToward(myLilyPad2); myFish7.drawFishHeadToward(myFish6); //******************************************************************************************** //Move the fish toward the corresponding balls in different speed. //******************************************************************************************** //myFish1.keepDistanceFrom(myBall1, 4); if (key == 'a') myFish1.moveToward(myBall1, 3); else if (key == 'b') myFish1.moveToward(mouseX, mouseY, 3); else myFish1.keepDistanceFrom(myBall1, 3); myFish2.moveToward(myBall2, 5); myFish3.moveToward(myBall3, 4); myFish4.moveToward(myBall4, 6); myFish5.keepDistanceFrom(myLilyPad2, 6); myFish6.keepDistanceFrom(myLilyPad2, 3); //myFish7.keepDistanceFrom(myFish6, 3); myFish7.moveToward(myFish6, 2); //******************************************************************************************** //Move the balls in different ways. //******************************************************************************************** //myBall1.moveAsBouncingBall(); //myBall2.moveUpDown(); //myBall3.moveRandom(); //myBall4.moveRandom(); myBall1.moveAsLilypad(90, 0.8, 15, 8); myBall2.moveInEllipse(400, 400, 300, 0.6, 0.5); myBall3.moveRandomV2(); myBall4.moveAsZigZagBouncingBall(3, 0.8); myLilyPad2.moveAsLilypad(100, 0.7, 16, 8); if (keyPressed && key == 'v') { //make the balls Visible myBall1.setVisibility(true); myBall2.setVisibility(true); myBall3.setVisibility(true); myBall4.setVisibility(true); } if (keyPressed && key == 'i') { //make the balls inVisible myBall1.setVisibility(false); myBall2.setVisibility(false); myBall3.setVisibility(false); myBall4.setVisibility(false); } } //***************************************************************************************** // The RigidFish class below defines the behavior of objects from the RigidFish class. //***************************************************************************************** class RigidFish { //******************************************************************************************** // For each object of the RigidFish class: // The following two data variables control the dynamics of the fins and the tail. //******************************************************************************************** float extentionScale; float increment; //***************************************************************************************** // For each object of the RigidFish class: // The following data variables model the shape of the fish //***************************************************************************************** // // float centerX: x coordinate of the center of the fish body // float centerY: y coordinate of the center of the fish body // float width: width of fish body // float fin: distance from head to the middle point of the tips of two fins // float tailLength: length of the fish tail // float tailWwidth: width of fish tail //***************************************************************************************** float centerX, centerY, length, width, fin, tailLength, tailWidth; //***************************************************************************************** // For each object of the RigidFish class: // The following data variables model the colorof the fish //***************************************************************************************** float R, G, B; //***************************************************************************************** // For each object of the RigidFish class: // The following constructor allows us to create a new object of the RigidFish class and // (i) set and propoerly set the center of the fish while // (ii) randmly set the values of the other control variables of the objects. //***************************************************************************************** RigidFish(float myCenterX, float myCenterY) { centerX = myCenterX; centerY = myCenterY; length = random(10, 80); width = random(10, 1.2*length); fin = random(5, 0.7*length); tailLength = random(10, 80); tailWidth = random(10, 80); extentionScale = 0.75 + random(0, 0.5); increment = 0.05 + random(0, 0.05); R = random(0,255); G = random(0,255); B = random(0,255); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to reset the center of the fish obejct. //***************************************************************************************** void setCenter(float myCenterX, float myCenterY) { centerX = myCenterX; centerY = myCenterY; } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to reset the shape of the fish obejct. //***************************************************************************************** void setShape(float myLength, float myWidth, float myFin, float myTailLength, float myTailWidth) { length = myLength; width = myWidth; fin = myFin; tailLength = myTailLength; tailWidth = myTailWidth; } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to reset the flapping parameters of the fish obejct. //***************************************************************************************** void setFlappingParameters(float myExtentionScale, float myIncrement) { extentionScale = myExtentionScale; increment = myIncrement; } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to reset the color of the fish obejct. //***************************************************************************************** void setFishColor(float myR, float myG, float myB) { R= myR; G = myG; B=myB; } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to draw the fish obejct with // fish body centered at (centerX, centerY) while heading toward (headX, headY) //***************************************************************************************** void drawFishHeadToward(float headX, float headY) { //********************************** //Set the fill color of the fish //********************************** fill(R, G, B); //********************************** //Determine the tail of the body //********************************** float tailX = centerX + (centerX - headX); float tailY = centerY + (centerY - headY); //********************************************************************* //Determine the orientation direction of the body: tail toward head //********************************************************************* float directionX = headX - tailX; float directionY = headY - tailY; //************************************************ //Determine the scaling factor of the body length //************************************************ float directionLength = sqrt(directionX*directionX + directionY*directionY); float lengthScale = (length / directionLength) *0.5; //********************************************************************* //Determine the orientation direction of the body: side to side //********************************************************************* float widthDirectionX = headY - tailY; float widthDirectionY = (-1)*(headX - tailX); //************************************************ //Determine the scaling factor of the body width //************************************************ float widthScale = (width / directionLength) *0.5; //************************************************ //Determine the fin location along of the body (on head to tail line) //************************************************ float finScale = (length / 2 - fin) / length; float finX = centerX + finScale*(2 * lengthScale)*directionX; float finY = centerY + finScale*(2 * lengthScale)*directionY; //************************* //Draw the body as a quad //************************* quad(centerX + lengthScale*directionX, centerY + lengthScale*directionY, finX + widthScale*widthDirectionX, finY + widthScale*widthDirectionY, centerX - lengthScale*directionX, centerY - lengthScale*directionY, finX - widthScale*widthDirectionX, finY - widthScale*widthDirectionY ); //********************************************************************* //Determine the extent of fin movement in a scale of 0.5 to 1.5 //********************************************************************* if (extentionScale> 1.25 || extentionScale< 0.75) increment *= -1; extentionScale += increment; //********************************************************************* //Determine the directions of the fin #1 //********************************************************************* float finDirectionX1 = (finX + widthScale*widthDirectionX) - (centerX + lengthScale*directionX); float finDirectionY1 = (finY + widthScale*widthDirectionY) - (centerY + lengthScale*directionY); //************************* //Draw fin #1 //************************* triangle( finX + widthScale*widthDirectionX, finY + widthScale*widthDirectionY, finX + extentionScale*widthScale*widthDirectionX + 0.5*finDirectionX1, finY + extentionScale*widthScale*widthDirectionY + 0.5*finDirectionY1, finX + (extentionScale - 0.15)*widthScale*widthDirectionX + 0.5*finDirectionX1, finY + (extentionScale - 0.15)*widthScale*widthDirectionY + 0.5*finDirectionY1 ); //********************************************************************* //Determine the directions of the fin #2 //********************************************************************* float finDirectionX2 = (finX - widthScale*widthDirectionX) - (centerX + lengthScale*directionX); float finDirectionY2 = (finY - widthScale*widthDirectionY) - (centerY + lengthScale*directionY); //************************* //Draw fin #2 //************************* triangle(finX - widthScale*widthDirectionX, finY - widthScale*widthDirectionY, finX - extentionScale*widthScale*widthDirectionX + 0.5*finDirectionX2, finY - extentionScale*widthScale*widthDirectionY + 0.5*finDirectionY2, finX - (extentionScale - 0.15)*widthScale*widthDirectionX + 0.5*finDirectionX2, finY - (extentionScale - 0.15)*widthScale*widthDirectionY + 0.5*finDirectionY2 ); //************************************************************ //Determine the scaling factors of the tail: length and width //************************************************************ float tailLengthScale = tailLength / directionLength; float tailWidthScale = (tailWidth / directionLength) * 0.5; //********************************** //Determine the tip of the tail //********************************** float tipX = centerX - lengthScale*directionX; float tipY = centerY - lengthScale*directionY; //************************************************ //Determine the bottom location along of the tail (on head to tail line) //************************************************ float endX = centerX - (lengthScale + tailLengthScale)*directionX; float endY = centerY - (lengthScale + tailLengthScale)*directionY; //************************* //Draw the tail as two triangles //************************* triangle(tipX, tipY, endX + 0.5* tailLengthScale*directionX, endY + 0.5* tailLengthScale*directionY, endX + (2 - extentionScale)*tailWidthScale * widthDirectionX, endY + (2 - extentionScale)*tailWidthScale * widthDirectionY ); triangle(tipX, tipY, endX + 0.5* tailLengthScale*directionX, endY + 0.5* tailLengthScale*directionY, endX - (2 - extentionScale)*tailWidthScale * widthDirectionX, endY - (2 - extentionScale)*tailWidthScale * widthDirectionY ); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to draw the fish obejct with // fish body centered at (centerX, centerY) while heading toward the center of another fish. //***************************************************************************************** void drawFishHeadToward(RigidFish anotherFish) { drawFishHeadToward(anotherFish.centerX, anotherFish.centerY); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to draw the fish obejct with // fish body centered at (centerX, centerY) while heading toward the center of a moving object. //***************************************************************************************** void drawFishHeadToward(MovingItem aMovingObject) { drawFishHeadToward(aMovingObject.centerX, aMovingObject.centerY); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to draw the fish obejct with // fish body centered at (centerX, centerY) while heading away from (awayX, awayY) //***************************************************************************************** void drawFishHeadAwawFrom(float awayX, float awayY) { float headX, headY; headX = centerX + (centerX - awayX); headY = centerY + (centerY - awayY); drawFishHeadToward(headX, headY); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to draw the fish obejct with // fish body centered at (centerX, centerY) while heading away from anotherFish //***************************************************************************************** void drawFishHeadAwawFrom(RigidFish anotherFish) { float headX, headY; headX = centerX + (centerX - anotherFish.centerX); headY = centerY + (centerY - anotherFish.centerY); drawFishHeadToward(headX, headY); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to draw the fish obejct with // fish body centered at (centerX, centerY) while heading away from aMovingObject //***************************************************************************************** void drawFishHeadAwawFrom(MovingItem aMovingObject) { float headX, headY; headX = centerX + (centerX - aMovingObject.centerX); headY = centerY + (centerY - aMovingObject.centerY); drawFishHeadToward(headX, headY); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to move the fish obejct closer // toward a target point (targetX, targetY) with an approaching rate of speed. //***************************************************************************************** void moveToward(float targetX, float targetY, float approachingRate) { //******************************************************************************************** //Update the X and Y coordinates of the center to move it closer toward the target //******************************************************************************************** centerX = centerX + 0.01*approachingRate*(targetX - centerX); centerY = centerY + 0.01*approachingRate*(targetY - centerY); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to move the fish obejct // toward the center of another fish. //***************************************************************************************** void moveToward(RigidFish anotherFish, float approachingRate) { moveToward(anotherFish.centerX, anotherFish.centerY, approachingRate); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to move the fish obejct // toward the center of another fish. //***************************************************************************************** void moveToward(MovingItem aMovingObject, float approachingRate) { moveToward(aMovingObject.centerX, aMovingObject.centerY, approachingRate); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to move the fish obejct to keep a distance // from a target point (targetX, targetY) with a rate of speed. //***************************************************************************************** void keepDistanceFrom(float targetX, float targetY, float apprachingRate) { float distance; //Calculate the Manhattann distance where distance = abs(targetX-centerX) + abs(targetY-centerY)+1; //******************************************************************************************** //Update the X and Y coordinates of the center to move it away from the target //******************************************************************************************** if (distance < 100) //Get away if it is too close { centerX = centerX - 9.95*apprachingRate*(targetX-centerX)/pow(distance, 1.4); centerY = centerY - 9.95*apprachingRate*(targetY-centerY)/pow(distance, 1.4); } else if (distance > 150) //Move closer if it is too far { centerX = centerX + 9.95*apprachingRate*(targetX-centerX)/pow(distance, 1.4); centerY = centerY + 9.95*apprachingRate*(targetY-centerY)/pow(distance, 1.4); } //******************************************************************************************** //Update the X and Y coordinates to make sure it is within the 800*800 frame //******************************************************************************************** if ( centerX > 800 ) centerX= 0; if ( centerY > 800 ) centerY= 0; if ( centerX < 0 ) centerX= 800; if ( centerY < 0 ) centerY= 800; } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to move the fish obejct to keep a distance // from the center of another fish. //***************************************************************************************** void keepDistanceFrom(RigidFish anotherFish, float apprachingRate) { keepDistanceFrom(anotherFish.centerX, anotherFish.centerY, apprachingRate); } //***************************************************************************************** // For each object of the RigidFish class: // The following method allows us to move the fish obejct to keep a distance // from the center of another fish. //***************************************************************************************** void keepDistanceFrom(MovingItem anObject, float apprachingRate) { keepDistanceFrom(anObject.centerX, anObject.centerY, apprachingRate); } } //***************************************************************************************** // The MovingItem class below defines the behavior of moving objects from the MovingItem class //***************************************************************************************** class MovingItem { //***************************************************************************************** // For each object of the MovingItem class: // The following data variables model the center of the object //***************************************************************************************** // // float centerX: x coordinate of the center of the moving object // float centerY: y coordinate of the center of the moving object //***************************************************************************************** float centerX, centerY; //******************************************************************************************** // For each object of the MovingItem class: // The following two data variables control the speed of the moving object. //******************************************************************************************** float x_vel, y_vel; //******************************************************************************************** // For each object of the MovingItem class: // The following data variable control the visibility of the moving object. //******************************************************************************************** boolean visibility; //******************************************************************************************** // For each object of the MovingItem class: // The following data variable control the angular degree of the moving object in the circle. //******************************************************************************************** float angle; //***************************************************************************************** // For each object of the MovingItem class: // The following constructor method sets the center of the moving object // and initializes it with a random speed. //***************************************************************************************** MovingItem(float x, float y) { centerX = x; centerY = y; x_vel = random(2, 9); y_vel = random(2, 9); visibility = false; } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to set the visibility of the moving obejct. //***************************************************************************************** void setVisibility(boolean status) { visibility = status; } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to set the velocity of the moving obejct along X and Y. //***************************************************************************************** void setVelocity(float myX_vel, float myY_vel) { x_vel = myX_vel; y_vel = myY_vel; } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct as a bouncing ball. //***************************************************************************************** void moveAsBouncingBall() { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // Update to the new position //***************************************************************************************** centerX += x_vel; centerY += y_vel; // Bounce off left & right wall if ((centerX<20) || (centerX> 800 -20)) { x_vel = x_vel* -1; } // Bounce off floor & ceiling if ((centerY<20) || (centerY> 800 -20)) { y_vel = y_vel * -1; } } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct zigzagging in some frequency // in some magnitude along the trajectry of a bouncing ball. //***************************************************************************************** void moveAsZigZagBouncingBall(float zigZagFrequency, float zigZagMagnitude) { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // Update to the new position on the trajectory of a bouncing ball //***************************************************************************************** centerX += x_vel; centerY += y_vel; // Bounce off left & right wall if ((centerX<20) || (centerX> 800 -20)) { x_vel = x_vel* -1; } // Bounce off floor & ceiling if ((centerY<20) || (centerY> 800 -20)) { y_vel = y_vel * -1; } //***************************************************************************************** // Add the zigzagging effect //***************************************************************************************** angle = angle + zigZagFrequency/15; if (angle > 2*3.14) angle = angle - 2*3.14; if (angle < 0 ) angle = angle + 2*3.14; if (angle < 3.14) { centerX += y_vel * zigZagMagnitude; centerY += -x_vel * zigZagMagnitude; } else { centerX += -y_vel * zigZagMagnitude; centerY += x_vel * zigZagMagnitude; } } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct as up and down in some . //***************************************************************************************** void moveUpDown() { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // Update y coordinate to the new position //***************************************************************************************** centerY += y_vel; // Bounce off floor, go up if ( (centerY> 800 -20) ) { y_vel = abs(y_vel) * -1; } // Bounce off ceiling, go down if ( centerY<20 ) { y_vel = abs(y_vel); } } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct right and left. //***************************************************************************************** void moveLeftRight() { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // Update x coordinate to the new position //***************************************************************************************** // Your code centerX += x_vel; // Bounce off from the wall on the right, go left // Your code if ( (centerX> 800 -20) ) { x_vel = abs(x_vel) * -1; } // Bounce off from the wall on the right, go right // Your code if ( centerX<20 ) { x_vel = abs(x_vel); } } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct randomly. //***************************************************************************************** void moveRandom() { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // sometimes randomly update the velocity //***************************************************************************************** // Your code if ( random(1, 100) < 3 ) { x_vel = random(-4, 4); y_vel = random(-4, 4); } moveAsBouncingBall(); } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct randomly with 3 control parameters. //***************************************************************************************** void moveRandom(int a, int b, int c) { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // sometimes randomly update the velocity //***************************************************************************************** // Your code if ( random(1, a) < b ) { x_vel = random(-c, c); y_vel = random(-c, c); } moveAsBouncingBall(); } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct randomly. //***************************************************************************************** void moveRandomV2() { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // sometimes randomly update the velocity //***************************************************************************************** // Your code if ( random(1, 100) < 40 ) moveLeftRight(); else moveUpDown(); } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct along a circle in an angular speed. //***************************************************************************************** void moveInCircle(float circleCenterX, float circleCenterY, float radius, float angularSpeed) { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // Update the location of the moving object to move around the circle //***************************************************************************************** centerX = circleCenterX + radius*cos( angle); centerY = circleCenterY + radius*sin( angle); angle = angle + angularSpeed/100; if (angle > 2*3.14) angle = angle - 2*3.14; if (angle < 0 ) angle = angle + 2*3.14; } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct along an ellipse // (as a squeezed circle given some aspect ratio) in an angular speed. //***************************************************************************************** void moveInEllipse(float circleCenterX, float circleCenterY, float radius, float ratio, float angularSpeed) { //************************************************************************** //Clear the canvas and Draw a ball in new position centered at (centerX, centerY): //Set the fill color to the red color, and then draw a circle with radius 20. //************************************************************************** if (visibility == true) { fill(255, 0, 255); ellipse(centerX, centerY-10, 20, 20); } //***************************************************************************************** // Update the location of the moving object to move around the ellipse //***************************************************************************************** centerX = circleCenterX + radius*cos( angle); centerY = circleCenterY + radius*sin( angle) * ratio; angle = angle + angularSpeed/100; if (angle > 2*3.14) angle = angle - 2*3.14; if (angle < 0 ) angle = angle + 2*3.14; } //***************************************************************************************** // For each object of the MovingItem class: // The following method allows us to move the obejct as a lilypad of a given size // as an ellipse (as a squeezed circle given a radius and an aspect ratio) in a speed. //***************************************************************************************** void moveAsLilypad(float radius, float ratio, float driftingSpeed, float frequencyOfChange) { //************************************************************************** //Draw the lily pad: //as a squeezed circle given a radius and an aspect ratio. //************************************************************************** //if (visibility == true) { fill(20, 220, 70); //Make it green ellipse(centerX, centerY, radius, radius*ratio); line(centerX,centerY, centerX+cos(3.14/5 )*radius/2, centerY+ratio*sin(3.14/5 )*radius/2 ); line(centerX,centerY, centerX+cos(3.14/5*3)*radius/2, centerY+ratio*sin(3.14/5*3)*radius/2 ); line(centerX,centerY, centerX+cos(3.14/5*5.5)*radius/2, centerY+ratio*sin(3.14/5*5.5)*radius/2 ); line(centerX,centerY, centerX+cos(3.14/5*7)*radius/2, centerY+ratio*sin(3.14/5*7)*radius/2 ); line(centerX,centerY, centerX+cos(3.14/5*9)*radius/2, centerY+ratio*sin(3.14/5*9)*radius/2 ); } //***************************************************************************************** // Update the location of the lily pad based on the speed and occilation //***************************************************************************************** if (angle < 0) { x_vel = random(-driftingSpeed, driftingSpeed)/10; y_vel = random(-driftingSpeed, driftingSpeed)/10; angle = frequencyOfChange*3.14; println(x_vel, y_vel); } else angle = angle-0.01; //***************************************************************************************** // Update the location of the lily pad based on the speed and the phase of occilation //***************************************************************************************** centerX = centerX + x_vel*cos(angle); centerY = centerY + y_vel*cos(angle); } }