SFML community forums

Help => Graphics => Topic started by: ShadoWhy on May 25, 2017, 01:35:34 pm

Title: Rotate a Sprite centered
Post by: ShadoWhy on May 25, 2017, 01:35:34 pm
I using SFML 2.4.2
My english is not the best sorry for mistakes.
I try to make a Pseudo3D Object(an Emerald or something like this) and have tried to Rotate the Picture.

The Variables of the Code are:
Defines:
#define DEFAULT_BIT_SCALE 4
#define DEFAULT_BIT_SIZE    8
 
Objects:
sf::Texture texture;
sf::Sprite sprite[DEFAULT_BIT_SIZE];
 

The Code in the Init look like this:
void bit8::Cube::init()
{      
        //Its an test Texture
        this->texture.loadFromFile("gfx/8x8x8.png");
        this->texture.setRepeated(false);
        this->texture.setSmooth(false);
       
        //This is the code for creation for test a cube
        for (int i = 0; i < DEFAULT_BIT_SIZE; i++) {
                this->sprite[i].setTexture(texture);
                this->sprite[i].setTextureRect(sf::IntRect(i * DEFAULT_BIT_SIZE, 0, DEFAULT_BIT_SIZE, DEFAULT_BIT_SIZE));
                this->sprite[i].setScale(DEFAULT_BIT_SCALE, DEFAULT_BIT_SCALE);
                this->sprite[i].setPosition(200, 300 + i*DEFAULT_BIT_SCALE);
               
        }
}
 

The Code for the Rotate look like this:
float degrees = 0
for (int i = 0; i < DEFAULT_BIT_SIZE; i++) {
                if (degrees > 359.90) {
                        degrees = 0;
                }
                degrees += 0.1f;
                this->sprite[i].setRotation(degrees);
        }
 
I don't know if the Question is in the right place.
And I have the problem that I have to change the Rotation axis to the Center and dont know a way to do that.
So is the question how to do the axis in the center.
Thanks for any answers.
Title: Re: Rotate a Sprite centered
Post by: GameBear on May 25, 2017, 03:30:47 pm
You will have to use the setOrigen function for that :)

https://www.sfml-dev.org/documentation/2.0/classsf_1_1Transformable.php#a56c67bd80aae8418d13fb96c034d25ec
Title: Re: Rotate a Sprite centered
Post by: Hapax on May 25, 2017, 05:04:38 pm
Note that the position is also based around the origin so you may need to adjust your positions after changing the origin.
Title: Re: Rotate a Sprite centered
Post by: ShadoWhy on May 30, 2017, 05:53:36 pm
Thanks.
It works.
Title: Re: Rotate a Sprite centered
Post by: Carlos Augusto Br Cpp on June 01, 2017, 05:30:54 pm
good... please put [SOLVED] on the name of this post, to help people know that you have get it... I was writing an answer when I see that you got it to works...  ;D