[SOLVED] [libGdx] wrong rectangle position after sprite scaling

Hello everyone,

i don’t understand why after scaling a sprite the rectangle that is supposed to be drawn at the same position,scale of the sprite is somehow “off” the way, here is two images that explain my problem:

sprite scale = 1

http://s27.postimg.org/upqk887j7/scale1.png

sprite scale = 0.5

http://s27.postimg.org/gkkr6eyhv/scale2.png

and this is how am setting up the rectangle :

// set get rectangle ;
	public void setRect() {
		Rectangle rect = new Rectangle();

		//change the rectangle size to match the sprite 
		rect.width = sprite.getWidth()*scale;
		rect.height = sprite.getHeight()*scale;
		rect.x = sprite.getX();
		rect.y = sprite.getY();

		this.rectangle = rect;
	}

thank you

Scaling of the sprite is actually a dilation around the sprite’s origin. The origin is not necessarily the same as it’s position. Read more in the libGDX docs.

I expect this would do what you want (at least for this case; you can probably come up with a more robust method since you know what else you need done):


public void setRect() {
      Rectangle rect = new Rectangle();

      float scaleW =  sprite.getWidth()*scale;
      float scaleH = sprite.getHeight()*scale;
      rect.width = scaleW;
      rect.height = scaleH;
      rect.x = sprite.getX() + scaleW / 2;
      rect.y = sprite.getY() + scaleH / 2;

      this.rectangle = rect;
}

thank you but i already figured that out :stuck_out_tongue:
also am scaling for 0.5 only for this example, actually my game need 0.1, but in both case, this cannot be a solution.
i also went through the doc and it still scares the heck out of me just like last year :-\

[quote]public void scale(float amount)
Sets the sprite’s scale relative to the current scale. for example: original scale 2 -> sprite.scale(4) -> final scale 6. The sprite scales out from the origin. This will not affect the values returned by getWidth() and getHeight()
[/quote]

You could do scaling yourself:


public void scale(Sprite s, float scale) {
    s.setSize(s.getWidth() * scale, s.getHeight() * scale);
}

That’s probably the behavior you want or are most comfortable with. It simply changes the size of the Sprite without changing the position returned by getX() etc. Otherwise known as dilation about the position (bottom-left corner in libGDX) It’s at least the same type of scaling you do to your rectangle in your code snippet, so it’ll at least be consistent.

i also tried that before positing and the result (with 0.5scale) was different than the image below but it wasn’t correct either, here it is :

http://s28.postimg.org/58z7s94p9/scale3.png

and the code used is this :

sprite.setSize(sprite.getWidth() * scale, sprite.getHeight() * scale);
public void updateSprite() {
		sprite.setPosition(position.x, position.y);
		sprite.setRotation(getAngle());
		sprite.setColor(color);

	}

	// set get rectangle ;
	public void setRect() {
		Rectangle rect = new Rectangle();

		// change the rectangle size to match the sprite
		rect.width = sprite.getWidth();
		rect.height = sprite.getHeight();
		rect.x = sprite.getX();
		rect.y = sprite.getY();
		

		this.rectangle = rect;
	}

What happens with no rotation? That occurs around the origin as well.

yup you are right, without rotating the sprite everything is perfect, but… why ? and how is this possible too ? (i will try setting the sprite origin to the center)

This is what is happening:

Instead of this:

Your circular sprite hides the effect by looking the same.

yup and this solved it ;D

sprite.setOriginCenter();

thank you !!

Ah, looks like my ninja edit wasn’t fast enough. Technically that 2nd image is “wrong” in that it’s the same situation as the first, but whatever.

You’re welcome. :wink:

lol oops ::slight_smile:
but i get the point
thnx man ^^