简体   繁体   English

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

[英]How to make OpenGL camera moving in SFML by mouse move

I've good working (this may be important) FPS camera module in my "Game Engine". 我在“游戏引擎”中的FPS摄像头模块工作正常(这可能很重要)。 Now I'm using it with WASD and Up/Down/Left/Right. 现在,我将它与WASD和上/下/左/右一起使用。 I want to add possibility to move camera by mouse. 我想增加通过鼠标移动相机的可能性。 It's my code I've written: 这是我写的代码:

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());
}

How can I get moving my camera without cursor moving on the screen? 如何在不移动光标的情况下移动相机? It's working few seconds and camera locks (so I can't mouse move then I must kill process). 它正在工作几秒钟,并且相机已锁定(因此,我无法移动鼠标,那么我必须终止进程)。 I'd rather get event of mouse move not mouse position but SFML doesn't support that. 我宁愿发生鼠标移动而不是鼠标位置的事件,但SFML不支持该操作。

Have you tried simply disabling the cursor while you're allowing this input? 您是否尝试过在允许此输入时仅禁用光标?

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

Some documentation 一些文件

I've solved this problem myself. 我已经解决了这个问题。 Here's code: 这是代码:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM