Mouse event wont update camera rotation in winforms

不打扰是莪最后的温柔 提交于 2019-12-01 11:27:35

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/18290289/mouse-event-wont-update-camera-rotation-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!