I am making a game in Java. And the collision between player and enemy is not working right.
What I want: when they collide, it's Game Over and we need to go to the Game Over Panel. What it does instead: the enemy runs over the player and the game just goes on.
I copy pasted a piece of my code and I really hope someone can find what I am doing wrong.
For further information, the player and enemies can should bullets to each other and that collision works. There are also obstacles placed randomly, which both player and enemy detect.
What do to when player collides with an enemy:
for(Enemy enemy: enemies){
if (checkCollision(player, enemy)){
player.dead = true;
}
}
I update the player in the action performed function:
if (!player.dead)
player.redraw();
else
{
spelTimer.stop();
mainVenster.switchPanel(new GameOverPanel(mainVenster));}
Function checkCollision (idk if this can be helpful):
public boolean checkCollision(tanks.Game_Object object1, tanks.Game_Object object2){
double dx = object1.x_pos - object2.x_pos;
double dy = object1.y_pos - object2.y_pos;
double distance = (dx*dx) + (dy*dy);
double minDistance = (object1.r+object2.r);
return (distance < minDistance*minDistance);
}
EDIT: The x_pos and y_pos are the center coordinates of my objects and r is the radius.