我在“游戏引擎”中工作得很好(这可能很重要)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不支持。
答案 0 :(得分:1)
答案 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;
}
}