2

I am working on a program to visualize an n-body particle simulation. To do this I have created a camera which is used to project particle positions unto the screen. Then, using a spritebatch these particles are rendered in the correct positions. However, for this to work, the origin of the coordinate system must be located at the bottom left of the screen. This is often not the case and instead the origin is located about a fourth of the screen up and to the left in all cases, and this changes when resizing the screen.

How do I guarantee the coordinate system origin is always at the bottom left of the screen, instead of elevated by a few hundred pixels in either axis?

Thank you.

  • 2
    Possible duplicate of [Changing the Coordinate System in LibGDX (Java)](https://stackoverflow.com/questions/7708379/changing-the-coordinate-system-in-libgdx-java) – Kleo G Jan 31 '18 at 14:54

2 Answers2

1

If you decided to use sprite you should stick with proposed workflow and draw them with:

sprite.draw(batch);

I mean, what's the purpose using sprites if you are getting textures and drawing them?

When you draw a texture to screen at position (0,0) it means that it's bottom left corner will be at screen bottom left corner. That' is sprite's "hot-spot" or "origin" is at it's left bottom corner by default.

But you can change that with setOrigin method:

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOrigin-float-float-

That is, origin is kinda "center" of the sprite, so i.e. if it rotates it will rotate around that spot. Also when you draw sprite at some coordinates sprites origin will be exactly at those coordinates. That way you don't have to adjust sprite drawing position.

Check on that page what methods you have for sprite class...

MilanG
  • 6,559
  • 2
  • 29
  • 55
0

I fixed my problem. Instead of using:

sprite.draw(batch);

I used:

batch.setColor(color);
batch.draw(sprite.getTexture(),sprite.getX(),sprite.getY(),sprite.getWidth()*sprite.getScaleX(),sprite.getHeight()*sprite.getScaleY());

I do not know why this worked.