鼠标事件不会更新的WinForms旋转摄像头 [英] Mouse event wont update camera rotation in winforms
问题描述
我的应用程序有用于一个WinForm中显示的XNA屏幕自定义面板。目前我已经显示没有问题的测试模型,现在正在摄影机运动。我的相机是一个免费的摄像头(不绑定到看任何特定的目标),但我已经遇到了麻烦鼠标更新相机的偏航和变桨它自己的轴。我想也许是出事了与我的更新方法,但事实并非如此,因为相机的更新向前和向后使用 KeyboardState
。但我不知道,为什么 MouseState
是行不通的。
FreeCamera.cs
使用XNAButtonState = Microsoft.Xna.Framework.Input.ButtonState;
....
MouseState pastMouseState;
私人浮动rotationSpeed_ = 1F / 60F;
私人浮动yaw_,pitch_;
:
私人无效updateMatrix()
{
矩阵rotationMatrix = Matrix.CreateRotationX(pitch_)*
Matrix.CreateRotationY (偏航_);
的Vector3向前=新的Vector3(0,0,1);
向前= Vector3.Transform(前进,rotationMatrix);
viewMatrix_ = Matrix.CreateLookAt(位置,位置向前+,向上);
projectionMatrix_ = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4,16.0f / 9.0f,0.1F,100000.0f);
}
私人无效cameraInput()
{
KeyboardState keyboardState = Keyboard.GetState(); < - 更新
currentMouseState = Mouse.GetState(); < - 不更新
如果(currentMouse.LeftButton == XNAButtonState.Pressed)
pitch_ - = rotationSpeed_;
如果(keyboardState.IsKeyDown(Keys.W))
移动(1);
如果(keyboardState.IsKeyDown(Keys.S))
移动(-1);
pastMouseState = currentMouseState;
}
公共无效更新()
{
cameraInput();
updateMatrix();
为了使用XNA的鼠标API(而不是WinForm的事件),则必须设置 Mouse.WindowHandle
适当(适用的 MSDN )。
如果您使用的是<一个HREF =http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1相对=nofollow>官方的 的样品,然后把这个在你的的MainForm
S'构造函数会做的伎俩:
Mouse.WindowHandle = this.Handle;
(当然使用Microsoft.Xna.Framework.Input;
)
My app has a custom panel used to display the XNA screen within a WinForm. I've currently displayed a test model with no problem and now working on camera movement. My camera is a Free Camera (not bound to look at any specific target), but I've been having trouble getting the mouse to update the yaw and pitch of the camera on its own axis. I thought maybe something was wrong with my update method, but that wasn't the case, because the camera updates moving forward and backwards using KeyboardState
. But I have no idea as to why the MouseState
isn't working.
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();
In order to use XNA's mouse API (rather than WinForm events) you must set Mouse.WindowHandle
appropriately (MSDN).
If you are using the official samples, then putting this in your MainForm
's constructor will do the trick:
Mouse.WindowHandle = this.Handle;
(Of course using Microsoft.Xna.Framework.Input;
)
这篇关于鼠标事件不会更新的WinForms旋转摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!