问题
Im using a tiled tmx map, and I created a class that adds bodies to each tile within a certain layer. This has been working great so far except for when a character or an enemy moves around on the screen, its body gets stuck on an edge between two tiles.
This seems to happen "sometimes" and in certain spots. Jumping makes u unstuck but its annoying when it happens, and i tried increasing the position iterations, but the problem keeps reoccurring.
Heres what my game looks like: http://i.stack.imgur.com/f5Igm.png I didnt render the tiles so that its easier to see what the tile's body looks like
回答1:
What happens is that an upper dynamic body's "skin" can get embedded into the shapes below it. If the upper body is then moved across the lower shapes and encounters a corner of one of those shapes, then an impulse is generated by the physics engine that's in opposition to the direction of travel.
Boom! Upper body gets stuck.
Here's a zoomed in image showing this for an upper rectangle moving to the right:
The blue dots and lines extending from them are where the Box2D manifold calculation code has determined impulses need to be applied. Note the blue dots and lines that are on the right side of the reddish/brown rectangle. Those are opposing impulses.
A solution, as Colonel Thirty Two suggested in the comments, is to use the "ghost-vertices" mechanism in edge shapes for the lower shapes (or just use a chain shape that effectively calculates the edges for you).
As background, I have my own fork of Box2D that I've been developing and I just pushed out an alternative solution for this very problem that doesn't require the lower shapes to be edge (or chain) shapes. I've also done a write-up of this alternative.
来源:https://stackoverflow.com/questions/25895130/box2d-bodies-that-are-really-close-together-are-getting-stuck