• Create Account

## Calculating damage calculation in game loop

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1warnexus  Prime Members

1535
Like
0Likes
Like

Posted 10 February 2013 - 08:44 PM

So my game in Java has collision detection done. Now the problem is calculating the damage the ship takes. Calculating it during the collision detection process seems to be a bad idea seeing as the ship initial lives of 3 decreases to 0 pretty quickly. This is because my collision detection is done using a for each loop.

It would seem the culprit would be the bounding box collision detection in the gameloop.

Each attack from the monsters is only suppose to take out one life from the ship.

The thing is I can only calculate the ship damage if a collision detection happens and the for each loop is the culprit that makes damage calculation goes too quick.

public class Ship extends Sprite implements KeyListener{

private int life;

public Ship(double x, double y) {
// ship life
life = 3;
}

public void setLife(int life)
{
this.life = life;
}

public int getLife()
{
return life;
}

}

public class Game extends Canvas implements KeyListener{

public void run(){
// basic game loop
// do not break out of the game loop when I change the state of the isRunning
while(!isLooping)
{
// laser check for every OneEye monsters in the ArrayList
for(GameComponent component: gameObjects){
if(component instanceof Laser)
{
((Laser) component).checkMonsterCollision(gameObjects);

}
if(component instanceof OneEyeSlashSkill)
{
((OneEyeSlashSkill) component).checkShipCollision(gameObjects);
}
}

}
}

public class OneEyeSlashSkill extends Sprite {

public void checkShipCollision(ArrayList<GameComponent> gameObjects )
{

for(GameComponent component: gameObjects)
{
if(component instanceof Ship)
{
// if collision happens
if((rectangle.intersects(((Ship) component).getRectangle())))
{
((Ship) component).setLife(((Ship) component).getLife() - 1);
// remove ship object

if(((Ship) component).getLife() == 0)
{
Game.getInstance().remove((Ship)component);
// remove the key listener associated with the ship object
Game.getInstance().removeKeyListener((KeyListener) component);
}
}
}
}
}

}

Edited by warnexus, 10 February 2013 - 09:49 PM.

### #2Boscan  Members

117
Like
1Likes
Like

Posted 10 February 2013 - 10:19 PM

Your problem isn't you calculating damage on the collision method (though i think it's wrong); I think you problem is that the OneEyeSkillSlash collides with your ship through various frames, making your ship life decrease rapidly.

So you have some options.

- Destroy the OneEyeSkillSlash after a hit.

- Make your OneEyeSkillSlash not be able to damage again the ship.

- Make your ship invincible for a short time after being hit.

- Implement some other mechanic.

### #3warnexus  Prime Members

1535
Like
0Likes
Like

Posted 10 February 2013 - 11:23 PM

Your problem isn't you calculating damage on the collision method (though i think it's wrong); I think you problem is that the OneEyeSkillSlash collides with your ship through various frames, making your ship life decrease rapidly.

So you have some options.

- Destroy the OneEyeSkillSlash after a hit.

- Make your OneEyeSkillSlash not be able to damage again the ship.

- Make your ship invincible for a short time after being hit.

- Implement some other mechanic.

Ah yes, that is correct. It is checking every frame!

Thank you. I know what to do now.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.