# Collision on the first rectangle only - Why?

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.

Here's 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);

}
// 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.

~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).

• 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). Commented Jul 25, 2012 at 1:43
• I'll post a proper answer in 7 hours. Commented Jul 25, 2012 at 1:44
• So is this solved? Commented Jul 25, 2012 at 2:59
• Yeps, its resolved. I didnt get time to answer the question till now though. Commented Jul 25, 2012 at 19:37

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))
...

• 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. Commented Jul 25, 2012 at 19:35

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;


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