为什么当尝试将鼠标移动 360c 度时,鼠标光标在中间卡顿
本文关键字:鼠标 光标 在中间 度时 移动 为什么 360c | 更新日期: 2023-09-27 18:36:42
鼠标不移动鼠标指针/光标卡在中间并结结巴巴/跳舞/摇晃。
这是我用于鼠标输入的代码:
private void ProcessInputCamera(float amount)
{
previousState = currentState;
currentState = Mouse.GetState();
MouseState currentMouseState = Mouse.GetState();
if (currentMouseState != originalMouseState)
{
float xDifference = currentMouseState.X - originalMouseState.X;
float yDifference = currentMouseState.Y - originalMouseState.Y;
leftrightRot -= rotationSpeed * xDifference * amount;
updownRot -= rotationSpeed * yDifference * amount;
Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
}
Vector3 moveVector = new Vector3(0, 0, 0);
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.Up))
moveVector += new Vector3(0, 0, -1);
if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.Down))
moveVector += new Vector3(0, 0, 1);
if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.Right))
moveVector += new Vector3(1, 0, 0);
if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.Left))
moveVector += new Vector3(-1, 0, 0);
if (keyState.IsKeyDown(Keys.Q))
moveVector += new Vector3(0, 100, 0);
if (keyState.IsKeyDown(Keys.Z))
moveVector += new Vector3(0, -100, 0);
AddToCameraPosition(moveVector * amount);
}
private void AddToCameraPosition(Vector3 vectorToAdd)
{
Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
cameraPosition += moveSpeed * rotatedVector;
UpdateViewMatrix(viewMatrix);
}
private void UpdateViewMatrix(Matrix viewMatrix)
{
Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;
Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
}
钥匙正在工作,他们正在旋转地形。但是使用鼠标,我希望能够像第一人称视角一样360c度改变我的视角,而不是旋转地形,也不是旋转物体,而是让我360c环顾四周。
问题是这行:
Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
出于某种原因,使鼠标诅咒卡在中间并跳舞/摇晃/结结巴巴。如果我取消注释此行而不使用它,那么鼠标移动很流畅,但它确实移动了任何东西 360c 度,我只能定期移动鼠标光标。
问题是为什么我不能像第一人称射击游戏中那样移动鼠标 360c?
这是 mt Game1.cs 代码,包括我标记它的相机代码。 http://pastebin.com/SF3iiftq
Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
此行将鼠标光标位置设置为屏幕中心。宽度/2 和高度/2 是屏幕中心的坐标。此行在每个游戏循环中执行一次,因此它会不断将光标位置设置回中心。这就是为什么鼠标卡在中心并在您尝试移动它时摇晃的原因。