libGDX Coordinate System

有些话、适合烂在心里 提交于 2020-01-14 03:05:17

问题


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.

I set up my Camera like this:

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

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.

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.


回答1:


Once you change your viewport to match the screen's aspect ratio then (0, 0) will no longer be at the bottom left of the screen unless the screen is square. If the screen is wider than it is high then the visible portion of the x axis will still go from 0.0 to 1.0, but 0.0 on the y axis will now be somewhere off the bottom of the screen.

If you adjust the camera so that (0, 0) is at the bottom left of the screen, and remember that the visible y axis will only go up to grapicsHeight / graphicsWidth then that should solve your coordinate problem.

I would recommend setting the camera to point to the middle of the screen rather than the bottom left. There's an example here that does exactly that, drawing a 2:1 rectangle which is always in the centre of the screen, always with a 2:1 ratio no matter how much you resize it.




回答2:


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

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.

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.



来源:https://stackoverflow.com/questions/15148198/libgdx-coordinate-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!