# What could cause a sudden stop in Box2D?

I'm using Box2d for a game, and I have a bug that's driving me nuts. I've simplified the situation down to a square player sliding back and forth frictionlessly on top of a floor composed of a series of square tiles, driven by the left and right keys (which apply a horizontal force). Works great, sliding back and forth across the whole floor.

Except... Every once in a while, the player will suddenly stick at the edge of one of the tiles as if it is hitting a (nonexistent) wall. Further pushes in the same direction it was traveling will fail, but as soon as I push backwards once in the opposite direction, I can push forwards past the sticking point again. The sticking point seems to be random, except for being on the edge of a tile. Happens while going left or right.

For debugging purposes, I keep the Positions/velocity values for the previous two update ticks and print them out when this stop occurs. As an example, here you see the player moving right, decelerating slightly; pos2 should be about 8.7, but it stops dead instead.

tick0:  pos= 8.4636 vel= 7.1875
tick1:  pos= 8.5816 vel= 7.0833
tick2:  pos= 8.5816 vel= 0.0000


So, as the player is 0.8 and the tiles 1.0 wide, the player is stopping just as it is about to cross onto the next tile (8.5816 + 0.8/2 = 8.9816). In fact, I get a collision message (which I ignore except noting that it happened). It only seems to happen at x.5816 (or -x.4184) while moving right, and x.4167 (or -x.5833) while moving left

I said that it's like hitting a wall, but in fact, when it hits a wall, the numbers look more like:

 tick0:  pos0= 12.4131 vel2= 8.4375
tick1:  pos1= 12.5555 vel1= 8.5417
tick2:  pos2= 12.5850 vel0= 0.0000


so it moves further right on the last tick, which puts it in contact with the wall.

Anyone seen anything like this. Any suggestion on how I could be causing this behavior.

• What does your player movement code currently do when a collision is detected? – Anko Dec 18 '12 at 19:37

This is a common issue with Box2D and tile maps. The problem you're facing is referred to as "ghost collisions" and is addressed in the manual.

The simplest solution, in my experience, is to use edge shapes.

Basically instead of making the tiles Box2D boxes, make the tiles out of edge shapes.

Your problem lies in the following words

on top of a floor composed of a series of square tiles

Essentially your box keeps getting caught on the edges of your floor. There is a great article on iforce2d.net about this, you can find it HERE. Some solutions to the problem:

• Use ghost vertices
• Replace your tiles with a single edge
• Replace a section of tiles with a single polygon
• Use a vertex clipping algorithm to remove duplicated vertices and collinear vertices (lying on the same plane/line)

Does this answer help? I've had the same issue with a pinball game and this worked for me.