libGDX坐标系坐标系、libGDX

2023-09-07 14:07:26 作者:不语也怜惜

我想正确的配置我的摄像机雪碧 S在libGDX显示在一个二维坐标系统正确地与原点在左下角。

I'm trying to properly configure my Camera and Sprites in libGDX to show up in a 2D coordinate system properly with the origin at the bottom left hand corner.

设置我的摄像机是这样的:

cameraWidth = Gdx.graphics.getWidth();
cameraHeight = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, cameraHeight/cameraWidth);

和我建立了我的雪碧就像这样:

And I set up my Sprites like this:

sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setScale(scale);
sprite.setPosition(startX,startY);

我的问题是与 sprite.setSize(X,Y)。如果我把所有的精灵有一个大小(1,纹理纵横比),那么一切都将绘制与正确的显示比例(不smushed或拉伸),但没有吸引在正确的位置。例如,如果我绘制内容在(0,0)时,将绘制其左下角关闭屏幕和向上的像素数的左侧

My problem is with sprite.setSize(x,y). If I set all the sprites to have a size of (1, texture aspect ratio), then everything draws with the right display ratio (not smushed or stretched), but nothing draws in the correct place. For example, if I draw something at (0,0), it will draw with its bottom left corner off the left side of the screen and up a number of pixels.

我已经被周围的比我能得到的东西绘制在不同的地方看到变化 - 也就是,如果我将它设置为(1,显示宽高比)东西看起来pretty的接近正确的地方拉 - 他们只是从他们的中心,而不是他们的左下角画,作为LibGDX规定。唯一的问题是,所有图像显示为smushed或拉伸,这是不行的。

I've noticed by changing around the ratio I can get things to draw in different places - namely if I set it to (1, display aspect ratio) things look pretty close to drawing in the right place - they just draw from their center, not their bottom left corner, as LibGDX specifies. The only problem is that the images all appear as smushed or stretched, which is no good.

这似乎是一个简单的问题,我只是想知道如何设置这个,所以我可以有一个合理的坐标系绘制的东西在正确的地方,正确的纵横比。谢谢你。

This seems like a simple problem and I just want to know how to set this up so I can have a sensible coordinate system that draws things in the right place and in the right aspect ratio. Thanks.

推荐答案

我已经找到了解决这个问题的:

I've found a solution to this problem:

将相机设定为邻(即使它已经正视相机)

Set the camera to ortho (even though it's already an orthographic camera)

camera.setToOrtho(false,1,screen height / screen width);

此外,每个精灵必须有它的位置设置为(X - sprite.getWidth()/ 2,Y - sprite.getHeight()/ 2 我延长了雪碧类,并覆盖了 setPosition两种的方法来解释这一点。现在,每次的位置被设置,雪碧的终了了去的地方,你会认为他们会去,与 setPosition两种(0,0)把它在左下方和 setPosition两种(1,高/宽)在左上角。

Also, each sprite must have its position set to (x - sprite.getWidth()/2, y - sprite.getHeight()/2. I extended the Sprite class and overrode the setPosition method to account for this. Now, every time the position is set, the Sprites end up going where you "would think they'd go", with setPosition(0,0) putting it in the bottom left and setPosition(1,height/width) in the top left.

奇怪的是,由此得出每一个角色周围的(X,Y)点为中心,这将使意义,因为宽/ 2和高度/ 2的位置被减去,除了没有的减去值不作 setPosition两种中心通过左下角的精灵 - 它集中在一个方式,我还没有想出

Oddly enough, this draws every sprite centered around the (x,y) point, which would make sense since width/2 and height/2 were subtracted from the position, except not subtracting the values does not make setPosition center the sprite via the bottom left corner - it's centered in a way I haven't figured out.