0
\$\begingroup\$

I have a problem where the bodies in my debug draw does not draw correctly, take a look: Box2D demonstration

As you can see, there is some space between the dynamic body and the ground. I have checked the contact listeners and they are indeed colliding, but with a space inbetween. The contact point is not drawn either, which gives the hint of some drawing error (?).

My PPM = 100. If I set it to 1, the problem disappears (dafuq), but that's not really feasable to have a 1 pixel/meter ratio. I have tried to play around with the cameras but without result. Here is a simplified snippet from my screen:

public class GameScreen implements Screen {

    private OrthographicCamera cam;
    private OrthographicCamera b2dcam;

    private World world;
    private Box2DDebugRenderer b2dr;

    private static final float PPM = 100;
    private static final int V_WIDTH = 160;
    private static final int V_HEIGHT = 90;


    public GameScreen() {
        cam = new OrthographicCamera();
        b2dcam = new OrthographicCamera();
        b2dr = new Box2DDebugRenderer();
        world = new World(new Vector2(0, -9.82f), true);

        /* ... create bodies etc ... */
    }


    public void render(float delta) {
        /* I actually use more complex fixed timestep - just simplified here */
        world.step(1/60f, 6, 2);

        // Render
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Align the b2dr renderer to the camera
        b2dr.render(world, cam.combined.cpy().scale(PPM, PPM, 1));
    }

    public void resize(int width, int height) {
        float aspectRatio = (float) width / (float) height;
        float aspectRatio2 = (float)height / (float)width;

        // This is to maintain the same aspect ratio, using virtual screen-size
        if(aspectRatio >= (float) V_WIDTH / (float) V_HEIGHT){
            cam.setToOrtho(false, V_HEIGHT * aspectRatio, V_HEIGHT);

        } else {
            cam.setToOrtho(false, V_WIDTH, V_WIDTH * aspectRatio2);
        } 

        cam.update();
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ How big or small are your objects? Maybe the problem is with float precision? This would explain that the problem does not occur when you are not using PPM \$\endgroup\$
    – admund
    Feb 5, 2015 at 14:35
  • \$\begingroup\$ It was a long time ago and I dont quite remember. I think I eventuellt solved from the answer below, since I was scaling the viewport too much I think. \$\endgroup\$
    – Eric
    Mar 5, 2016 at 19:31

1 Answer 1

1
\$\begingroup\$

http://www.iforce2d.net/b2dtut/gotchas#smallgap

To address this problem there is a tiny space around fixtures which is used as a buffer, in which two adjacent fixtures can agree that they are touching, but there is no overlap so no correction needs to be made - they can smile at each other and relax. This is another value in b2Settings, and while you can change it (b2_linearSlop) you should really know what you are doing before you do.

Basically there is always going to be a distance between stuff, or else stuff would vibrate and jitter. To stabilize things it will come to rest without actually being totally in contact. I guess you could change your scale to make them small enough not to be visible.

Since this is a constant mm distance my guess is that it would be noticeable on smaller scales.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .