[Solved] How to fix edge catching on tiles

Jul 25, 2012 at 8:00 PM

Hi, I'm have a bit of a problem with a rectangle moving along flat tiled surfaces. The rectangle occasionally collides with the sides of tiles when it should only be colliding with the top.

I've tried playing around with adding velocities when they collide as mentioned in http://farseerphysics.codeplex.com/discussions/273199 but it doesn't seem to work for me. It certainly helps, but if the first collision for the object is the left or right edge of tile (which happens extremely often) it still has the same problems.

I've also tried using rounded rectangles and capsules, but those both introduce weird bouncing problems as well as not completely fixing the edge catching.

I would also like to avoid trying to combine the tiles to make bigger bodies if at all possible.

Does anyone have any other suggestions for things I could try? Thanks.

Jul 25, 2012 at 9:29 PM

I'm not quite sure of the exact problem here, since you mention "sides" and "tops" in the same sentence. Are you making a tile-based ground and your character keeps bouncing on the seams? Or are you making "platform" things that you can jump through, but at the same time you want to be able to walk on it?

If it's the first one, one possible solution is to create an edge collider that goes along the top of the surfaces. If you need to deform the ground, just recalculate the part you're modifying. This should also work if you have jagginess when falling while hugging a wall.

If it's the second problem, I've used this simple line of code (registered as OnCollision delegate):

bool body_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
    if (fixtureB.Body.Position.Y < body.Position.Y - 0.4f) return true;
    return false;
}

As you can see, it checks if the player's altitude is lower than the platforms altitude, and if so, the collision is dismissed. The 0.4f is just an arbitrary value, you can change it to suit your own scale.

Jul 25, 2012 at 11:03 PM

Here is an explanation of why this happens:

http://www.iforce2d.net/b2dtut/ghost-vertices

Jul 26, 2012 at 12:02 AM

You could go back to the capsule and set the moment of inertia to float.PostiveInfinity which should help on your "weird bouncing problems" problem.

Jul 26, 2012 at 2:14 AM
Morbidi wrote:

I'm not quite sure of the exact problem here, since you mention "sides" and "tops" in the same sentence. Are you making a tile-based ground and your character keeps bouncing on the seams? Or are you making "platform" things that you can jump through, but at the same time you want to be able to walk on it?


Sorry, it was the first one. I changed to an edge based system and it seems to be working pretty well, needs some tweeking but I think it will work. Thanks a lot.

And thanks for the link jerrysb, the site has some good stuff.