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

Author Topic: How to rotate a texture around the center of a rectangle (solved)  (Read 3374 times)

0 Members and 1 Guest are viewing this topic.

billarhos

  • Newbie
  • *
  • Posts: 17
    • View Profile
Hi

i try to figure out how to rotate a square texture (512x512) around a center of a given rectangle (not square).
here is a my code.

void cSprite::render(sf::FloatRect _dstRect, float _angle, sf::Color color)
{
        if (visible)
        {
                float angle =  _angle / 180.f * (float)M_PI;
                float cos = cosf(angle);
                float sin = sinf(angle);
               
                sf::Sprite              sprite;
                sprite.setTexture(*texture);

                float x = _dstRect.width / size.x;
                float y = _dstRect.height / size.y;
               
                sf::FloatRect rect = sprite.getGlobalBounds();
               
                sprite.setPosition(sf::Vector2f(_dstRect.left + _dstRect.width / 2, _dstRect.top + _dstRect.height / 2));
                sprite.setOrigin(sf::Vector2f(rect.width / 2, rect.height / 2));
                sprite.setRotation(_angle);

                sprite.setScale(sf::Vector2f(x, y));

                GAME_MANAGER->getWindow()->draw(sprite);
        }
}
 

i am trying to make a roulette.
the problem is shown in the attached image
thank you for any suggestions

http://www.mediafire.com/file/bg66i027erqv1na/test%282%29.jpg
« Last Edit: January 03, 2018, 08:13:42 am by billarhos »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: How to rotate a texture around the center of a rectangle
« Reply #1 on: January 02, 2018, 08:27:59 pm »
The problem is that you're trying to manipulate things in 3D while it's actually just a 2D image. I sure it's somehow possible, but it's certainly isn't something SFML provides out of the box. You can still rotate it normally, but then you'll have to figure out a way to deform it the way you want it to look.

I'm not sure, but it seems like Hapax's Sprite 3D could be of use to you: https://github.com/Hapaxia/SelbaWard/wiki/Sprite-3D

Please actually attach the image next time or use a service like imgur to upload it. Having to download and locally open the image just to view is rather annoying.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

billarhos

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: How to rotate a texture around the center of a rectangle
« Reply #2 on: January 03, 2018, 08:12:24 am »
Thanks a lot. I managed to solve it.

//------------------------------------------------------------------------------
void cSprite::render(sf::FloatRect _dstRect, float _angle, sf::Color color)
{
        if (visible)
        {
                float angle = _angle / 180.f * (float)M_PI;
                float cos = cosf(angle);
                float sin = sinf(angle);

                m_renderStates.texture = texture;
                m_renderStates.blendMode = sf::BlendAlpha;

                sf::Vector2f dest[4];

                float x0 = - size.x / 2;
                float y0 = - size.y / 2;
               
                float x1 = size.x / 2;
                float y1 = y0;
               
                float x2 = size.x / 2;
                float y2 = size.y / 2;
               
                float x3 = x0;
                float y3 = y2;

                float fX = _dstRect.width / size.x;
                float fY = _dstRect.height / size.y;

                float tX = _dstRect.left + _dstRect.width / 2;
                float tY = _dstRect.top + _dstRect.height / 2;

                dest[0].x = (x0 * cos - y0 * sin) * fX + tX;
                dest[0].y = (x0 * sin + y0 * cos) * fY + tY;
                                                                                                 
                dest[1].x = (x1 * cos - y1 * sin) * fX + tX;
                dest[1].y = (x1 * sin + y1 * cos) * fY + tY;
                                                                                                 
                dest[2].x = (x2 * cos - y2 * sin) * fX + tX;
                dest[2].y = (x2 * sin + y2 * cos) * fY + tY;
                                                                                                 
                dest[3].x = (x3 * cos - y3 * sin) * fX + tX;
                dest[3].y = (x3 * sin + y3 * cos) * fY + tY;

                m_vertices[0] = sf::Vertex(
                        dest[0], color,
                        sf::Vector2f(0, 0)
                );

                m_vertices[1] = sf::Vertex(
                        dest[1], color,
                        sf::Vector2f(size.x, 0)
                );

                m_vertices[2] = sf::Vertex(
                        dest[2], color,
                        sf::Vector2f(size.x, size.y)
                );

                m_vertices[3] = sf::Vertex(
                        dest[3], color,
                        sf::Vector2f(0, size.y)
                );

                GAME_MANAGER->getWindow()->draw(m_vertices, 4, sf::PrimitiveType::Quads, m_renderStates);
        }
}