1

I am working on a mobile game in c++ in Replit, and have run into an issue.
I have a rectangle, defined with a color, position, and specific origin. I rotate it every frame, but it rotates around 0, 0 instead of it's origin. I have looked through similar problems but none solve my problem.

Here is my code

int main()
{
    RenderWindow window(VideoMode(375, 475), "");

    RectangleShape sprite(Vector2f(275, 25));
    Vector2f Position(-187.5f, -375);
    sprite.setOrigin(Position);
    sprite.setFillColor(Color::White);
    sprite.setPosition(-137.5, 0);

    
    while (window.isOpen()) {
        Event event;
        while (window.pollEvent(event)) {
            if (event.type == Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(sprite);
    sprite.rotate(0.01);
        // Copy the buffer to the window.
        window.display();
    }

    return 0;
}

Here is a screenshot of the code running:
enter image description here

enter image description here

enter image description here

Can anyone help me?

  • 2
    Move it to the origin. Rotate it. Move it back. Re-display the sprite. – lastchance Mar 20 '23 at 16:04
  • Would this work if it is being updated based on other objects/forces acting upon it? – Schrödingers Capybara Mar 20 '23 at 16:14
  • You asked how to rotate about a specified point, say (a,b). To do that when your library only allows you to rotate about the origin then: translate by (-a,-b), rotate by the angle required; translate back by (+a,+b), then re-display. This is completely independent of any actions (forces or moments) acting on it (and that wasn't in your original question). – lastchance Mar 20 '23 at 16:20
  • It's kinda working, but It is rotating around it's top left instead of its center. Here is the code.: ``` window.clear(); float newx = left - x; float newy = top - y; Vector2f newp(newx, newy); Vector2f backp(newx + x, newy + y); scale.setPosition(newp); scale.rotate(0.01); scale.setPosition(backp); // Copy the buffer to the window. window.draw(scale); window.display(); ``` – Schrödingers Capybara Mar 20 '23 at 16:58
  • @SchrödingersCapybara Because you're positioning its top left (the default origin of the object) at the origin. Use `setOrigin` to move the rectangle's origin. – molbdnilo Mar 20 '23 at 17:04
  • @molbdnilo can you give me an example in an answer building off of lastchance's comment? – Schrödingers Capybara Mar 20 '23 at 17:07

1 Answers1

0

So if i understand you want to rotate your rectangle around it's center ? if yes here is main.cpp:

#include <SFML/Graphics.hpp>

using namespace sf;
int main()
{
    RenderWindow window(VideoMode(375, 475), "");

    RectangleShape sprite(Vector2f(275, 25));
    sprite.setFillColor(Color::White);
    Vector2f Position(sprite.getPosition().x + 275 / 2, sprite.getPosition().y + 25 / 2);
    sprite.setOrigin(Position);
    Vector2f pos(137.5f, 300);
    sprite.setPosition(pos);


    while (window.isOpen()) {
        Event event;
        while (window.pollEvent(event)) {
            if (event.type == Event::Closed)
                window.close();
        }

        

        window.clear();
        window.draw(sprite);
        sprite.rotate(0.01);
        // Copy the buffer to the window.
        window.display();
    }

    return 0;
}
goAT2160
  • 68
  • 12