同时旋转2点左右

时间:2016-10-13 02:28:22

标签: c++ rotation sfml

基本上,我试图同时围绕2个点旋转一个正方形。

这里,当鼠标向左或向右移动时,我试图围绕它的中心旋转方块,当鼠标向上或向下移动时,我在任何其他点(这里50:100)旋转。然而,我的方块在屏幕上跳跃,当我试图围绕中心旋转时,它继续以50:100左右旋转。 有任何建议如何解决?

#include <SFML/Graphics.hpp>
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;
  }    

1 个答案:

答案 0 :(得分:0)

不是使用从您的形状继承的sf::Transfomable类,而是为其创建sf::Transform并使用rotate (float angle, const Vector2f &center)

每次在sf::Vector2f

中移动时,您只需存储鼠标位置