2

我正在尝试为 FPS 游戏实现相机移动。我想我快搞定了,但还有一些问题需要解决。我的鼠标移动设置如下:

protected override void OnLoad(EventArgs e)
{
    Mouse.Move += OnMouseMove;
}

void OnMouseMove(object sender, MouseMoveEventArgs e)
{
    _lookDir.X += e.XDelta * _mouseSensitivity;
    _lookDir.Y -= e.YDelta * _mouseSensitivity;
}

当鼠标实际上在窗口内时,这似乎工作得很好,但是一旦我离开窗口,它就不起作用了。我认为我要做的是以某种方式将鼠标限制在窗口内,因为即使它在我的鼠标在窗口外时触发了鼠标移动,并且仍然遇到同样的问题,就在我的桌面上而是坐标。

那么......我该怎么做 - 将鼠标锁定在窗口内?我基本上只是将鼠标位置设置为中心吗?如果是这样...如何设置鼠标位置?我正在使用 Windows,但如果 OpenTK 提供它,我更喜欢非本机解决方案。

4

3 回答 3

2

我做过的一种做法是使用旧的 Quake 游戏(和半衰期 1),每一帧你都将光标位置重置为屏幕中间,然后从屏幕中间计算增量。

编辑:要移动光标,请查看Cursor.Position

于 2012-02-12T00:55:38.023 回答
2

在 OpenTK 3.2 中非常简单

OpenTK.GameWindow window = new OpenTK.GameWindow();
window.CursorGrabbed = true;

您还可以使用此代码段来隐藏光标。只需根据需要实施即可。

window.Cursor = MouseCursor.Empty;
于 2020-07-30T14:40:21.880 回答
1

这是我现在的解决方案……虽然它特定于 Windows。

protected void ResetCursorPosition()
{
    Cursor.Position = WindowCenter;
    _lastMousePos = Cursor.Position;
}

protected void LockMouse()
{
    _lockMouse = true;
    _origCursorPosition = Cursor.Position;
    CursorVisible = false;
    ResetCursorPosition();
}

protected void UnlockMouse()
{
    _lockMouse = false;
    CursorVisible = true;
    Cursor.Position = _origCursorPosition;
}

void OnMouseDown(object sender, MouseButtonEventArgs e)
{
    if (!_lockMouse) LockMouse();
}

void OnKeyDown(object sender, KeyboardKeyEventArgs e)
{
    if(e.Key == Key.Escape)
    {
        if (_lockMouse) UnlockMouse();
        else Exit();
    }
}

protected override void OnUpdateFrame(FrameEventArgs e)
{
    Title = string.Format("{0} - {1:0.0} FPS", _windowTitle, RenderFrequency);

    float moveSpeed = Keyboard[RunKey] ? 0.9f : 0.4f;

    if (Keyboard[MoveForwardKey])
    {
        _pos.X += (float)Math.Cos(_lookDir.X) * moveSpeed;
        _pos.Z += (float)Math.Sin(_lookDir.X) * moveSpeed;
    }

    if (Keyboard[StrafeLeftKey]) // FIXME: holding W + A gives extra speed (also, perhaps strafing should be slower?)
    {
        _pos.X -= (float) Math.Cos(_lookDir.X + Math.PI / 2) * moveSpeed;
        _pos.Z -= (float) Math.Sin(_lookDir.X + Math.PI / 2) * moveSpeed;
    }

    if (Keyboard[StrafeRightKey])
    {
        _pos.X += (float)Math.Cos(_lookDir.X + Math.PI / 2) * moveSpeed;
        _pos.Z += (float)Math.Sin(_lookDir.X + Math.PI / 2) * moveSpeed;
    }

    if (Keyboard[MoveBackKey])
    {
        _pos.X -= (float)Math.Cos(_lookDir.X) * moveSpeed;
        _pos.Z -= (float)Math.Sin(_lookDir.X) * moveSpeed;
    }

    if(Keyboard[JumpKey])
    {
        _pos.Y += moveSpeed;
    }

    if (Keyboard[CroucKey])
    {
        _pos.Y -= moveSpeed;
    }

    if (_lockMouse)
    {
        var mouseDelta = Cursor.Position - new Size(_lastMousePos);
        if (mouseDelta != Point.Empty)
        {
            _lookDir.X += mouseDelta.X * _mouseSensitivity;
            _lookDir.Y -= mouseDelta.Y * _mouseSensitivity;
            ResetCursorPosition();
        }
    }

    var target = _pos + new Vector3((float)Math.Cos(_lookDir.X), (float)Math.Sin(_lookDir.Y / 2), (float)Math.Sin(_lookDir.X));
    _viewMat = Matrix4.LookAt(_pos, target, _up);
    _projUniform.Mat4(_viewMat * _projMat);
}
于 2012-02-12T19:50:15.830 回答