简体   繁体   English

libgdx精灵尺寸是米还是像素?

[英]libgdx sprite dimension meters or pixels?

I have been reading "Learning Libgdx Game development". 我一直在阅读“学习Libgdx游戏开发”。 I tried the below snippet: 我尝试了以下代码段:

// First the camera object is created with viewport of 5 X 5. 
OrthographicCamera camera = new OrthographicCamera(5, 5);

I have a texture having a dimension of 32 pixels by 32 pixels. 我有一个尺寸为32像素乘32像素的纹理。 I form a sprite out of this 我由此形成一个精灵

Sprite spr = new Sprite(texture);

// I set the size of Spr as 
spr.setSize(1,1);

According to the book the dimensions above are meters and not pixels. 根据这本书,上面的尺寸是米而不是像素。

What I don't understand is how is mapping from meters to pixels happening on the screen? 我不明白屏幕上是如何发生从米到像素的映射的? When I draw the sprite on the screen the size is not even half a meter let alone 1. 当我在屏幕上绘制精灵时,它的大小甚至不到半米,更不用说1。

Also, the size of the underlying texture is 32 X 32 pixels. 同样,基础纹理的大小为32 X 32像素。 WHen I resize, the size of my sprites also changes. 当我调整大小时,子画面的大小也会改变。

Then, what would be the dimensions of spr.setPosition(x, y)? 那么,spr.setPosition(x,y)的尺寸是多少? Will they be meters or pixels? 它们是米还是像素?

The library uses pixels for dimensions like texture size, and meters for in-game units. 该库将像素用于纹理大小等尺寸,并将米用于游戏内单位。

setPosition will move an object in game units. setPosition将以游戏单位移动对象。 When you move an object X game units, the number of pixels changes based on the camera's projection matrix amongst other settings. 当您移动一个对象X个游戏单位时,像素数会根据相机的投影矩阵以及其他设置而变化。

If you think about it, it wouldn't make sense to move in pixels. 如果您考虑一下,以像素为单位移动就没有意义。 If camera A is zoomed in more than cameraB moving X pixels in the view of each camera would require moving two different amounts. 如果相机A的放大倍数大于相机B,则在每个相机的视图中移动X像素将需要移动两个不同的量。

Edit: Sorry, I made some assumptions in your understanding above, partially misunderstood the question, and frankly used the misleading wording. 编辑:很抱歉,我在您的理解中做出了一些假设,部分地误解了该问题,并坦率地使用了误导性措辞。 The key is that the convention of meters for units is not built-in, it's one that you enforce because the ratio of one pixel to one meter in Box2D wouldn't make sense. 关键是米的惯例不是内置的,这是您必须执行的惯例,因为Box2D中1像素与1米的比率没有意义。 The wording I used implied that internally setPosition cares about meters, but you should be doing the scaling yourself. 我使用的措词暗示内部setPosition关心仪表,但是您应该自己进行缩放。 Often times the ratio I see in libgdx is 30 pixels = 1 meter. 我在libgdx中看到的比例通常是30像素= 1米。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM