Wednesday, June 22, 2016

xna - 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) enter image description here


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;
}

Answer




I think the problem is that when you are iterating through your collisions (playerCollisionRectangles) -- you are adjusting the position of your player more than once. This would explain the correct behavior when you are passing over single rectangles. When you are passing over more than one rectangle, the depth correction is being fired multiple times, causing jitter.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...