我在屏幕上从Box2D绘制bodys时遇到问题。
你可以看到Box2DDebugRenderer在屏幕上绘制了所有旧位置,我该如何避免呢?
忽略render()中除最后一行之外的所有行。
public class GameRenderer {
SpriteBatch batcher;
private GameWorld myWorld;
private OrthographicCamera cam;
private Box2DDebugRenderer renderer;
public Array<Body> bodies = new Array<Body>();
public GameRenderer(GameWorld myWorld){
cam = new OrthographicCamera();
cam.setToOrtho(true, 320.0f, 200.0f);
batcher = new SpriteBatch();
batcher.setProjectionMatrix(cam.combined);
renderer = new Box2DDebugRenderer();
this.myWorld = myWorld;
}
public void render(){
batcher.begin();
bodies = myWorld.getBodies();
if(bodies != null){
//Entity e = (Entity) bodies.get(0).getUserData();
//batcher.draw(e.getImage(), bodies.get(0).getPosition().x, bodies.get(0).getPosition().y, 10, 10);
}
batcher.end();
renderer.render(myWorld.world, cam.combined);
}
}
答案 0 :(得分:1)
只需设置所需的清晰颜色,然后使用所需的缓冲区调用glClear()以清除。然后,您可以使用新的场景图形自由渲染新帧。
@Override
public void render() {
Gdx.gl.glClearColor( 1, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
// scene render code...
}