0
\$\begingroup\$

In LibGDX I have few sprites that all have set position (0,0). So in theory they all should render in one place on top of each other but what ends up happening is that all the sprites are being rendered in different spot on screen. For all sprites objects all I do is:

Sprite sprite = new Sprite(new Texture("texture.png"));
sprite.setOriginCenter();
sprite.setScale(1/PPM);

And then when rendering it:

batch.begin();
sprite.draw(batch)
...
batch.end();

And here is what I get:

enter image description here

rocket sprite is in completely different place than fire sprite and some other sprites I couldn't even find. I marked "real" 0,0 point with yellow circle. So why are does sprites in different spots? I've heard that it could be because of scaling but since I am setting sprite's origin to center it shouldn't be a problem, right?

\$\endgroup\$
0
\$\begingroup\$

One possible reason I can see is that your textures have different sizes, and the actual rocket and fire only take up a small portion of them (for example, 256x256 image with a 16x16 rocket in the top right corner).

Another possibility might be that you're not applying the transformation while renderering some the sprites. If you're using a Camera remember to call batch.setTransformationMatrix(camera.combined); before batch.begin();.

And as you yourself noted, scaling might also be the source of the issue. You could try this code instead of setting scale:

sprite.setSize(sprite.getWidth() / PPM, sprite.getHeight() / PPM);

See if any of these resolves your issue and leave a comment if they do / don't!

\$\endgroup\$
1
  • \$\begingroup\$ using setSize() instead setScale() solved the issue. Thanks \$\endgroup\$ – matip May 9 '17 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.