如何通过鼠标移动使OpenGL相机在SFML中移动

时间:2016-05-16 16:00:54

标签: c++ linux opengl

我在“游戏引擎”中工作得很好(这可能很重要)FPS相机模块。现在我正在使用它与WASD和向上/向下/向左/向右。我想增加用鼠标移动相机的可能性。这是我写的代码:

if (event.type == sf::Event::MouseMoved)
{
    static glm::vec2 lastPos;
    glm::vec2 mousePos(event.mouseMove.x, event.mouseMove.y);
    glm::vec2 delta(lastPos - mousePos);
    delta *= -0.01;
    cam->addRotation(delta);

    sf::Vector2i center(parentWnd->getSFMLWindow()->getSize().x/2, parentWnd->getSFMLWindow()->getSize().y/2);
    lastPos.x = center.x;
    lastPos.y = center.y;

    sf::Mouse::setPosition(center, *parentWnd->getSFMLWindow());
}

如果没有光标在屏幕上移动,如何移动相机?它工作几秒钟和相机锁(所以我不能移动鼠标然后我必须杀死进程)。我宁愿得到鼠标移动事件而不是鼠标位置,但SFML不支持。

2 个答案:

答案 0 :(得分:1)

您是否尝试过在允许此输入时禁用光标?

void sf :: Window :: setMouseCursorVisible(bool visible)

Some documentation

答案 1 :(得分:1)

我自己解决了这个问题。这是代码:

if (event.type == sf::Event::MouseMoved) {
    static glm::vec2 lastMousePos;
    glm::vec2 mousePos(event.mouseMove.x, event.mouseMove.y);

    glm::vec2 deltaPos(mousePos - lastMousePos);
    const float mouseSensitivity = 0.1f;
    deltaPos *= mouseSensitivity;
    deltaPos.y *= -1;
    cam->addRotation(deltaPos);

    sf::Window const& sfWindow = *parentWnd->getSFMLWindow();
    auto windowSize = sfWindow.getSize();

    uint32_t maxMousePosRadius = glm::min(windowSize.x, windowSize.y) / 3;
    glm::vec2 windowCenter(windowSize.x / 2, windowSize.y / 2);

    if (glm::length(mousePos - windowCenter) > maxMousePosRadius) {
        sf::Mouse::setPosition(sf::Vector2i((int)windowCenter.x, (int)windowCenter.y), sfWindow);
        lastMousePos = windowCenter;
    }
    else {
        lastMousePos = mousePos;
    }
}