# Array Rectangle Collision Handling Inconsistency XNA 4.0 [closed]

I made a simple collision detection prototype.Here's the code for the Player class's collision handler:

public void handleCollision(GameObject OtherObject)
{
if(this.CollidesWith(OtherObject))
{
if(OtherObject is Stage)
{
float rightEdgeDistance = OtherObject.BoundRect.X - (Position.X + this.BoundRect.Width);
float leftEdgeDistance = OtherObject.BoundRect.X + OtherObject.BoundRect.Width - Position.X;

float TopEdgeDistance = OtherObject.BoundRect.Y - (Position.Y + this.BoundRect.Height);
float BottomEdgeDistance = OtherObject.BoundRect.Y + OtherObject.BoundRect.Height - Position.Y;

float Left_Right_SmallerDistance = Math.Min(Math.Abs(rightEdgeDistance), Math.Abs(leftEdgeDistance));
float Top_Bottom_SmallerDistance = Math.Min(Math.Abs(TopEdgeDistance), Math.Abs(BottomEdgeDistance));

float smallerDistance = Math.Min(Math.Abs(Left_Right_SmallerDistance), Math.Abs(Top_Bottom_SmallerDistance));

if (smallerDistance == Math.Abs(leftEdgeDistance))
{
Position.X = OtherObject.BoundRect.X + OtherObject.BoundRect.Width;
Velocity.X = 0;
}
else if (smallerDistance == Math.Abs(rightEdgeDistance))
{
Position.X = OtherObject.BoundRect.X - this.BoundRect.Width;
Velocity.X = 0;
}
else if (smallerDistance == Math.Abs(BottomEdgeDistance))
{
Position.Y = OtherObject.BoundRect.Y + OtherObject.BoundRect.Height;
Velocity.Y = 0;
}
else if (smallerDistance == Math.Abs(TopEdgeDistance))
{
Position.Y = OtherObject.BoundRect.Y - this.BoundRect.Height;
Velocity.Y = 0;
}
}
}


and here's my Player class's Input Handler method I made:

public void inputHandler(GameTime gameTime,GameObject OtherObject)
{
Velocity = Vector2.Zero;

if (Keyboard.GetState().IsKeyDown(Keys.W))
Velocity.Y -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (Keyboard.GetState().IsKeyDown(Keys.S))
Velocity.Y += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (Keyboard.GetState().IsKeyDown(Keys.A))
Velocity.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (Keyboard.GetState().IsKeyDown(Keys.D))
Velocity.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

handleCollision(OtherObject);
Position += Velocity;
}


Also here's my Update method In the Main Game Class.

protected override void Update(GameTime gameTime)
{
this.Exit();

for (int i = 0; i < 3; i++)
player.Update(gameTime, stage[i]);
powerUp.Update(gameTime,player);
base.Update(gameTime);
}


It works, but when I use array it becomes buggy for some elements of the arrays as in the character can still move a bit inside of the rectangle when I press the key to go down and it will stay outside of the top of the rectangle only if I let go of the key and vice versa.

The rectangles are an array of 3. If I were to move and test the 3rd box. It will work perfectly.But if test the 2nd, it will sink in a bit and the 1st a bit more than the 2nd. I use for loop for the update and draw the array elements. I see that the pattern is, the 1st is the first one to be checked in the Main Update method is will suffer the worst collision handling, while the last one will always be perfect. I tried other ways to handle the collision but to no avail. Why is that? And how can I counter this? Help please!