这是我现在的解决方案……虽然它特定于 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);
}