我目前正在尝试制作游戏,而且我仍然是使用相机的新手,我认为可能需要两个OrthographicCamera
,但我不确定这是否是最多的有效的方式,甚至如何做到这一点。
基本上,我希望这是它的布局:
主区是主要内容,即服务器接口。 游戏等级是实际游戏部分所在的位置。我目前正在使用ScissorStack
来剪切该区域,但是通过此演示,结果让我对如何执行此操作提出疑问:
public class TestScissorStackAndCamera extends ApplicationAdapter {
private SpriteBatch batch;
private OrthographicCamera camera;
private Sprite sprite;
private int width, height;
@Override
public void create() {
Gdx.gl.glClearColor(0, 0, 0, 0);
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera = new OrthographicCamera(width, height);
camera.position.set(width / 2, height / 2, 0);
camera.update();
createSprite();
}
private void createSprite() {
Pixmap map = new Pixmap(width, height, Format.RGBA8888);
map.setColor(Color.RED);
map.fillRectangle(0, 0, width, height);
map.setColor(Color.BLUE);
map.drawLine(width / 2, 0, width / 2, height);
map.drawLine(0, height / 2, width, height / 2);
Texture texture = new Texture(map);
sprite = new Sprite(texture);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined); // The Question!
batch.begin();
{
Rectangle scissors = new Rectangle();
Rectangle area = new Rectangle(10, 10, width - 20, height - 20);
ScissorStack.calculateScissors(camera, batch.getTransformMatrix(), area, scissors);
ScissorStack.pushScissors(scissors);
batch.draw(sprite, 0, 0);
batch.flush();
ScissorStack.popScissors();
}
batch.end();
}
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "ScissorStack & Camera Test";
config.resizable = false;
new LwjglApplication(new TestScissorStackAndCamera(), config);
}
}
质疑batch.setProjectionMatrix(camera.combined)
我在代码中用注释The Question!
标记了一行,这是影响结果的因素。如果我没有它,使用camera.translate(...)
方法,图像将在(0, 0)
处绘制,但它的作用是移动查看的部分。如果我有这条线,当我使用camera.translate(...)
方法时,图像将分别绘制到相机的位置。
对于我正在开发的游戏,如果没有设置projectionMatrix
,它会表现得很笨拙,但是当我设置它时,它会混淆游戏剩余部分的位置。我甚至添加了一些测试功能,并且它没有在正确的ScissorStack
我怎样才能设置两台摄像机,或者我该怎样做才能正确有效地设置我想要的东西?
凭借我的实际游戏(不是模拟游戏),这就是它正在做的事情。它应该在红线内部渲染,但它不是:
如果您希望我的GameLevel的当前代码正在处理ScissorStack
和OrthographicCamera
:
public GameLevel(int x, int y, int displayWidth, int displayHeight) {
this.x = x; // x = 10
this.y = y; // y = 10
this.displayWidth = displayWidth; // displayWidth = Gdx.graphics.getWidth() - x - 10
this.displayHeight = displayHeight; // displayHeight = Gdx.graphics.getHeight() - y - 120
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(displayWidth / 2, displayHeight / 2, 0);
// FBLAGame.batch.setProjectionMatrix(camera.combined);
camera.update();
init();
}
...
@Override
public void render() {
Rectangle area = new Rectangle(x, y, displayWidth, displayHeight);
Rectangle scissor = new Rectangle();
Matrix4 matrix = FBLAGame.batch.getTransformMatrix();
ScissorStack.calculateScissors(camera, matrix, area, scissor);
ScissorStack.pushScissors(scissor);
renderLevel();
FBLAGame.batch.flush();
ScissorStack.popScissors();
Pixmap map = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Format.RGBA8888);
map.setColor(Color.RED);
map.drawRectangle((int) area.x, (int) area.y, (int) area.width, (int) area.height);
Texture t = new Texture(map);
map.dispose();
FBLAGame.batch.draw(t, 0, 0);
}