TLDR; 使用FitViewport和OrthographicCamera。想要在屏幕边缘(不是视口边缘!)之外绘制,而不会丢失对象的自动调整大小以及精美居中的原点。怎么样?
我开发的游戏中一切都居中(因此坐标系的原点应该在正中心)。在另一边应该有从世界外面通过原点到世界外部的球射击,如下所示:
我使用以下 - 简化代码:
// Skipping the package name and imports...
public class Main extends ApplicationAdapter {
private OrthographicCamera camera;
private FitViewport viewport;
private ShapeRenderer renderer;
@Override
public void create () {
this.camera = new OrthographicCamera();
this.viewport = new FitViewport(1080, 1920, camera);
this.viewport.apply();
this.renderer = new ShapeRenderer();
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.renderer.setProjectionMatrix(camera.combined);
this.renderer.begin(ShapeRenderer.ShapeType.Filled);
// For illustrating purposes
this.renderer.setColor(.5f, .5f, .5f, 1);
this.renderer.rect(-Gdx.graphics.getWidth() / 2, -Gdx.graphics.getHeight() / 2, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.renderer.setColor(1, 1, 1, 1);
// Gets drawn perfectly in center
this.renderer.circle(0, 0, 100);
// Gets drawn somewhere else where it does not belong
this.renderer.circle(-Gdx.graphics.getWidth() / 2, -Gdx.graphics.getHeight() / 2, 100, 100);
this.renderer.end();
}
@Override
public void dispose () {
this.renderer.dispose();
}
@Override
public void resize (int width, int height) {
this.viewport.update(width, height);
}
}
...产生这个结果:
我的问题是:如何在边缘外部绘制,同时仍保持自动调整任何绘制对象的大小以适应设备的分辨率以及居中的原点?