Genbox / VelcroPhysics

High performance 2D collision detection system with realistic physics responses.
MIT License
672 stars 115 forks source link

Problem with sliding #48

Closed Crumbler closed 6 years ago

Crumbler commented 7 years ago

I'm coding a simple game with SFML.Net and I'm using Farseer Physics(from codeplex). The player is a rectangle with a fixed rotation. The level is just a bunch of tiles(which are static rectangles with a fixed rotation.). However, when the player is sliding across several tiles, he sometimes gets stuck on the corners of tiles that are close together. Here's a gif demonstrating the issue: https://imgur.com/a/LOp4W The character gets stuck on the corners. The scale is 90 pixels to 1 meter. Here is a download of my project(compiled): YetAnotherSFMLApp.zip Edit: I didn't have this issue when using Box2Dx.

louis-langholtz commented 7 years ago

This looks like the "getting stuck" problem that happens when trying to slide a polygon shape over other polygon shapes. You can work around it via a few ways like the "ghost vertices" technique that I think is implemented in the VelcroPhysics ChainShape. Just make the ground (or a layer just above it) that you're trying to slide over out of the ChainShape.

Incidentally, if this is that problem, I've written up a technique that solves it for polygons. If people want to implement it within VelcroPhysics I'd be honored though I don't know for sure what kind of performance hit it'd have. The more eyes on it the better in any case I figure.

Genbox commented 7 years ago

It is as @louis-langholtz says. You can use the extra vertices of ChainShape to link segments together to prevent this from happening.

Crumbler commented 7 years ago

I've found several possible solutions to my problem.

  1. This was suggested by my friend. Have each multi-tile structure as 1 huge body(with hard-coded fixtures). This would make level editing a pain though.

  2. From the iforce2d article - make the edges rounded, It doesn't solve the problem. The player just begins bumping over the ghost vertices.

  3. CollinearSimplify - I found this while browsing the documentation on codeplex. FarseerPhysics has quite a few algorithms. I tried adding each tile as a rectangle. Vertices ver = new Vertices(); ver.AddRange(PolygonTools.CreateRectangle(tempentity.GetGlobalBounds().Width / 90f, tempentity.GetGlobalBounds().Height / 90f)); ver = FarseerPhysics.Common.PolygonManipulation.SimplifyTools.CollinearSimplify(ver, 1f); BodyFactory.CreatePolygon(world, ver, 1f); However, using this algorithm produces a huge collision box in the center of the level. Am I doing anything wrong?

  4. Using a ChainShape. Unfortunately I lack the skill to code an algorithm that would find groups of tiles and combine them into a group with a ChainShape. My level is just a txt file with bytes(a tileset).

nkast commented 7 years ago

Maybe you can try to add a few rounded 'lengs' to your sprite.

|______________________________|
    O      O      O       O

will this work?

nkast commented 7 years ago

or add a OnSereration event to your tiles, and disable the Contact when the collision cause it to stuck.

experdot commented 7 years ago

@nkast This problem also occurs in the vertical direction.According to your approach,the"legs" must be changed to "tentacles":)

o|          |o
o|          |o
o|__________|o
   o  o  o
Crumbler commented 7 years ago

I think this is what I'm going to do: Add each tile as 4 edges. Then remove overlapping edges and merge the ones that are connected by one point.

Crumbler commented 6 years ago

The problem is solved now. You can close the issue.