0

I'm making a simple fishing game but having trouble with the collision detection. I have 4 classes (fisherman, fish, hook, shark) and need to detect collisions between the fisherman and the shark and between the fish and the hook.

Neither of the collisions are working but I'll just post the shark/fisherman one for now. When I say not working, they do not produce any errors in processing and all the other actions work fine (resetting height when object has reached top of the screen) but it doesn't detect the collision and doesn't stop the game as intended.

//Boolean in shark class returns true when it collides with the Fisherman (player1)

boolean sharkHitBoat(Fisherman player1)
{
  int player1Left = player1.x;
  int player1Right = player1.x + 80;
  int player1Top = player1.y;
  int player1Bottom = player1.y + 20;

  int shark1Left = shark1.x;
  int shark1Right = shark1.x+shark1.w;
  int shark1Top = shark1.y;

  if (shark1Top + shark1.dy >= player1Bottom && shark1Right >= player1Left && shark1Left <= player1Right && shark1Top <= player1Top)
  {
    return true;
  }
  else {
    return false;
  }
} 

//I've then used this if statement in my main class under void draw method

//if shark reaches top then reset, if shark hits boat then game over
    if (shark1.sharkHitBoat(player1) == false && shark1.sharkReachTop() == false)
    {
      shark1.y=1000;
    }
    else if (shark1.sharkHitBoat(player1) == true)
    {
      gameMode = GAMEOVER;
      textSize(60);
      fill(255,30,0);
      text("YOU LOSE", 270, 200);
      textSize(30);
      fill(255,255,255);
      text("PRESS SPACE TO PLAY AGAIN", 200, 230);
    }

// sharkReachTop boolean

boolean sharkReachTop()
  {
    y=y-dy;
    return (y>=110);
  }
codenoob
  • 37
  • 5
  • Could you add a set of example values for `player1.x`, `player1.y`, `shark1.x`, `shark1.y`, `shark1.w` and `shark1.dy` for which you expect a `true`, but get a `false`? – Max Vollmer Apr 29 '18 at 00:14
  • Sorry i'm not quite sure how to do that, when I run the game and the two objects collide (quite clearly) it just resets the sharks position as if it reached the top of the water. How can I retrieve the values of those variables? – codenoob Apr 29 '18 at 02:59
  • Put a break point at the line where it goes wrong and inspect the values. See [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) Alternatively log the values out, e.g. with `std::cout`. – Max Vollmer Apr 29 '18 at 09:06
  • Also your error might be in `sharkReachTop()`, so it would be good if you'd add that method to your question. And I have the feeling `shark1.sharkReachTop() == false` is the opposite of what you actually want. – Max Vollmer Apr 29 '18 at 09:08
  • the sharkReachTop method is correct I've just added it to show you but the way I've written the code F=T and T=F – codenoob Apr 29 '18 at 14:49
  • `y=y-dy;` Are you sure this is correct? You are overwriting `y` everytime you call `sharkReachTop()`. – Max Vollmer Apr 29 '18 at 16:08

0 Answers0