2013-08-17 29 views
1

我的应用程序有一个自定义面板,用于在WinForm中显示XNA屏幕。目前我已经展示了一个没有问题的测试模型,现在正在处理相机移动。我的相机是一个免费的相机(不一定要看任何特定的目标),但我一直无法让鼠标更新相机在其轴上的偏航和俯仰。我认为我的更新方法可能有问题,但情况并非如此,因为相机使用KeyboardState更新前后移动。但我不知道为什么MouseState不起作用。鼠标事件不会更新winforms中的相机旋转

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(); 

回答

1

为了使用XNA的鼠标API(而不是WinForm的事件),则必须设置Mouse.WindowHandle适当MSDN)。

如果您使用的是​​samples,然后把这个在您的MainForm的构造将这样的伎俩:

Mouse.WindowHandle = this.Handle; 

(当然using Microsoft.Xna.Framework.Input;