同时绕两个点旋转

Rotating around 2 points at the same time

本文关键字:两个 旋转      更新时间:2023-10-16

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

这里,当鼠标向左或向右移动时,我试图围绕它的中心旋转正方形,当鼠标向上或向下移动时,我围绕任何其他点旋转(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;
  }    

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

你只需要将鼠标每次移动的位置存储在sf::Vector2f