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 made a very simple collision detection, it works fine. However when i decided to load in all my rectangles into a list and then give each of them collision, it only adds it to the first one. Any ideas? I've tried it with a for loop as well.

Heres the code:

    Rectangle interaction, interaction2, interaction3;

    List<Rectangle> collisionList = new List<Rectangle>();

        public GamePlayScreen()
            {

                interaction = new Rectangle(200, 200, 32, 32);
                interaction2 = new Rectangle(400, 200, 32, 32);
                interaction3 = new Rectangle(200, 400, 32, 32);

                collisionList.Add(interaction);
                collisionList.Add(interaction2);
                collisionList.Add(interaction3);

            }
    // This right here is just to show you guys that it is getting updated.
    // This is not the real update, i refrained from posting that since it might confuse
    // people.
    public override Update(GameTime gameTime)
    {
    CheckCollision();
        }

     float xx;
     float yy;

        private void 

CheckCollision()
            {
                foreach (Rectangle rect in collisionList) 
                {
                if (player.Sprite.Bounds.Y > rect.Top - 33 && player.Sprite.Bounds.Y < rect.Bottom && player.Sprite.Bounds.X < rect.Right && player.Sprite.Bounds.X > rect.Left - 30)
                {
                    player.Sprite.position.Y = yy;
                }
                if (player.Sprite.Bounds.Y < rect.Bottom && player.Sprite.Bounds.Y > rect.Top && player.Sprite.Bounds.X < rect.Right && player.Sprite.Bounds.X > rect.Left - 30)
                {
                    player.Sprite.position.Y = yy;
                }
                if (player.Sprite.Bounds.X > rect.Left - 30 && player.Sprite.Bounds.X < rect.Right && player.Sprite.Bounds.Y > rect.Top - 30 && player.Sprite.Bounds.Y < rect.Bottom)
                {
                    player.Sprite.position.X = xx;
                }
                else
                {
                    xx = player.Sprite.position.X;
                    yy = player.Sprite.position.Y;
                }
                }

            }

    //The Draw method

    spriteBatch.Draw(scrollDownButtonNormal, interaction, Color.White);
                spriteBatch.Draw(scrollDownButtonNormal, interaction2, Color.White);
                spriteBatch.Draw(scrollDownButtonNormal, interaction3, Color.White);

I will of course continue to work on this, and post a solution if i find one.

Any help is appreciated, thanks :)

~Etarnalazure

UPDATE Just checked with a boolean if the other boxes react, it changes to true. So it means it works just fine (I put the boolean in between each if loop to check if it registered any collision.)

share|improve this question
    
By putting this: xx = player.Sprite.position.X; yy = player.Sprite.position.Y; instead of this: else { xx = player.Sprite.position.X; yy = player.Sprite.position.Y; } It worked just fine (I put it outside the Foreach loop as well). –  Etarnalazure Jul 25 '12 at 1:43
    
I'll post a proper answer in 7 hours. –  Etarnalazure Jul 25 '12 at 1:44
    
So is this solved? –  Keith Thomas Jul 25 '12 at 2:59
    
Yeps, its resolved. I didnt get time to answer the question till now though. –  Etarnalazure Jul 25 '12 at 19:37
add comment

2 Answers

Have you tried to use the Intersect method of Rectangle structure? I don't' know if it resolves your problem but maybe it may be useful to see if your collision algorithm is working well. So, for example, if Bounds is a Rectangle you could do:

if (player.Sprite.Bounds.Intersect(rect))
 ...
share|improve this answer
    
Thats not really what i was looking for. Since with intersect you cant check which side the play comes from. If you had read the code i posted you would see that i check every side of each square to so the that the code can stop the player from moving. –  Etarnalazure Jul 25 '12 at 19:35
add comment
up vote 0 down vote accepted

I've found the answer to the question, it was rather simple honestly.

By putting this:

 xx = player.Sprite.position.X; yy = player.Sprite.position.Y;

instead of this:

else { xx = player.Sprite.position.X; yy = player.Sprite.position.Y; }

It worked just fine (I put it outside the Foreach loop as well).

share|improve this answer
add comment

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.