flashing problem in animation
in
Programming Questions
•
2 years ago
I have a momentary flashing problem in this short animation when the object is at 180 degrees. There is no logic to it. Any ideas?
/*Animation with strange flash at 180 degrees*/
Fish indecisiveFish;
FishReverse indecisiveFishReverse;
float fishAngle;
void setup () {
size (screen.width*3/4, screen.height*5/6);
fishAngle=245.0;
indecisiveFish=new Fish (color (255, 40, 10));
indecisiveFishReverse=new FishReverse (color (255, 40, 10));
}
void draw() {
background (0, 170, 250);
float angleCondition=fishAngle/180; // declare and create a variable that calculates the division of the angle of the cos and sin movement by 180 to determine the condition when the fish changes direction
int angleConditionConvertedtoInteger = int (angleCondition); // convert this variable from a float type to an integer type
if (angleConditionConvertedtoInteger%2==0) { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that if the remainder is an even number, the fish swims back
indecisiveFishReverse.move (); // move the reverse indecisive fish so that it looks like it is swimming back towards the start line
indecisiveFishReverse.display (); // draw the reverse indecisive fish so that it looks like it is swimming back towards the start line
}
else { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that when the remainder is an odd number, the fish swims forward
indecisiveFish.move(); // move the indecisive fish in its forward direction so that it swims towards the finish line
indecisiveFish.display(); // draw the indecisive fish in its forward direction so that it swims towards the finish line
}
}
// Fish class
class Fish {
float fishX;
float fishY;
color clrFish;
float fishAngleSpeed;
float fishCenterX;
float fishCenterY;
float sizeArcFishMoveX;
float sizeArcFishMoveY;
float fishLocX;
float fishLocY;
float speedXFish;
Fish (color clrFishtemp) { //constructor for the Fish class
clrFish=clrFishtemp;
fishAngleSpeed=0.4; // assign a value of 0.4 to the change of angle used in the cos and sign wave movement of the fish
fishCenterX=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the fish
fishCenterY=height/2; // assign a value of half the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the fish
sizeArcFishMoveX=width/3; // assign a value of a third of the width of the canvas to the variable that will determine the radius of the cos wave motion of the fish
sizeArcFishMoveY=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the radius of the sin wave motion of the fish
}
// method to move the Fish
void move () {
fishX=fishLocX+speedXFish;
fishY=fishLocY;
fishLocX=fishCenterX+sizeArcFishMoveX*cos(radians(fishAngle)); // assign a cos wave movement to the variable that determines the X location of the fish in the animation
fishLocY=fishCenterY+sizeArcFishMoveY*sin(radians(fishAngle)); // assign a sin wave movement to the variable that determines the Y location of the fish in the animation
fishAngle+=fishAngleSpeed; // increment the angle of the fish cos and sin wave movements by 0.4
speedXFish+=(random(0.1, 0.8)); // assign a value that is a random value to move the fish horizontally across the screen
}
// method to display the Fish
void display () {
fill(clrFish);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishX, fishY);
vertex(fishX+50, fishY+40);
vertex(fishX+105, fishY+20);
vertex(fishX+150, fishY+50);
vertex(fishX+90, fishY+80);
vertex(fishX+55, fishY+70);
vertex(fishX+30, fishY+90);
endShape(CLOSE);
arc (fishX+116, fishY+42, 10, 15, 0, PI);
}
}
// Reverse Fish subclass of the superclass Fish
class FishReverse extends Fish {
// constructor for the class called Fish Reverse
FishReverse (color clrFishtemp) {
super ( clrFishtemp);
}
// method to display the Fish Reverse
void display () {
fill(clrFish);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishX, fishY+50);
vertex(fishX+45, fishY+20);
vertex(fishX+100, fishY+40);
vertex(fishX+150, fishY);
vertex(fishX+120, fishY+90);
vertex(fishX+95, fishY+70);
vertex(fishX+60, fishY+80);
endShape(CLOSE);
arc (fishX+34, fishY+42, 10, 15, 0, PI);
}
}
/*Animation with strange flash at 180 degrees*/
Fish indecisiveFish;
FishReverse indecisiveFishReverse;
float fishAngle;
void setup () {
size (screen.width*3/4, screen.height*5/6);
fishAngle=245.0;
indecisiveFish=new Fish (color (255, 40, 10));
indecisiveFishReverse=new FishReverse (color (255, 40, 10));
}
void draw() {
background (0, 170, 250);
float angleCondition=fishAngle/180; // declare and create a variable that calculates the division of the angle of the cos and sin movement by 180 to determine the condition when the fish changes direction
int angleConditionConvertedtoInteger = int (angleCondition); // convert this variable from a float type to an integer type
if (angleConditionConvertedtoInteger%2==0) { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that if the remainder is an even number, the fish swims back
indecisiveFishReverse.move (); // move the reverse indecisive fish so that it looks like it is swimming back towards the start line
indecisiveFishReverse.display (); // draw the reverse indecisive fish so that it looks like it is swimming back towards the start line
}
else { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that when the remainder is an odd number, the fish swims forward
indecisiveFish.move(); // move the indecisive fish in its forward direction so that it swims towards the finish line
indecisiveFish.display(); // draw the indecisive fish in its forward direction so that it swims towards the finish line
}
}
// Fish class
class Fish {
float fishX;
float fishY;
color clrFish;
float fishAngleSpeed;
float fishCenterX;
float fishCenterY;
float sizeArcFishMoveX;
float sizeArcFishMoveY;
float fishLocX;
float fishLocY;
float speedXFish;
Fish (color clrFishtemp) { //constructor for the Fish class
clrFish=clrFishtemp;
fishAngleSpeed=0.4; // assign a value of 0.4 to the change of angle used in the cos and sign wave movement of the fish
fishCenterX=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the fish
fishCenterY=height/2; // assign a value of half the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the fish
sizeArcFishMoveX=width/3; // assign a value of a third of the width of the canvas to the variable that will determine the radius of the cos wave motion of the fish
sizeArcFishMoveY=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the radius of the sin wave motion of the fish
}
// method to move the Fish
void move () {
fishX=fishLocX+speedXFish;
fishY=fishLocY;
fishLocX=fishCenterX+sizeArcFishMoveX*cos(radians(fishAngle)); // assign a cos wave movement to the variable that determines the X location of the fish in the animation
fishLocY=fishCenterY+sizeArcFishMoveY*sin(radians(fishAngle)); // assign a sin wave movement to the variable that determines the Y location of the fish in the animation
fishAngle+=fishAngleSpeed; // increment the angle of the fish cos and sin wave movements by 0.4
speedXFish+=(random(0.1, 0.8)); // assign a value that is a random value to move the fish horizontally across the screen
}
// method to display the Fish
void display () {
fill(clrFish);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishX, fishY);
vertex(fishX+50, fishY+40);
vertex(fishX+105, fishY+20);
vertex(fishX+150, fishY+50);
vertex(fishX+90, fishY+80);
vertex(fishX+55, fishY+70);
vertex(fishX+30, fishY+90);
endShape(CLOSE);
arc (fishX+116, fishY+42, 10, 15, 0, PI);
}
}
// Reverse Fish subclass of the superclass Fish
class FishReverse extends Fish {
// constructor for the class called Fish Reverse
FishReverse (color clrFishtemp) {
super ( clrFishtemp);
}
// method to display the Fish Reverse
void display () {
fill(clrFish);
stroke(1);
strokeWeight (3);
beginShape();
vertex(fishX, fishY+50);
vertex(fishX+45, fishY+20);
vertex(fishX+100, fishY+40);
vertex(fishX+150, fishY);
vertex(fishX+120, fishY+90);
vertex(fishX+95, fishY+70);
vertex(fishX+60, fishY+80);
endShape(CLOSE);
arc (fishX+34, fishY+42, 10, 15, 0, PI);
}
}
2