libGDX 坐标系 [英] libGDX Coordinate System

查看:45
本文介绍了libGDX 坐标系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 libGDX 中正确配置我的 CameraSprite 以正确显示在 2D 坐标系中,原点位于左下角.

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);

我这样设置我的Sprite:

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

我的问题是 sprite.setSize(x,y).如果我将所有精灵设置为 (1, 纹理纵横比) 的大小,那么所有内容都以正确的显示比例绘制(不模糊或拉伸),但没有绘制在正确的位置.例如,如果我在 (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, display aspect ratio) 事情看起来非常接近在正确的地方 - 他们只是从他们的中心,而不是他们的左下角,正如 LibGDX 指定的那样.唯一的问题是图像都显示为模糊或拉伸,这不好.

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.我扩展了 Sprite 类并覆盖 setPosition 方法来解决这个问题.现在,每次设置位置时,Sprite 最终会去你认为他们会go",将 setPosition(0,0) 放在左下角,setPosition(1,height/width) 放在左上角.

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.

这篇关于libGDX 坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆