LibGx Box2D Pixels to Meters仅绘制大型对象

时间:2017-05-07 14:27:26

标签: java oop box2d

我试图了解Box2D物理引擎。 我在LibGX中使用它,我即将遇到问题。 当我从像素转换为米,只绘制大型对象.. 这样说:

public class GameScreen implements Screen{
static int PPM = 100;
Box2DDebugRenderer debugRenderer;
World w = new World(new Vector2(0,-0.981f),true);
OrthographicCamera cam;

public static Body createDynamicBody(World world, int x, int y, int w, 
    int h, int density, int scale) {
    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.DynamicBody;
    def.position.set(x/scale,y/scale);
    Body b = world.createBody(def);
    FixtureDef fdef =new FixtureDef();
    PolygonShape shape = new PolygonShape();
    shape.setAsBox((w/2)/scale,(h/2)/scale);
    fdef.shape = shape;
    b.createFixture(fdef);
    return b;
}

 // initialized when Screen is loaded
 @Override
public void show() {
    cam = new OrthographicCamera();
    font = new BitmapFont();
    debugRenderer = new Box2DDebugRenderer();
    cam.setToOrtho(false,800,480);
    debugRenderer = new Box2DDebugRenderer();
    // this body is not drawn
    Body b = createDynamicBody(w,200,200,150,150,5, PPM);
    // this body is drawn
    Body b2 = createDynamicBody(w,200,200,200,200,5, PPM);
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.2f,0.1f,0.7f,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    debugRenderer.render(w,camera.combined.cpy().scale(PPM,PPM,0));
    w.step(1/60f,6,2);
}

}

1 个答案:

答案 0 :(得分:0)

在createDynamicBody中,您使用int进行缩放。这会导致你在分割时失去精度。简单地用浮动比例替换你的int比例,它应该可以工作。

public static Body createDynamicBody(World world, int x, int y, int w, 
                                            int h, int density, float scale)