Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: can't rotate an object around its center even after setting the origin  (Read 534 times)

0 Members and 1 Guest are viewing this topic.

bobo68

  • Newbie
  • *
  • Posts: 1
    • View Profile
i'm trying to make an object rotate around its center, so i did this:
sprite.setOrigin(object.scale * .5);
sprite.setScale(object.scale);
and in a separate function i rotate the object by some amount every frame

the problem is that it doesn't work, and the object still doesn't rotate around the center, even though all solutions i found online say it should work like this.

but there's another weird thing: from what it looks on the sprite (https://www.sfml-dev.org/tutorials/2.5/graphics-sprite.php) tutorial page, textures should resize to what their host sprite's rectangle size is. the fact is that the texture (that I load from file and set using sprite.setTexture(texture)) seems to not do this because I loaded two different sized textures and, setting scale to (1,1), the resulting images on the window were of different sizes. the rescaling still works, but I thought that setting both sizes to (1,1) would make both sprites appear the same size. I'm saying this because this made me think that it could be a problem with this: the sprite's center point is actually being correctly set but the image is too big, so it goes beyond the size of the sprite.

does anyone know what's happening? is it a setOrigin problem or a texture problem? did i misunderstand how either of these work?
also let me know if something wasn't too clear, i probably didn't explain very well

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: can't rotate an object around its center even after setting the origin
« Reply #1 on: October 29, 2022, 02:20:59 pm »
There are two different concepts here: resolution and scale.
The resolution of a texture is the number of pixels. For example a 64x64 texture. You might only use a small region of a texture, like a 32x32 tile from a large texture, its the resolution of the region (whole texture or smaller part of a texture) that matters.

Scale is a multiplier, it's not a size on its own. The on-screen size of a sprite is texture resolution multiplied by scale.
A 64x64 texture on a sprite with scale of 1,1 will be 64x64 pixels.
A 64x64 texture on a sprite with scale of 2,2 will be 128x128 pixels.
So if you have two sprites with different resolutions but the same scale, they will appear different sizes on screen.

The origin is set using pixels (before scaling). For a sprite with a 64x64 texture, regardless of scale, you'd set the origin to 32,32 (the centre), not 0.5,0.5.

If you want to scale something to a specific size, you need to calculate the scale factor. For example, to make a 1024x768 texture stretch to 1920x1080 (for a background, lets say) you'd have a scale of (1920.0/1024.0), (1080.0/768.0). That would be a scale of 1.875,1.40625.

For example here's some code from an old sample I made a few years ago to scale a background sprite to fill the window:
        sf::Vector2u resolution = m_backgroundSprite->getTexture()->getSize();
        m_backgroundSprite->setScale(float(m_window.getSize().x) / resolution.x, float(m_window.getSize().y) / resolution.y);
 

 

anything