setCenter in Libgdx not available in nightlies package, and setOrigin not working well?

StackOverflow https://stackoverflow.com/questions/23513572

  •  16-07-2023
  •  | 
  •  

سؤال

I would like to change the center point of my image, the position (in the world) works fine, but not the center point (in the sprite), the link for the Sprite API : http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOriginCenter()

only setOrigin is available in the methods for a Sprite, and changing it with -100,-100 or 0,0 does not change anything. Would anyone know why?

sprite = new Sprite(tex);
sprite.setPosition(pos.x, pos.y);
sprite.setOrigin(-100,-100);//(sprite.getWidth()*0.5f, sprite.getHeight()*0.5f);//(0,0);
sprite.setSize(0.5f, 0.5f);
هل كانت مفيدة؟

المحلول

With setOrigin you set the origin of the Sprite, which is responsible for rotation and scaling. But it does not affect the position.
This means, that if you set the origin to P(width/2,height/2) and you rotate your Sprite it will rotate arround his center. If you set the origin to the left lower corner P(0,0) it will rotate arround that corner.
The position instead is always the left, lower corner and it is not affected by the origin.
If you want to hold a position as center point you need to recalculate the left lower corner out of that:

sprite.setPosition(posVector.x-sprite.getWidth()/2, posVector.y-sprite.getHeight()/2);

Note, that the origin is relative to the Sprites position. So P(0,0) is the left lower corner and P(width,height) is the right upper corner.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top