Libgdx的物体位置和ShapeRenderer的位置不一致

5

我对libgdx还不太熟悉。基本上我创建了一个球在弹跳的世界,并正在进行一些实验。之前,我的摄像机位置是固定的:

http://prntscr.com/567hvo

现在,在渲染方法中增加了以下代码行,使得摄像机始终跟随玩家(即圆形):

camera.position.set(player.getPosition().x, player.getPosition().y, 0); 

玩家是一种身体类型。因此,当我这样做时,它会跟随玩家,但形状渲染器现在表现出奇怪的行为。看看发生了什么:http://prntscr.com/567i9a。尽管我将相机和形状渲染器与玩家的同一位置进行比较,但它们并不处于相同的位置。以下是我的代码:
public class Game extends ApplicationAdapter {

World world = new World(new Vector2(0, -9.8f), true);  
Box2DDebugRenderer debugRenderer;  
OrthographicCamera camera;  
static final int PPM = 100;
static float height,width;
Body player;
RayHandler handler;
PointLight l1;

ShapeRenderer shapeRenderer;

@Override  
public void create() {     
    shapeRenderer = new ShapeRenderer();

    height = Gdx.graphics.getHeight();
    width = Gdx.graphics.getWidth();

    //set camera
     camera = new OrthographicCamera();  
     camera.setToOrtho(false, (width)/PPM/2, (height)/PPM/2);
     camera.update();  

     //Ground body  
     BodyDef groundBodyDef =new BodyDef();  
     groundBodyDef.position.set(new Vector2(width/PPM/2/2, 0));  
     Body groundBody = world.createBody(groundBodyDef);  
     PolygonShape groundBox = new PolygonShape();  
     groundBox.setAsBox((width/PPM/2/2), 0.1f);  
     groundBody.createFixture(groundBox, 0.0f);  

     //wall left
     BodyDef wallLeftDef = new BodyDef();
     wallLeftDef.position.set(0, 0f);
     Body wallLeftBody = world.createBody(wallLeftDef);

     PolygonShape wallLeft = new PolygonShape();
     wallLeft.setAsBox(width/PPM/20/2, height/PPM/2);
     wallLeftBody.createFixture(wallLeft,0.0f);

     //wall right
     BodyDef wallRightDef = new BodyDef();
     wallRightDef.position.set((width/PPM)/2, 0f);
     Body wallRightBody = world.createBody(wallRightDef);

     PolygonShape wallRight = new PolygonShape();
     wallRight.setAsBox(width/PPM/20/2, height/PPM/2);
     wallRightBody.createFixture(wallRight,0.0f);



     //Dynamic Body  
     BodyDef bodyDef = new BodyDef();  
     bodyDef.type = BodyType.DynamicBody;  
     bodyDef.position.set((width/PPM)/2/2, (height / PPM)/2/2);  
     player = world.createBody(bodyDef);  
     CircleShape dynamicCircle = new CircleShape();  
     dynamicCircle.setRadius(5f/PPM);  
     FixtureDef fixtureDef = new FixtureDef();  
     fixtureDef.shape = dynamicCircle;  
     fixtureDef.density = 0.4f;  
     fixtureDef.friction = 0.2f;  
     fixtureDef.restitution = 0.6f;  
     player.createFixture(fixtureDef);  
     debugRenderer = new Box2DDebugRenderer();  

     //Lighting

     handler = new RayHandler(world);
     handler.setCombinedMatrix(camera.combined);
     PointLight l1 = new PointLight(handler,5000,Color.CYAN,width/PPM/2,width/PPM/2/2/2,height/PPM/2/2);

     new PointLight(handler,5000,Color.PURPLE,width/PPM/2,width/PPM/2/1.5f,height/PPM/2/2);


     shapeRenderer.setProjectionMatrix(camera.combined);

}  


public void update() { 
    if(Gdx.input.isKeyJustPressed(Keys.UP)) { 
        player.applyForceToCenter(0, 0.75f, true);
    }
    if(Gdx.input.isKeyJustPressed(Keys.RIGHT)) { 
        player.applyForceToCenter(0.5f, 0f, true);
    }
    if(Gdx.input.isKeyJustPressed(Keys.LEFT)) { 
        player.applyForceToCenter(-0.5f, 0f, true);
    }

}



@Override  
public void dispose() {  
}  

@Override  
public void render() {   



     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  
     debugRenderer.render(world, camera.combined);  
     world.step(1/60f, 6, 2); 
     handler.updateAndRender();




     shapeRenderer.begin(ShapeType.Filled);
     shapeRenderer.setColor(1, 1, 1, 1);
     shapeRenderer.circle(player.getPosition().x, player.getPosition().y, 5f/PPM, 100);
     shapeRenderer.end();

    camera.position.set(player.getPosition().x, player.getPosition().y, 0); 

    camera.update();
    update();


}  

@Override  
public void resize(int width, int height) {  




}  

@Override  
public void pause() {  
}  

@Override  
public void resume() {  
}  

}

1个回答

7

您只设置了shapeRenderers的projectionmatrix一次,因此在渲染时不会更新。

您应该将

shapeRenderer.setProjectionMatrix(camera.combined);

在你的渲染方法之前添加。
 shapeRenderer.begin(ShapeType.Filled);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接