Having trouble getting various things to happen in my program. Deals with constructors, changing object direction when it bounces, and 1 more
in
Programming Questions
•
1 year ago
THE CODE IS BELOW THIS WALL OF TEXT (SORRY)
So here's my problem. First I'd like to get the constructor to have all the parameters instead of doing
it individually for each new instance. I tried following an example here:
But didn't work out very well.
Secondly, I'm trying to get my fishes to bounce around based off of the "law of reflection" in physics
around a specified bound for my width and height of an aquarium that isn't there atm. What my fish does
instead is bounce once and then goes to the left of the screen straight horizontally.
I'm not entirely sure what I did wrong. My code for trying to do that is in a function called moveFish(fish f){..}
It's on lines 92-102.
Thirdly, I'd like to say the following:
If i press the left mouse button and if the button pressed was in between a certain bound for height and width,
then the fishes will move away from the point at which it's clicked.
This is what I have so far but i'm not even sure if the syntax for this is right:
if (mousePressed){
if (mouseButton == RIGHT){
if((mouseX>=TANK_LEFT)&& (mouseX<=TANK_RIGHT)){
(do something here)
}
}
}
if (mousePressed){
if (mouseButton == RIGHT){
if(mouseY>=WATER_TOP)&& (mouseY<=WATER_TOP)){
(do something here)
}
}
}
- int randw = (int)random(30, 50);
- int randh = (int)random(randw-20, randw);
- fish f1;
- class fish{
- int x, y;
- int h;
- int w;
- float dir;
- int r;
- int g;
- int b;
- float spd;
- fish(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9){
- x = p1;
- y = p2;
- h = p3;
- w = p4;
- dir = p5;
- r = p6;
- g = p7;
- b = p8;
- spd = p9;
- }
- }
- //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(){
- size(1000, 800);
- // The 1 fish to draw
- 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),
- (int)random(360),
- (int)random(0, 256),
- (int)random(0, 256),
- (int)random(0, 256),
- (int)random(3,7)
- );
- }
- void draw(){
- background(255,255,255);
- makeFish(f1);
- moveFish(f1);
- }
- 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
- /*basically what i'm saying here is that if the direction variable has a number between
- 90 degrees and 270 degrees, then the fish faces left. For all other values it faces right*/
- if (radians((f.dir)60) > PI*0.5 && radians((f.dir)60) < PI*1.5) { //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);
- }
- else { //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);
- }
- //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)));
- }
- void moveFish(fish f){
- //uses trigonometry for f.x and f.y
- f.x = f.x + int(f.spd*cos(radians((f.dir)60)));
- f.y = f.y + int(f.spd*sin(radians((f.dir)60)));
- //check to see if fish collides with borders and adjust accordingly
- if ((f.x + f.w/2 >= TANK_RIGHT) || (f.x - f.w/2 <=TANK_LEFT))
- f.dir = PI - radians((f.dir)60);
- if ((f.y + f.h/2 >= WATER_BOTTOM) || (f.y - f.h/2 <=WATER_TOP))
- f.dir = radians((-f.dir));
- }
1