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 have asked a similar question before except i am more experienced and have more understanding of XNA. I have bullets that shoot whenever you press RightTrigger. I am having trouble though creating bounding boxes around each bullet in the list though. I want to have this so you can shoot the man and when he is hit he will die, except for some reason no bounding boxes are being drawn. I have updated this now because im trying the same method in a platformer after i gave up on the shooter game because i gave up on it because i couldnt get the collision detection right. Now im trying this method again and am having trouble. The thing is, it does work but only on the last ground piece added to the list. Here is the code for the collision detection in the Game1 class:

OLD CODE (DISREGARD)

public bool hitBullet()
    {

        foreach (Bullets bullet in bullets.bulletAmm)
        {
            bulletRect = new Rectangle(
                (int)bullet.position.X,
                (int)bullet.position.Y,
                bullet.size.X,
                bullet.size.Y);

            manRectangle = new Rectangle(
                (int)man.position.X,
                (int)man.position.Y,
                man.pistolTexture.Width,
                man.pistolTexture.Height);             
        }

        return bulletRect.Intersects(manRectangle);           
    }

NEW CODE (PAY ATTENTION)

 public bool HitGround()
    {
        foreach (Ground newGround in ground.groundAmmount)
        {
            groundRectangle = new Rectangle(
                (int)newGround.position.X,
                (int)newGround.position.Y,
                newGround.shortTexture.Width,
                newGround.shortTexture.Height);

            iceBoyRectangel = new Rectangle(
                (int)iceBoy.position.X,
                (int)iceBoy.position.Y,
                iceBoy.texture.Width,
                iceBoy.texture.Height);
        }

        return groundRectangle.Intersects(iceBoyRectangel);
    }

now the same method of

    foreach (Bullets bullet in bullets.bulletAmm) 

or foreach(Ground newGround in ground.groundAmmount);

works for updating and drawing each bullet which works. but not for the bounding boxes. Any suggestions?

EDIT 1:

i noticed now that this method works sometimes, but only for about 1 in every 20 bullets which makes this even stranger.

EDIT 2:

i thought that i had solved this issue but it turns out i havent. I have updated the code to what it is now.

share|improve this question
1  
Is your code transcription accurate? You recreate bulletRect and manRectangle each iteration yet do nothing with them until after the loop finishes. –  Chewy Gumball Feb 21 '13 at 0:47
1  
Take a look at your loop again. You only check for intersection of the last item in the list. –  daddz Feb 25 '13 at 0:51
    
@daddz yes that was it, thanks. i had to create a seperate bool variable and declare it as true if the intersected inside the loop than return the value of that variable. –  Battle_Pasture Feb 25 '13 at 1:02

1 Answer 1

If I understand your question, your bullets update and draw fine, but the collision detection only works sometimes (1 out of 20 or so).

It could be what's called tunneling, where during one frame the bullet is not yet to the man & then the next frame the bullet is past the man (no longer intersecting, has gone right through). This happens especially when bullets move fast (many pixels per frame). In these cases you need to use a sweep test instead of a simple test.

If that sounds like your issue, here is a good helper for that: http://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?page=3

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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