【问题标题】:Rotating around 2 points at the same time同时旋转2个点
【发布时间】:2016-10-13 02:28:21
【问题描述】:

基本上,我试图同时围绕 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;
  }    

【问题讨论】:

  • 您希望您的 retangle 连续移动还是仅在鼠标移动时移动?

标签: c++ rotation sfml


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多