C# 鼠标事件不会更新winforms中的相机旋转
我的应用程序有一个自定义面板,用于在WinForm中显示XNA屏幕。我目前展示了一个没有问题的测试模型,现在正在研究相机的运动。我的相机是一个免费的相机(不一定要看任何特定的目标),但我一直很难让鼠标在自己的轴上更新相机的偏航和俯仰。我认为我的更新方法可能有问题,但事实并非如此,因为相机使用C# 鼠标事件不会更新winforms中的相机旋转,c#,winforms,mouseevent,xna-4.0,C#,Winforms,Mouseevent,Xna 4.0,我的应用程序有一个自定义面板,用于在WinForm中显示XNA屏幕。我目前展示了一个没有问题的测试模型,现在正在研究相机的运动。我的相机是一个免费的相机(不一定要看任何特定的目标),但我一直很难让鼠标在自己的轴上更新相机的偏航和俯仰。我认为我的更新方法可能有问题,但事实并非如此,因为相机使用KeyboardState前后更新。但是我不知道为什么MouseState不起作用 FreeCamera.cs using XNAButtonState = Microsoft.Xna.Framework.I
KeyboardState
前后更新。但是我不知道为什么MouseState
不起作用
FreeCamera.cs
using XNAButtonState = Microsoft.Xna.Framework.Input.ButtonState;
....
MouseState pastMouseState;
private float rotationSpeed_ = 1f / 60f;
private float yaw_, pitch_;
...
private void updateMatrix()
{
Matrix rotationMatrix = Matrix.CreateRotationX(pitch_) *
Matrix.CreateRotationY(yaw_);
Vector3 forward = new Vector3(0, 0, 1);
forward = Vector3.Transform(forward, rotationMatrix);
viewMatrix_ = Matrix.CreateLookAt(Position, Position + forward, Up);
projectionMatrix_ = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4, 16.0f / 9.0f, 0.1f, 100000.0f);
}
private void cameraInput()
{
KeyboardState keyboardState = Keyboard.GetState(); <-- updates
currentMouseState = Mouse.GetState(); <-- not updating
if (currentMouse.LeftButton == XNAButtonState.Pressed)
pitch_ -= rotationSpeed_;
if (keyboardState.IsKeyDown(Keys.W))
move(1);
if (keyboardState.IsKeyDown(Keys.S))
move(-1);
pastMouseState = currentMouseState;
}
public void update()
{
cameraInput();
updateMatrix();
使用XNAButtonState=Microsoft.Xna.Framework.Input.ButtonState;
....
穆斯庄园帕斯穆斯庄园;
专用浮子旋转速度=1f/60f;
专用浮子偏航、俯仰;
...
私有void updateMatrix()
{
矩阵旋转矩阵=矩阵.CreateRotationX(节距)*
矩阵。创建旋转y(偏航);
向量3向前=新向量3(0,0,1);
前进=矢量3.变换(前进,旋转矩阵);
viewMatrix=矩阵。CreateLookAt(位置,位置+向前,向上);
projectionMatrix=矩阵。CreatePerspectiveFieldOfView(
MathHelper.p版本4、16.0楼/9.0楼、0.1楼、100000.0楼);
}
私人空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置空置
{
KeyboardState KeyboardState=Keyboard.GetState();为了使用XNA的鼠标API(而不是WinForm事件),您必须适当地设置鼠标.WindowHandle
如果您使用的是,则将其放入main表单的构造函数中即可:
Mouse.WindowHandle = this.Handle;
(当然使用Microsoft.Xna.Framework.Input;
)为了使用Xna的鼠标API(而不是WinForm事件),您必须适当地设置鼠标.WindowHandle
)
如果您使用的是,则将其放入main表单的构造函数中即可:
Mouse.WindowHandle = this.Handle;
(当然使用Microsoft.Xna.Framework.Input;
)