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);
}