了解LibGDX坐标系和图形精灵 [英] Understand LibGDX Coordinate system and drawing sprites

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

问题描述

因此,我非常激动地开始将LibGDX用作OUYA和PC的第一个Android标题,但是我遇到了一些与LibGDX有关的问题. (我的所有问题都可以通过查看源代码来回答,但我实际上也在尝试理解设计选择).

首先是坐标系.我使用项目设置" jar创建了一个项目,并且它像这样创建了一个OrthographicCamera

camera = new OrthographicCamera(1, h/w);

从我的阅读中,我了解到LibGdx使用左下角表示0,0和yUp.美好的. 我发现如果愿意,可以很容易地将y更改为y,但是我不理解所创建的下一部分代码.

对于创建的默认精灵,位置设置如下.

logoSprite.setOrigin(logoSprite.getWidth()/2, logoSprite.getHeight()/2);
logoSprite.setPosition(-logoSprite.getWidth()/2, -logoSprite.getHeight()/2);

运行此基本程序时,我看到添加的徽标图像位于屏幕中央.我要了解的是为什么值在设置位置为负值,为什么它使用子画面的宽度和高度而不是视口的图形w和h?如果更改为屏幕的宽度和高度,则会在屏幕右下方的奇数位置绘制图像.

我的下一个问题是sprite.setSize与sprite.setScale.两者为何有区别? (除了setScale保持getWidth和getHeight不变外,它们似乎做同样的事情.

由于我的游戏将大量使用2D相机进行平移,缩放和旋转,因此我试图在开始编写任何代码之前尽可能多地了解libgdx框架.

请注意,我有游戏开发和数学背景,并且使用XNA制作了2D和3D游戏.我发现LibGdx有点令人沮丧,因为它没有像我期望的那样抽象化OpenGL,到目前为止,我一直在尝试的2D绘图似乎比应该的还要混乱!

我还想指出,我打算在动画中使用spine.那应该改变我选择使用y-up还是y-down?

解决方案

如果要在屏幕中央绘制精灵,请在您的create方法中进行

logosprite.setposition(scrw/2-logosprite.getwidth()/2,scrh/2-logosprite.getheight/2);

scrw是您视口的宽度,

scrh是您视口的高度,

这样,您的精灵将位于屏幕中央

sprite.setsize用于设置精灵的大小,sprite.setscale用于缩放大/小纹理时,以使其在所有设备(hdpi.mdpi,xhdpi,ldpi)中保持良好的质量.

如果您使用的是spine,则无需担心,它在libgdx中可以正常工作.

So I am super stoked to start using LibGDX for my first android title for OUYA and PC, but I am running into some snags with LibGDX. (All of my questions can be answered by looking at source, but I am really trying to understand the design choices as well).

To start with, the coordinate system. I created a project using the Project Setup jar, and it creates an OrthographicCamera like so

camera = new OrthographicCamera(1, h/w);

From my reading, I understand that LibGdx uses bottom left corner for 0,0 and yUp. Fine. I see that it is pretty easy to change to y down if I want to, but I am not understanding the next bit of code that was created.

For the default sprite that gets created the position is set like so.

logoSprite.setOrigin(logoSprite.getWidth()/2, logoSprite.getHeight()/2);
logoSprite.setPosition(-logoSprite.getWidth()/2, -logoSprite.getHeight()/2);

When I run this basic program, I see the logo image I have added is centered on the screen. What I am trying to understand is why the values are negative in set position, and why is it using the sprite width and height instead of the graphics w and h of the view port? If I change to the screen width and height, then the image is drawn in some odd position in the lower right hand side of the screen.

My next question is sprite.setSize vs sprite.setScale. Why is the difference between the two? (They appear to do the same thing, except setScale leaves getWidth and getHeight unchanged).

Since my game will be using a 2D camera heavily for panning, zooming and rotation, I am trying to understand as much as I can about the libgdx framework before I start writing any code.

As a side note, I have a game development and math background and I have made several 2D and 3D games using XNA. I am finding LibGdx a bit frustrating as it does not abstract away OpenGL as much as I was expecting it to, and so far the 2D drawing I have been experimenting with seems to be more confusing than it should be!

I also wanted to note that I am planning to use spine for my animations. Should that change my choice to use y-up or y-down?

解决方案

If you want to draw a sprite in center of screen, do this in your create method

logosprite.setposition(scrw/2-logosprite.getwidth()/2,scrh/2-logosprite.getheight/2);

here scrw is your viewport's width,

and scrh is your viewport's height,

this way your sprite will be in center of screen

sprite.setsize is used for setting size of the sprite and sprite.setscale is used when we scale a large/small texture so that its quality remains good in all devices(hdpi.mdpi,xhdpi,ldpi)..

no need to worry if you are using spine it works smoothly in libgdx..

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

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