Can't figure out how to assign a certain number so that my object faces a certain direction
in
Programming Questions
•
1 year ago
- int randw = (int)random(30, 50);
- int randh = (int)random(randw-20, randw);
- class fish{
- int x, y;
- int h;
- int w;
- float dir;
- int r;
- int g;
- int b;
- fish(int p1, int p2, int p3, int p4, float p5, int p6, int p7, int p8){
- x = p1;
- y = p2;
- h = p3;
- w = p4;
- dir = p5;
- r = p6;
- g = p7;
- b = p8;
- }
- }
- //useful constants for Assigment 2
- final int TANK_LEFT = 100;
- final int TANK_RIGHT = 900;
- final int WATER_TOP = 300;
- final int WATER_BOTTOM = 700;
- void setup(){
- fish f1;
- fish f2;
- fish f3;
- fish f4;
- fish f5;
- size(1000, 800);
- //parameters for the class so it varies for each face drawn
- f1 = new fish((int)random(TANK_LEFT+randw, TANK_RIGHT-(randw/2)), (int)random(WATER_TOP, WATER_BOTTOM -(randh/2)), (int)random(randw-20, randw), (int)random(30, 50), random(0, TWO_PI), (int)random(0, 256), (int)random(0, 256), (int)random(0, 256));
- f2 = new fish((int)random(TANK_LEFT+randw, TANK_RIGHT-(randw/2)), (int)random(WATER_TOP, WATER_BOTTOM -(randh/2)), (int)random(randw-20, randw), (int)random(30, 50), random(0, TWO_PI), (int)random(0, 256), (int)random(0, 256), (int)random(0, 256));
- f3 = new fish((int)random(TANK_LEFT+randw, TANK_RIGHT-(randw/2)), (int)random(WATER_TOP, WATER_BOTTOM -(randh/2)), (int)random(randw-20, randw), (int)random(30, 50), random(0, TWO_PI), (int)random(0, 256), (int)random(0, 256), (int)random(0, 256));
- f4 = new fish((int)random(TANK_LEFT+randw, TANK_RIGHT-(randw/2)), (int)random(WATER_TOP, WATER_BOTTOM -(randh/2)), (int)random(randw-20, randw), (int)random(30, 50), random(0, TWO_PI), (int)random(0, 256), (int)random(0, 256), (int)random(0, 256));
- f5 = new fish((int)random(TANK_LEFT+randw, TANK_RIGHT-(randw/2)), (int)random(WATER_TOP, WATER_BOTTOM -(randh/2)), (int)random(randw-20, randw), (int)random(30, 50), random(0, TWO_PI), (int)random(0, 256), (int)random(0, 256), (int)random(0, 256));
- makeFish(f1);
- makeFish(f2);
- makeFish(f3);
- makeFish(f4);
- makeFish(f5);
- }
- /*drawFish draws a fish based on the provided parameters.
- Parameters:
- int x - x coordinate of fish (centre of body)
- int y - y coordinate of fish
- int w - width of fish's body (tail is w/2 more)
- int h - height of fish's body and tail
- int dir - 0 = right, 1 = left
- int r,g,b - fill colour of fish
- */
- void makeFish(fish f) {
- stroke(0);
- fill(f.r, f.g, f.b);
- ellipse(f.x, f.y, f.w, f.h); //body
- int tail; //x-offset for tail's connection to the body
- if (f.dir == (random(PI*0.5, PI*1.5))) { //facing right
- tail = -(f.w/2); //tail is to the left of the body
- //draw an eye on the right side of the head
- fill(255);
- ellipse(f.x+(f.w*.25), f.y-(f.h*.25), 10, 10);
- fill(0);
- ellipse(f.x+(f.w*.25), f.y-(f.h*.25), 5, 5);
- }
- else { //facing left
- tail = (f.w/2); //tail is to the right of the body
- //draw an eye on the left side of the head
- fill(255);
- ellipse(f.x-(f.w*.25), f.y-(f.h*.25), 10, 10);
- fill(0);
- ellipse(f.x-(f.w*.25), f.y-(f.h*.25), 5, 5);
- }
- //draw the tail using the offset calculated above
- fill(f.r, f.g, f.b);
- triangle(f.x+tail, f.y, (f.x+(2*tail)), (f.y+(f.h/2)), (f.x+(2*tail)), (f.y-(f.h/2)));
- }
Then in line 44-48, 5th parameter in, I have something writte as the following: random(0, TWO_PI).
Further down I have line 72 basically saying when the direction variable is between PI*0.5 and PI*1.5, then the object (fish) will have a "left" orientation.
On line 82, I have an else statement. I want this else statement to basically mean the following: For all other values between 0 and PI*2 that is not between PI*0.5 and PI*1.5 the object (fish) will have a "right" orientation.
My logic is probably wrong.
Everytime I try to run it, all I get is fishes having a "left" orientation.
1