# 2D platformer multiple rectangle collision cause jitter

I posted this question two weeks ago: 2D platformer corner collision and I implemented NoobsArePeople2's solution.

Problem is, when player intersects two rectangles that are inside each other, he starts jittering. I don't know how to describe it so I made a video: http://www.youtube.com/watch?v=2AYUGeuxM0c

In the first 4 or so seconds, you can see that when he collides with two rectangles that arent in each other or touching each other, everything works fine, but in the rest of the video, you can see that when player touches side, he starts to jitter (please note that because of fraps, my game was running at quarter of FPS I normaly have, so without fraps the jitter is much faster)

To give you idea how my collision rectangles look now, here is debug mode (each rectangle is randomly colored)

I have couple of algortihms in place that merge rectangles into one (still need to do some work on that), but some simply cannot be merged. One example is the C object on the right. Right now it is made of 7 rectangles (will be 3 after I complete my algorithm) and when player slides on side of it, he starts to jitter.

I suspect problem is in my gravity, but Im not sure. I've spent 5 days on trying to fix this and Im lost.

Heres my code (slighty modified from platformer sample):

        for (int i = 0; i < playerCollisionRectangles.Count; i++)
{
Vector2 depth = Collision.GetIntersectionDepth(player.PlayerRectangleInWorld, playerCollisionRectangles[i]);
if (depth != Vector2.Zero)
{
float absDepthX = Math.Abs(depth.X);
float absDepthY = Math.Abs(depth.Y);

// Resolve the collision along the shallow axis.
if (absDepthY < absDepthX)
{
// Resolve the collision along the Y axis.
player.PlayerPositionY += Convert.ToInt32(depth.Y);
}
else
{
// Resolve the collision along the X axis.
player.PlayerPositionX += Convert.ToInt32(depth.X);
}
}
}


And here is my gravity code (very very simple):

        if (Gravity)
{
//for camera
position.Y += 2;
//position in world
playerPositionY += 2;
}


And lastly here is one of my methods to move player (i have similiar methods to move him left and up):

    public void MoveRight()
{
animate.Enabled = true;
animate.Row = 0;
position.X += speed;
playerPositionX += speed;
}

• In what order are you handling input, applying gravity and doing collision detection? Should be in that order. – David Gouveia Jan 3 '12 at 16:08
• And when you change player.PlayerPositionX/Y inside the collision code, is the player.PlayerRectangleInWorld reflecting the change too? – David Gouveia Jan 3 '12 at 16:11
• Yes to both of those questions. – isklenar Jan 3 '12 at 16:32
• Hmm, and if you suspect gravity, have you tried disabling it to verify if it still occurs? – David Gouveia Jan 3 '12 at 16:40
• Just tried it, problem still persist. – isklenar Jan 3 '12 at 16:49