SFML community forums

Help => Graphics => Topic started by: HemoGoblin on September 09, 2012, 03:44:32 pm

Title: xy rotation using transform?
Post by: HemoGoblin on September 09, 2012, 03:44:32 pm
Hi there!
I was trying to implement xy rotation for my sprite. Only way i could think of was to use transforms with 3x3 rotation matrix. However the image is no longer centered (its position was set to the center of view and the origin was set to half the texture size). The question is, how to rotate it using transforms without repositioning? Thanks
Title: Re: xy rotation using transform?
Post by: eXpl0it3r on September 09, 2012, 04:12:09 pm
What do you exactly mean with 'my sprite'? Have you written your own implementation of a sprite?
If so, have you thought about deriving from sf::Transformable (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Transformable.php)?

If you're using the sf::Sprite, you can simply change the origin and make the transformation to that point.

If you want to work with a rotation matrix, you can use sf::Transform (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Transform.php) and instead of inserting the position, you use the transformation origin.
Title: Re: xy rotation using transform?
Post by: HemoGoblin on September 09, 2012, 04:18:48 pm
I have this code:


#include <SFML/Graphics.hpp>


int main() {

        sf::RenderWindow Window(sf::VideoMode(1024, 600), "Test");

        sf::Texture Texture;
        Texture.loadFromFile("A.png");

        sf::Sprite Sprite(Texture);
        Sprite.setOrigin(36.0f, 36.0f);
        Sprite.setPosition(512.0f, 300.0f);

        while (Window.isOpen()) {

                sf::Event Event;
                while (Window.pollEvent(Event)) {

                        switch (Event.type) {

                                case sf::Event::Closed:

                                        Window.close();
                                        break;

                        }

                }

                Window.clear(sf::Color::White);

                sf::Transform B(std::cos(30.0f), 0.0f, std::sin(30.0f), 0.0f, 1.0f, 0.0f, -std::sin(30.0f), 0.0f, std::cos(30.0f));
                Window.draw(Sprite, B);

                Window.display();

        }

}

What do you actually mean by "transformation origin"?
Title: Re: xy rotation using transform?
Post by: eXpl0it3r on September 09, 2012, 04:29:15 pm
Btw there's a code=cpp tag for highlighting sourcecode instead of using the quote tag (dropdown box right)... ;)

So you're infact using sf::Sprite, but why can't you just call sprite.rotate()?
What do you imagine under the term xy rotation?

Do you want to rotate the sprite around one edge?

Also take a look at the example in the documentation for sf::Transform, you don't need to specify all the parameters. ;)
Title: Re: xy rotation using transform?
Post by: HemoGoblin on September 09, 2012, 04:37:10 pm
Sprite.rotate uses z-rotation only, i need to rotate it also around the other axes (to achieve simple "3d" effect).
Title: Re: xy rotation using transform?
Post by: eXpl0it3r on September 09, 2012, 05:03:07 pm
Sprite.rotate uses z-rotation only, i need to rotate it also around the other axes (to achieve simple "3d" effect).
Ah now I understand! :)

Will that even work properly? I mean the resulting geometry won't be a rectangle anymore, thus you'll either have to directly change the texture/image to get that effect, or use something else instead of sf::Sprite (e.g. sf::VertexArray or sf::ConvexShape)...
Title: Re: xy rotation using transform?
Post by: Laurent on September 09, 2012, 05:07:19 pm
You can't work in other planes with sfml-graphics, it's a 2D module. The 3x3 matrix encapsulated by sf::Transform will only let you rotate around the Z axis. The last column of the matrix is not a Z column, it's the "W" column, which allows to handle translations.

If you want to do 3D stuff you're on your own.
Title: Re: xy rotation using transform?
Post by: HemoGoblin on September 09, 2012, 05:09:09 pm
Okay, thanks.