无法看到Box2DdebugRenderer的身体轮廓

时间:2017-02-09 15:12:20

标签: libgdx box2d

我现在暂时使用box2ddebugrenderer将尸体渲染到世界中,但我无法看到在运行时在世界中创建的身体的单色轮廓。

这是我游戏的屏幕代码:

public class GameScreen implements Screen {

static final int VIEWPORT_WIDTH = 20;
static final int VIEWPORT_HEIGHT = 13;

World world;
Body ground;

final float TIME_STEP = 1 / 300f;
float accumulator = 0f;

OrthographicCamera camera;
private Box2DDebugRenderer renderer;

@Override
public void render(float delta) {
    //Clear the screen
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    renderer.render(world, camera.combined);

    accumulator += delta;

    while (accumulator >= delta) {
        world.step(TIME_STEP, 6, 2);
        accumulator -= TIME_STEP;
    }
}



@Override
public void show() {
    world = createWorld();
    ground = createGround(world);
    renderer = new Box2DDebugRenderer();

    camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
    camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0f);
    camera.update();

}

public World createWorld() {
    return new World(Constants.WORLD_GRAVITY, true);
}

public Body createGround(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y));
    Body body = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Constants.GROUND_WIDTH / 2, Constants.GROUND_HEIGHT / 2);
    body.createFixture(shape, Constants.GROUND_DENSITY);
    shape.dispose();
    return body;
}
}    

1 个答案:

答案 0 :(得分:0)

需要在BodyDef中定义物理体的BodyType

BodyDef bodyDef=new BodyDef();
bodyDef.BodyType= BodyType.STATIC;

不同常数的值应该是合适的,以便身体像你一样躺在相机视口内 Constants.GROUND_X,
Constants.GROUND_Y,
Constants.GROUND_WIDTH,
Constants.GROUND_HEIGHT

创建小身体并检查,身体轮廓是否可见。
在此之后,如果屏幕上没有任何内容,仍然使用SpriteBatch绘制简单的Sprite。

还有一个条件
可能是你在你的Child Game类中重写了Game的渲染方法而没有调用父渲染方法。