Search code examples
box2dgame-physicsphysics-engine

Smoothly move between bodies


I'm not sure if the title is correct (please comment if it is not). I have a sprite (rectangle body), and I'm applying force on it to make it move from left to right and then go backward. The platform consists of a few rectangle blocks of same size, lined up, with no distance between them . What they form is a perfectly straight line. The platform can be flat or sloped.

What I want

The sprite moves smoothly forward and backward on the platform.

What actually happens

The sprite is sometimes clogged at the point it moves from one block to another, even there is no space between blocks at all. Also sometimes at that point a collision is registered.

I have tried to search and found something called "internal corners", but have no clue how to fix this problem. Is there a way to get around? Thanks!


Solution

  • To fix this problem properly you will need to make your ground from a chain shape, or from edges with their 'ghost vertices' set up correctly. Check the Box2D manual for details on what ghost vertices are. A chain shape is basically a bunch of edge shapes, but it takes care of setting up their ghost vertices for you.

    Depending on what stage of the project you are at it may be quite troublesome to switch to using edges/chains. The next best plan would be to clip a piece off the corner of the shapes that are getting stuck, to help them move over each other more easily. A last-ditch option would be to use a circle instead of a rectangle.

    See also:

    http://code.google.com/p/box2d/wiki/FAQ

    http://box2d.org/forum/viewtopic.php?f=3&t=3048

    http://www.cocos2d-iphone.org/forum/topic/31787

    http://www.cocos2d-iphone.org/forum/topic/29462

    http://www.box2d.org/forum/viewtopic.php?f=3&t=8409

    http://www.box2d.org/forum/viewtopic.php?f=3&t=7935

    http://www.box2d.org/forum/viewtopic.php?f=8&t=7917

    http://www.box2d.org/forum/viewtopic.php?f=3&t=7805

    http://www.cocos2d-iphone.org/forum/topic/22084

    http://www.google.com :)