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

Author Topic: Rotate around 2 points at the same time  (Read 1894 times)

0 Members and 1 Guest are viewing this topic.

BlackCrock

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Rotate around 2 points at the same time
« on: October 13, 2016, 06:35:53 am »
Hello,

I'm trying to rotate a square around it's center when I'm moving mouse left/right, and to rotate it around point 50:100, when mouse is moving up/down. My implementation (which is wrong, obviously) makes my square to jump around 2 points on the screen; when it's supposed to rotate only around the center, it rotates around 50:100. Any suggestions/directions how to overcome this?
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;

Event evt;

int main(int argc, char* argv)
{
        RenderWindow window(VideoMode(600, 600), "test");

        RectangleShape test(Vector2<float>(20.0f, 20.0f));
        test.setFillColor(Color::Red);
        test.setPosition(300, 300);
        test.setOrigin(10, 10);

        Clock deltaTime;
        Clock timer;
        timer.restart();

        int mouseX = 0, mouseY = 0;
        int curMouseX = 0, curMouseY = 0;
        float offset = 100;
        bool moving = false;

        while (window.isOpen())
        {
                while (window.pollEvent(evt)) {
                        switch (evt.type)
                        {
                        case Event::MouseMoved:
                                curMouseX = mouseX;
                                curMouseY = mouseY;
                                mouseX = evt.mouseMove.x;
                                mouseY = evt.mouseMove.y;
                                moving = true;
               
                                break;
                        }                      
                }

                float elaspedTime = deltaTime.restart().asSeconds();

                if (curMouseX != mouseX && moving) {

                        test.setOrigin(10, 10);
                        test.rotate(360 * elaspedTime);
                        //  test.setOrigin(50, 100); Tried this to avoid jumping. When uncommented, doesn't rotate around the center.
                }

                if (curMouseY != mouseY && moving) {
                        test.setOrigin(50, 100);       
                        test.rotate(60 * elaspedTime);                 
                }
                window.clear();
                window.draw(test);
                window.display();
                moving = false;
        }
        return 0;
}
 

Jonny

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • Email
Re: Rotate around 2 points at the same time
« Reply #1 on: October 13, 2016, 08:08:01 am »
I don't know about you but I'd find it extremely hard to only move my mouse along one axis! are you sure you're not making tiny movements along both?

To test you could either make it output your mousex and mousey each time the mouse moves and check what the values are, or alternatively just test one axis at a time to make sure they behave as expected

BlackCrock

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Rotate around 2 points at the same time
« Reply #2 on: October 13, 2016, 08:17:57 am »
This is what I actually did, and yeah, indeed, it's hard to move mouse along one axis. What I noticed, square moves around the 50:100 in any case.

The greatest problem anyway, is to rotate around 2 points at the same time (or maybe to make an illusion of it? If I move mouse diagonally square has to move around 50:100 and it's center). I was thinking about using the Transform directly, but I don't know how to apply it here.
« Last Edit: October 13, 2016, 08:24:30 am by BlackCrock »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rotate around 2 points at the same time
« Reply #3 on: October 13, 2016, 08:57:41 am »
Laurent Gomila - SFML developer