[beginner's level] need some help on how to move a fish nicely!
in
Programming Questions
•
2 years ago
Hi,
Software : Processing
I have almost come to end. This is my final questions, i have tried to solve those questions on my own
for like 4-5 hours (with some research from google) and i have figured out most of them.
However, theres still a few that i cant figure them out.
1) in my setup tab, i cant really setup for the player to both eat smaller fishes, and bounce away from the bigger fishes, i can either do one of them. (either eat all of them, or bounce off all of them)
2) when my fish size goes bigger, a werid small fish (i believe parent fish) is inside the fish...very werid, and
when it gets BIG, the big fish doesnt rotate according to the keys.
3) my fish starts swimming when it begins, any ways to stop him doing that? stick in one position so i can move him?
4) keyboard pressed is not response that nicely, any way to fix?
These are my final questions, hope someone can give me some tips so i can move forward again tomorrow!! Thanks
see attached files!
Software : Processing
I have almost come to end. This is my final questions, i have tried to solve those questions on my own
for like 4-5 hours (with some research from google) and i have figured out most of them.
However, theres still a few that i cant figure them out.
1) in my setup tab, i cant really setup for the player to both eat smaller fishes, and bounce away from the bigger fishes, i can either do one of them. (either eat all of them, or bounce off all of them)
2) when my fish size goes bigger, a werid small fish (i believe parent fish) is inside the fish...very werid, and
when it gets BIG, the big fish doesnt rotate according to the keys.
3) my fish starts swimming when it begins, any ways to stop him doing that? stick in one position so i can move him?
4) keyboard pressed is not response that nicely, any way to fix?
These are my final questions, hope someone can give me some tips so i can move forward again tomorrow!! Thanks
see attached files!
FISH CLASS :
- class Fish {
- float fishX,fishY,velX,velY,fSize;
- float sharkX,sharkY;
- PVector acc,vel,pos;
- boolean fishAlive;
- color c;
- Fish() {
- fishX = random(width);
- fishY = random(height);
- sharkX = 1000;
- sharkY = 500;
- pos = new PVector( random(width), random(height) );
- vel = new PVector( 0,0 );
- acc = new PVector( 0, 0);
- velX = random(0.5, 1);//determine random speed
- velY = random(0.5, 1);
- fSize = random(0.1,0.5);
- fishAlive = true;
- c = color(random(255), random(255), random(255));
- }
- void caught(){
- velX = 0;
- velY = 0;
- fishY = -1000;
- fishAlive = false;
- }
- void update() {
- vel.add(acc); //instant forces that need to be include in vel
- pos.add(vel); //always do this to move particle
- acc.set(0, 0, 0);
- }
- boolean fishDetections(Fish otherFish) //detect other fish method, being called on top
- {
- if ( abs(fishX - otherFish.fishX) < 50*(fSize + otherFish.fSize)
- && abs(fishY - otherFish.fishY) < 30*(fSize + otherFish.fSize))//detects other fish and turn around
- {
- return true;
- }
- return false;
- }
- void bounce(Fish otherFish){
- float angle = atan2 (fishY - otherFish.fishY, fishX -otherFish.fishX);
- velX = 1;
- velY = sin(radians(fishX)*2);
- otherFish.velX = 1;
- otherFish.velY = sin(radians(fishX)*2);
- }
- void boundary()
- {
- if (fishX > width-32) {
- fishX = width-32;
- velX *= -1;
- }
- if (fishX < +23) { //added value to make it more precise
- fishX = 23;
- velX *= -1;
- }
- if (fishY > height -30) {
- fishY = height-30;
- velY *= -1;
- }
- if (fishY < 20 && fishAlive) {
- fishY = 20;
- velY *= -1;
- }
- }
- void drawMe() {
- int i = 0;
- if (fishAlive) {
- /*velY = sin(radians(fishX))*40;*/
- fishX += velX;
- velY = sin(radians(fishX)*2);
- /*fishY += velY*sin(theta);*/
- }
- else {
- fishY +=velY;
- }
- fishX += velX;
- fishY += velY;
- fill(c);
- pushMatrix();
- translate(fishX,fishY);
- scale(fSize);
- if (velX<0) {
- rotateY(PI);
- }
- if(!fishAlive) {
- rotate(-PI/2);
- }
- ellipse(0,0,10,10);
- //fin
- stroke(0);
- smooth();
- fill(191,171,191);
- curve(0,200,-95,0,-50,0,200,200);
- curve(0,-500,-95,0,-50,0,200,-100);
- //fin top
- fill(191,171,191);
- triangle(0,-20,-20,-70,-40,-20);
- triangle(10,-20,-10,-65,-30,-20);
- triangle(20,-20,0,-60,-20,-20);
- triangle(30,-20,10,-55,-10,-20);
- triangle(40,-20,20,-50,0,-20);
- //body of the fish
- fill(c);
- arc(0,0,100,110,0,PI/2);
- arc(0,0,120,140,0,PI);
- arc(0,0,120,80,PI,TWO_PI-PI/2);
- arc(0,0,120,80,radians(270),radians(360));
- //eyes
- fill(255);
- stroke(0);
- ellipse(30,-10,30,30);
- ellipse(60,-10,30,30);
- fill(0);
- ellipse(30,-10,5,5);
- ellipse(60,-10,5,5);
- //gill
- fill(191,171,191);
- triangle(30,28,10,45,20,15);
- fill(191,171,120);
- triangle(30,20,0,40,10,10);
- //student # special (301087482)
- //Your fish body must be decorated with dots.
- fill(71,74,47);
- strokeWeight(2);
- ellipse(-20,50,5,5);
- ellipse(-10,50,5,5);
- ellipse(-20,40,5,5);
- ellipse(-15,30,5,5);
- ellipse(-20,50,5,5);
- ellipse(-30,40,5,5);
- ellipse(-25,30,5,5);
- ellipse(-20,40,5,5);
- ellipse(-30,30,5,5);
- ellipse(-25,20,5,5);
- ellipse(-75,20,5,5);
- ellipse(-85,10,5,5);
- ellipse(-80,0,5,5);
- //mouth
- fill(255);
- stroke(205,0,0);
- strokeWeight(2);
- ellipse(50,30,15,15);
- popMatrix();
- }
- }
- class PlayerFish extends Fish {
- float angle;
- PlayerFish() {
- super();
- c = (255);
- fishX = 500;
- //fullfilling the assignment student number #2 requirement
- //spawn near the middle of the bottom
- fishY = 700;
- }
- void update() {
- super.update();//call super class update method
- angle = atan2(vel.x, vel.y);
- }
- void addAcc (PVector newAcc) {
- acc.add(newAcc);
- }
- boolean intersect(Fish otherFish){
- float distance = dist(fishX, fishY, otherFish.fishX, otherFish.fishY);
- if (distance < fSize + 50){
- return true;
- }else{
- return false;
- }
- }
- void boundary()
- {
- if (fishX > width-32) {
- fishX = width-32;
- velX *= -1;
- }
- if (fishX < +23) { //added value to make it more precise
- fishX = 23;
- velX *= -1;
- }
- if (fishY > height -30) {
- fishY = height-30;
- velY *= -1;
- }
- if (fishY < 20 && fishAlive) {
- fishY = 20;
- velY *= -1;
- }
- }
- void drawMe() {
- super.drawMe();
- fill(c);
- pushMatrix();
- translate(fishX,fishY);
- scale(0.1);
- fishX += vel.x;
- fishY += vel.y;
- if (vel.x<=0 /*&&fishX<width-52*/) {
- rotateY(PI);
- }
- /*if(!fishAlive) {
- rotate(-PI/2);
- } */
- ellipse(0,0,10,10);
- //fin
- stroke(0);
- smooth();
- fill(191,171,191);
- curve(0,200,-95,0,-50,0,200,200);
- curve(0,-500,-95,0,-50,0,200,-100);
- //fin top
- fill(191,171,191);
- triangle(0,-20,-20,-70,-40,-20);
- triangle(10,-20,-10,-65,-30,-20);
- triangle(20,-20,0,-60,-20,-20);
- triangle(30,-20,10,-55,-10,-20);
- triangle(40,-20,20,-50,0,-20);
- //body of the fish
- fill(c);
- arc(0,0,100,110,0,PI/2);
- arc(0,0,120,140,0,PI);
- arc(0,0,120,80,PI,TWO_PI-PI/2);
- arc(0,0,120,80,radians(270),radians(360));
- //eyes
- fill(255);
- stroke(0);
- ellipse(30,-10,30,30);
- ellipse(60,-10,30,30);
- fill(0);
- ellipse(30,-10,5,5);
- ellipse(60,-10,5,5);
- //gill
- fill(191,171,191);
- triangle(30,28,10,45,20,15);
- fill(191,171,120);
- triangle(30,20,0,40,10,10);
- //student # special (301087482)
- //Your fish body must be decorated with dots.
- fill(71,74,47);
- strokeWeight(2);
- ellipse(-20,50,5,5);
- ellipse(-10,50,5,5);
- ellipse(-20,40,5,5);
- ellipse(-15,30,5,5);
- ellipse(-20,50,5,5);
- ellipse(-30,40,5,5);
- ellipse(-25,30,5,5);
- ellipse(-20,40,5,5);
- ellipse(-30,30,5,5);
- ellipse(-25,20,5,5);
- ellipse(-75,20,5,5);
- ellipse(-85,10,5,5);
- ellipse(-80,0,5,5);
- //mouth
- fill(255);
- stroke(205,0,0);
- strokeWeight(2);
- ellipse(50,30,15,15);
- popMatrix();
- }
- }
enemy class
- class Shark extends Fish {
- Shark () {
- super();
- }
- void drawMe() {
- if (fishAlive) {
- /*velY = sin(radians(fishX))*40;*/
- sharkX += velX + 5;
- /*fishY += velY*sin(theta);*/
- }
- if (sharkX > 1300)
- sharkX = -300;
- pushMatrix();
- translate(sharkX,sharkY);
- rotateY(PI);
- scale(0.8);
- fill(84,84,84);
- noStroke();
- ellipse(0,0,150,45);
- //shark eye
- noStroke();
- fill(255);
- ellipse(-55,-7,10,10);
- fill(155,0,0);
- ellipse(-58,-5.5,5,5);
- //shark teeth
- noStroke();
- fill(255);
- triangle(-65,5,-55,25,-45,10);
- //shark fin
- noStroke();
- fill(84,84,84);
- triangle(-45,0,30,-45,10,-15);
- triangle(-35,20,-15,40,-10,20);
- //shark tail
- triangle(60,0,100,-30,85,1);
- triangle(60,0,100,20,85,1);
- popMatrix();
- }
- }
- import processing.opengl.PGraphicsOpenGL;
- int amount = 15;
- int enemyamount= 1;
- Fish[]fishs = new Fish [amount];
- PlayerFish player = new PlayerFish();
- Shark enemy = new Shark();
- boolean up, down, right, left ;
- float speed = 0.8;
- PlayerFish respawn() {
- return new PlayerFish();
- }
- void keyPressed() {
- if (key == 'w') up = true;
- if (key == 'a') left = true;
- if (key == 's') down = true;
- if (key == 'd') right = true;
- }//keypressed
- void keyReleased() {
- if (key == 'w') up = false;
- if (key == 'a') left = false;
- if (key == 's') down = false;
- if (key == 'd') right = false;
- }//keyReleased
- void setup() {
- size(1000,1000,OPENGL);
- smooth();
- for(int i=0; i<amount; i++)
- {
- fishs [i]=new Fish();
- }
- }
- void draw()
- {
- background(255);
- fill(26,131,135);
- smooth();
- rect(0,0,1000,1000);
- if(up) player.addAcc( new PVector (0,-speed));
- if(down) player.addAcc( new PVector (0,speed));
- if(left) player.addAcc( new PVector (-speed,0));
- if(right) player.addAcc( new PVector (speed,0));
- player.update();
- player.boundary();
- player.drawMe();
- for (int i=0; i<enemyamount; i++) {
- enemy.drawMe();
- }
- for (int i=0; i<amount; i++) {
- fishs[i].boundary();
- fishs[i].drawMe();
- for (int k = 0; k < amount; k++)
- {
- if ( fishs[i].fishAlive && fishs[k].fishAlive && i!=k)
- {
- if ( fishs[i].fishDetections(fishs[k])) //detects other fish and turn around
- {
- float angle = atan2 ( fishs[i].fishY-fishs[k].fishY, fishs[i].fishX-fishs[k].fishX);
- fishs[i].velX = 1 * cos(angle);
- fishs[i].velY = 1.5 * sin(angle);
- fishs[k].velX = 1 * cos(angle - PI);
- fishs[k].velY = 1.5 * sin(angle - PI);
- }
- }
- // && player.intersect(fishs[i])
- if(player.fSize < fishs[i].fSize + 10 && player.intersect(fishs[i]) ) {
- player.bounce(fishs[i]);
- /*fishs[i].caught();*/
- /*player.fSize *= 1.1;*/
- }
- }
- /*for (int k = 0; k > player.fSize; k++) {
- if(player.fishDetections(fishs[k])) {
- player.bounce(fishs[k]);
- }
- }*/
- }
- }
1