我的box2d身体下降太慢了

时间:2015-08-17 19:47:34

标签: android libgdx box2d

我尝试了几乎所有东西,但没有任何作用。 如果你运行它,你会看到一个方块非常缓慢地下降。 我是初学者,所以解释它不是太复杂。 有关此代码的任何问题,请发送电子邮件至dylan.missu@gmail.com 这是我的代码:

@Override
public void show() {
    camera = new OrthographicCamera();
    debugRenderer = new Box2DDebugRenderer();

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(100,100);
    body = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Gdx.graphics.getHeight()/6,Gdx.graphics.getHeight()/6);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1f;
    fixtureDef.restitution = 2;
    Fixture fixture = body.createFixture(fixtureDef);
}
private void line(float X, float Y, float w, float h) 
{
    BodyDef bodyDef = new BodyDef();
    bodyDef.type=BodyDef.BodyType.StaticBody;
    bodyDef.position.set(X,Y);
    PolygonShape polygonShape=new PolygonShape();
    polygonShape.setAsBox(w,h);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape=polygonShape;
    fixtureDef.restitution=0.4f;
    fixtureDef.friction=0.5f;
    Body theFloor=world.createBody(bodyDef);
    theFloor.createFixture(fixtureDef);
}
private void wall(float A)
{
    // is there a better way of doing this?
    line(0,Gdx.graphics.getHeight()/2-A,Gdx.graphics.getWidth()/2-A,0);
    line(Gdx.graphics.getWidth()/2-A,0,0,Gdx.graphics.getHeight()/2-A);
    line(0,-Gdx.graphics.getHeight()/2+A,Gdx.graphics.getWidth()/2-A,0);
    line(-Gdx.graphics.getWidth()/2+A,0,0,Gdx.graphics.getHeight()/2-A);
}
@Override
public void render(float delta) {
    world.step(1 / 5f, 6, 2);

    OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    processAccelerometer();
    wall(1);

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    debugRenderer.render(world, camera.combined);
}

@Override
public void dispose() {
    world.dispose();
    debugRenderer.dispose();
}

private void processAccelerometer() {

    float y = Gdx.input.getAccelerometerY();
    float x = Gdx.input.getAccelerometerX();

    if (prevAccelX != x || prevAccelY != y) {

        world.setGravity(new Vector2(y, -x));

        prevAccelX = x;
        prevAccelY = y;
    }
}
@Override
public void hide()
{
    // TODO: Implement this method
}

@Override
public void resize(int p1, int p2)
{
    // TODO: Implement this method
}

@Override
public void resume()
{
    // TODO: Implement this method
}

@Override
public void pause()
{
    // TODO: Implement this method
}

@Override
public void render()
{
    // TODO: Implement this method
}

}

1 个答案:

答案 0 :(得分:2)

在Box2D中,您必须考虑1个像素= 1米。因此,基本上,您在Box2D中模拟的所有内容默认都是巨大的。 Box2D模拟对于远距离,巨大质量,巨大的速度都不准确......

所以你要做的就是用转换因子转换你的视口,这样你就可以看一下更容易模拟的小实体。

例如,假设您想要100像素= 1米,您可以在创建游戏画面时输入该代码:

WORLD_TO_BOX = 1/100f;  
BOX_TO_WORLD = 1/WORLD_TO_BOX;

//Creation of the camera with your factor 100
camera = new OrthographicCamera();
camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX;  
camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX;  
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);  
camera.update();  

然后,您将按camera.viewportWidthcamera.viewportHeight而不是Gdx.graphics.getWidth()Gdx.graphics.getHeight()

创建您的方框

我的情况你会:

PolygonShape shape = new PolygonShape();
shape.setAsBox(camera.viewportHeight/6,camera.viewportHeight/6);

您可以在我的代码中看到,还有一个BOX_TO_WORLD转换因子。当您想要在box2D主体上渲染图形时,将使用它。 为此,请查看answer of this question