Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx setOrigin and setPosition not working as expected?

Tags:

android

libgdx

I create a camera:

camera = new OrthographicCamera(5.0f, 5.0f * h/w);

Create a sprite:

ballTexture = new Texture(Gdx.files.internal("data/ball.png"));
ballTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(ballTexture, 0, 0, ballTexture.getWidth(), ballTexture.getHeight());
ball = new Sprite(region);

Set the origin, size, and position:

ball.setOrigin(ball.getWidth()/2,ball.getHeight()/2);
ball.setSize(0.5f, 0.5f * ball.getHeight()/ball.getWidth());
ball.setPosition(0.0f, 0.0f);

Then render it:

batch.setProjectionMatrix(camera.combined);
batch.begin();
ball.draw(batch);
batch.end();

But when I render it, the bottom left of my ball sprite is at (0, 0), not the center of it, as I would expect it to be because I set the origin to the center of the sprite. What am I missing?

like image 570
shino Avatar asked Jul 04 '12 03:07

shino


1 Answers

The origin relates to rotation and scaling, as is described in the JavaDocs for the method.

like image 92
nEx.Software Avatar answered Oct 21 '22 17:10

nEx.Software