Confused with image scaling and positioning in libgdx

I have a rather tricky task to wrap my head around the actual side of displaying things with libgdx. So it just seems pretty confusing in terms of what needs to be done to actually render something on the screen. I guess my confusion can be divided into two parts:

  • What exactly needs to be done in terms of creating an image? There's Texture, TextureRegion, TextureAtlas, Sprite, Batch and maybe a few other art-related assets that I'm missing. How does all this communicate and connect with each other? What a "value chain" amongst I think it would be a way to express it.

  • From the point of view of placement, everything that is created from the above material on the monitor or display, how different coordinates and sizes relate and translate to each other and from each other? Let's say there is an X image that I want to put on the screen. IT had its own set of dimensions and coordinates, but then also the viewport size (is there a viewport position?) And camera position (is there a camera size?). Apart from all this, the total dispatch size, which is from Gdx.graphics. Some examples of what I could do could be as follows:

    • X is my "global map", which is larger than my screen size. I want to be able to scroll / pan it. What are the coordinates / positions that I should use when displaying?
    • Y is larger than my screen size. I want to zoom out and always in the center of the screen / display. What scale factor am I using here and what are the coordinates / positions?
    • Z is smaller than my screen size. I want to stick it in the top left corner of the screen and have it stick to the global map I mentioned earlier. Which positioning system am I using?

Sorry if that was a bunch of stuff ... I think tl; dr of this second part is what set of positions / coordinates, sizes and scales should I do everything in terms of?

I know it might be a lot to ask right away, and I also know that most of these things can be found on the internet, but after sifting through tutorial after tutorial, I can't get a straight answer on how it all relates to each other ... Any help would be appreciated.

+3


source to share


2 answers


  • A texture is essentially the raw image data.
  • TextureRegion allows you to capture smaller areas from a larger texture. For example, a common practice is to pack all the images for your game / application into one large texture (LibGDX "TexturePacker" is a separate program that does this) and then use the larger texture regions for your individual graphics. This is because switching textures is slow and hard, and you want to minimize this process.
  • When you stack your images into one large image using TexturePacker, it creates an ".atlas" file that stores the names and locations of your individual images. TextureAtlas allows you to load an .atlas file and then extract the original images for use in your program.
  • Sprite adds position and color properties to the texture. Please note: the texture API does not have methods for setting / getting position or color. The sprites will be your characters and other objects that you can actually move and position on the screen.
  • The / SpriteBatch is an efficient way to paint multiple sprites on the screen. Instead of making draw calls for each sprite one at a time, the package makes multiple draw calls at the same time.

And I hope I don't add to the confusion, but another option that I really like is to use the Actor and Stage classes over the Sprite and SpriteBatch classes. Actor is similar to Sprite, but adds additional functionality to move / animate using the act method. The stage replaces SpriteBatch as it uses its own internal SpriteBatch, so you don't need to explicitly use SpriteBatch.



There is also a whole set of interface components (table, button, text box, slider, progress bar, etc.) that are all Actor-based and work with Scenes.

I cannot help with question 2. I am sticking to UI based applications, so I don’t know the best practices for working with large game worlds. But hopefully someone more knowledgeable in this area can help you with this.

+2


source


This should have taken a long time to answer as a comment, so I replied as another answer ...

I think both Sprite / SpriteBatch and Actor / Stage are equally strong as you can still animate and move with Sprite / SpriteBatch, but Actor / Stop is easier to work with. There are two methods on the stage, called "act" and "draw", which allow the stage to update and draw each actor it contains very easily. You override the action method for each of your participants to specify what action you want to take. Check out some of the different Stage / Actor tutorials with sample code to see how to use it.



Also, I was a little flawed before Actor was equivalent to Sprite, because Sprite contains a texture, but the Actor itself has no graphics component. There is an Actor extension called Image that includes Drawable, so the Image class is actually the equivalent of Sprite. An actor is a base class that provides methods for actions (or "updates"), but it doesn't have to be graphical. I have used Actors for other purposes, such as triggering sound sounds at specific times.

Atlas creates a large texture containing all of your png files and then lets you get regions from it for individual pngs. So the pipeline for getting specific png graphics would be Atlas> Region> Sprite / Image. Both the Image and Sprite classes have scoped constructors.

+1


source







All Articles