chhenning
chhenning

Reputation: 2077

How to rotate a shape around a point

In SFML how would I rotate a shape around some point using transformations? I know how to use cos and sin but like to understand how to use transformation.

I think my problem is that I don't get the sfml interface. Here is one example:

// Draw circle at center
sf::CircleShape c;
c.setFillColor(sf::Color::Red);
c.setPosition(width / 2, height / 2);
c.setRadius(50);
c.setOrigin({50, 50});

Now, how would I move and rotate the circle around the center of the screen inside the frame refresh loop?

Upvotes: 0

Views: 2803

Answers (1)

chhenning
chhenning

Reputation: 2077

Turns out there is rotate function which takes a center point.

sf::CircleShape c;
c.setFillColor(sf::Color::Red);
c.setPosition(width / 2.f, height / 2.f - 100);
c.setRadius(50);
c.setOrigin({50, 50});

float angle = 1.0;

sf::Transform t;

while(true)
{
    window.clear(sf::Color::Black);

    t.rotate(angle, { width/2.f, height / 2.f });

    window.draw(c, t);

    window.display();
}

Upvotes: 1

Related Questions