Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've got a bullet and collision data for the bullet, when the bullet collides, it's supposed to cut an integer off of the max health for the object.

Basically heres what I want:

Object is shot, Object health = Object Health - 35

What I put in the code:

        if (game.bullet.BoundingBox.Intersects(game.temko.BoundingBox))
        {
            shotM1911 = true;
            game.bullet.Bullet = false;


        }

        if (shotM1911 == true && gameTime.ElapsedGameTime.Milliseconds == 3)
        {
            shotM1911 = false;

        }

        if (shotM1911 == true)
        {
            game.temko.temkohealth = game.temko.temkohealth - game.bullet.bulletdamage;
        }

I think its just updating too fast for the bullet to call it false again (resulting in the temkos measly 200 health to hit negative numbers real fast) how to I tell it to subtract 35 only once upon collision (and get ready to do it again when shot.

share|improve this question
    
You should use augmented assignment operators as they are more concise: x -= y instead of x = x - y –  congusbongus Apr 30 '13 at 4:14
1  
It may be firing multiple times because of the 3 millisecond wait... Try removing that and test it –  Savlon Apr 30 '13 at 5:10
1  
Why exactly is the 3ms in there? Have you tried stepping through your code with a debugger? –  Tetrad Apr 30 '13 at 7:41
    
I just thought it would make it detect faster lol I'll try it now –  Nero-One May 1 '13 at 0:29
add comment

closed as too localized by Byte56, Anko, msell, bummzack, Nate May 1 '13 at 21:19

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

In most games, bullets are removed from the game once they hit something, so they don't have this problem.

There are two situations where this problem crops up:

  1. When bullets can hit many different objects at different times. Maybe the bullet travels through objects to hit more objects behind it; maybe the bullet bounces around and hits anything in its path
  2. When bullets stick around and deal damage depending on how long an object is touching/intersecting it, such as a fireball or laser beam

Depending on which of the two (or both) applies, you can use several techniques:

  • Add a "grace period/distance" for the bullet once it hits an object, during which it cannot hit any objects. After every collision, record the time and/or location of the collision, and don't let it collide again until enough time has passed or enough distance has been travelled. This is easy to implement but doesn't work well for a lot of edge cases, such as if your bullet will bounce many times in a short time or distance.
  • If damage is a function of how long the bullet was "in collision", calculate collisions at fixed game time intervals, and calculate damage based on the number of intervals the bullet was "in collision". The downside is that you need to have a good understanding and control over your game loop to pull this off.
  • If bullets can hit many objects but only once each, for every bullet remember which objects it has collided with so far. This is also very easy to implement, but may not scale well with the number of bullets/objects and collisions.
share|improve this answer
add comment

You are assuming that each frame will take exactly 3 milliseconds to execute, but if it's less or higher then it will continue to chip away the health.

Additionally you are not using a cumulative value for the elapsed time but instead using the one in ElapsedTime instance which will only relay the time elapsed since the last frame.

var isInvulnerable = false;
var milliseconds = 0;
...

if (game.bullet.BoundingBox.Intersects(game.temko.BoundingBox) && !isInvulnerable)
{
    shotM1911 = true;
    game.bullet.Bullet = false;
    game.temko.temkohealth = game.temko.temkohealth - game.bullet.bulletdamage;
    isInvulnerable = true;
    milliseconds = 0;
}

if (isInvulnerable)
{
    milliseconds += gameTime.ElapsedGameTime.Milliseconds;

    if (milliseconds > 3)
    {
        isInvulnerable = false;
    }
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.