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 am making a action platform 2D game, something like Megaman. I am using XNA to make it. already made player phisics,collisions, bullets, enemies and AIs, map editor, scorolling X Y camera (about 75% of game is finished ). as i progressed i noticed that my game would be more interesting to play if bullets would be destroyed on collision with regular(stationary ) map blocks, only problem is that if i use my collision detection (each bullet with each block) sometimes it begins to lag(btw if my bullet exits the screen player can see it is removed from bullet list)

So how to improve my collision detection so that memory usage would be so high? :) ( on a map 300x300 blocks for example don't think that bigger map should be made);

            int block = 0;
            int bulet= 0;
            bool destroy_bullet = false;

            while (bulet < bullets.Count)
            {
                while (block <  blocks.Count)
                {
                    if (bullets[bulet].P_Bul_rec.Intersects( blocks[block].rect))
                    {//bullets and block are Lists that holds objects of bullet and block classes
                        //P_Bul_rec just  bullet rectangle  

                        destroy_bullet = true;
                    }
                    block++;
                }
                if (destroy_bullet)
                { bullets.RemoveAt(bulet); destroy_bullet = false; }
                else { bulet++; }
                block = 0;
            }
share|improve this question
2  
A CD check between the bullet and a tile should be O(1). If you can convert from screen position to tile location usually Tile[x / tileWidth + (y / tileHeight) * mapWidth] you can completely remove the inner while block. –  ClassicThunder Mar 19 '12 at 23:17
    
Alternately, when you move the bullet, raycast from its current location to the final location, and you won't have fast bullets teleporting through blocks. –  AndrewS yesterday

2 Answers 2

Your collision detection is slow because you're checking for collisions between every bullet and every block, even if they're nowhere near one another. You should implement a space-partitioning scheme such as a quadtree. This will greatly minimize the number of potential collisions you have to detect, and should keep things at a playable speed.

Here's an article that talks specifically about using Quadrees in XNA.

share|improve this answer
    
Thank you for a great answer :) K.G. i'll try it out :) –  Eddy Mar 22 '12 at 16:07

I don't think a Quadtree is necessary, since you already have the blocks/map grid. Basically convert the bullet coordinates to grid coordinates (aka position divided by block size) and then check collisions only with blocks that are near it. I guess you could also check it based on velocity of the bulled (if +x/+y then check only blocks that are forward or upward of the bullet and so on).

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.