简体   繁体   中英

libgdx sprite dimension meters or pixels?

I have been reading "Learning Libgdx Game development". 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. 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.

Also, the size of the underlying texture is 32 X 32 pixels. WHen I resize, the size of my sprites also changes.

Then, what would be the dimensions of 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. When you move an object X game units, the number of pixels changes based on the camera's projection matrix amongst other settings.

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.

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. The wording I used implied that internally setPosition cares about meters, but you should be doing the scaling yourself. Often times the ratio I see in libgdx is 30 pixels = 1 meter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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