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

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

问题描述

所以我非常兴奋地开始将 LibGDX 用于我的第一个 OUYA 和 PC 的 android 标题,但我在使用 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).

首先是坐标系.我使用 Project Setup jar 创建了一个项目,它像这样创建了一个 OrthographicCamera

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

根据我的阅读,我了解到 LibGdx 将左下角用于 0,0 和 yUp.美好的.我发现如果我愿意,可以很容易地更改为 y down,但我不理解创建的下一段代码.

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

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

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.

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

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

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

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.

顺便说一句,我有游戏开发和数学背景,并且使用 XNA 制作了几款 2D 和 3D 游戏.我发现 LibGdx 有点令人沮丧,因为它并没有像我期望的那样抽象出 OpenGL,而且到目前为止,我一直在试验的 2D 绘图似乎比它应该的更令人困惑!

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!

我还想指出,我计划在动画中使用脊椎.这是否应该改变我使用 y-up 或 y-down 的选择?

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?

推荐答案

如果你想在屏幕中心绘制一个精灵,在你的 create 方法中这样做

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

这里的 scrw 是你的视口的宽度,

here scrw is your viewport's width,

而 scrh 是您视口的高度,

and scrh is your viewport's height,

这样你的精灵就会在屏幕的中心

this way your sprite will be in center of screen

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

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

如果您使用的是 spin,则无需担心它在 libgdx 中运行顺畅..

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

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

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