2
votes

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.

4
It's possible your frame rate isn't high enough and even though the bullet looks like it's intersecting the player, the program only sees the frame before and the frame after. You might want to try increasing the threshold minDistance to something twice as big and see how things work. - bcr
how did you update coordinates? - Haifeng Zhang
@bcr I'm going to try and see what that gives. I must say though, when I (the player) shoot at enemies or obstacles (and I hit them), they get removed. - James Mads
@haifzhan I set a public Timer timer at a delay of 50ms. I hope this is what you wanted to know? - James Mads

4 Answers

1
votes

Your player and enemies are represented by circles, right? That's what it seems like from your code. In that case, the correct collision formula is:

double dist = Math.sqrt(Math.sqr(obj1.x + ob1.radius - obj2.x - obj2.radius) + Math.sqr(obj1.y + ob1.radius - obj2.y - obj2.radius));

if (dist < (obj1.radius + obj2.radius)) {
  //collision
}
0
votes

May be

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 (Math.sqrt(distance) < minDistance);    
}
0
votes

For this situation, I would recommend a Rectangle. When you create a Rectangle you tell it the top-left point, then how far it extends in each direction (Like you're trying to figure out in your method). You then just have to update the Rectangle with the player (using rectangle.reshape(x,y,width,height)) , and everywhere you currently check collision, check the player's Rectangle over the Enemy's Rectangle with the collision method.

Rectangle r = new Rectangle(x,y,100,100);
Rectangle q = new Rectangle(x2,y2,100,100);
if(r.intersects(q)){
    //What occurs during collision
}
-2
votes

Actually the best way to modelize collisions in a game on java is to draw 4 hit boxes : A very thin rectangle in top, bottom, left, right of your character. An then if one of the hit boxes of the enemy contains on of the hitboxes of the player (une the intersects method), you will know that there has been a collision, and even better, you wil know the side of the collision.

more info: https://www.youtube.com/watch?v=mdJNtXP1QiY