Java Libgdx Orthographic Camera 初始位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21913894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Libgdx Orthographic Camera initial position
提问by 1000Suns
I would like the camera to be positioned correctly but I am getting the result below:
我希望相机正确定位,但我得到以下结果:
It seems like when I resize the window, the map does not get rendered properly. Why does that happen?
似乎当我调整窗口大小时,地图无法正确呈现。为什么会这样?
Code:
代码:
public void render(float delta){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
mapRenderer.setView(camera);
mapRenderer.render(background);
mapRenderer.render(foreground);
shapeRenderer.setProjectionMatrix(camera.combined);
//draw rectangles around walls
for(MapObject object : tiledMap.getLayers().get("walls").getObjects()){
if(object instanceof RectangleMapObject) {
RectangleMapObject rectObject = (RectangleMapObject) object;
Rectangle rect = rectObject.getRectangle();
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
shapeRenderer.end();
}
}
//done drawing rectangles
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
}
@Override
public void show(){
//call the tile map here
//I believe this is called first before render() is called
tiledMap = new TmxMapLoader().load("data/mapComplete.tmx");
mapRenderer = new OrthogonalTiledMapRenderer(tiledMap, 1f);
//initiate shapeRenderer. Can remove later
shapeRenderer = new ShapeRenderer();
shapeRenderer.setColor(Color.RED);
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
采纳答案by jpm
This should center the camera
at the viewport
of the game.
这应该居中camera
在viewport
游戏中。
@Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.position.set(width/2f, height/2f, 0); //by default camera position on (0,0,0)
}
回答by noone
You do not set the position of the camera anywhere. Thus it is looking at (0, 0) by default (which means (0, 0) will be in the center of your screen). The TiledMapRenderer renders the bottom left corner of the map at (0, 0) which means that it will fill the top right quadrant of your screen. That's what you see in your screenshot.
您没有在任何地方设置相机的位置。因此,它默认查看 (0, 0) (这意味着 (0, 0) 将位于屏幕的中心)。TiledMapRenderer 在 (0, 0) 处渲染地图的左下角,这意味着它将填充屏幕的右上角象限。这就是您在屏幕截图中看到的内容。
To set it to the center of the map, you could do something like the following:
要将其设置为地图的中心,您可以执行以下操作:
TiledMapTileLayer layer0 = (TiledMapTileLayer) map.getLayers().get(0);
Vector3 center = new Vector3(layer0.getWidth() * layer0.getTileWidth() / 2, layer0.getHeight() * layer0.getTileHeight() / 2, 0);
camera.position.set(center);