【问题标题】:Joystick Coordinates操纵杆坐标
【发布时间】:2021-01-18 00:54:39
【问题描述】:

大家好,我需要您的帮助,使用开放式 TK 使用 Xaml 在 dot net framework 4.5 上工作,我想通了。使用以下代码。我想在 Windows 窗体上复制它,但打开的 TK 不喜欢窗体。我做了很多谷歌搜索,发现了 2 个选项 SlimDX 和 SharpDX 直接输入。我的目标是使用操纵杆通过操纵杆输入沿表单移动控件。我似乎无法以开放 TK 的方式复制轴 1 和轴 2。我正在寻找例如 XAML 的 c3 清晰代码中所示的 0 或 1 和 -1。

using OpenTK.Input;

private DispatcherTimer clock;
private GamePadState oldgstate;
private JoystickState oldjstate;
private int activeGamepad = 0;
private int activeJoystick = 0;


 public MainWindow()
    {
        InitializeComponent();
        createNewTimer();
        oldgstate = GamePad.GetState(activeGamepad);
        oldjstate = Joystick.GetState(activeJoystick);
    }

    //public Direction TestOpenTK.MainWindow.MyProperty { get; set; }

    private void createNewTimer() {
        clock = new DispatcherTimer();
        clock.Tick += new EventHandler(checkGamePads);
        clock.Interval = new TimeSpan(0,0,0,0,25);
        clock.Start();
    }

    private void checkGamePads(Object sender, EventArgs e)
    {
        var gState = GamePad.GetState(activeGamepad);
        var jState = Joystick.GetState(activeJoystick);


        if (!gState.Equals(oldgstate))
        {
            txtGamePadDebug.Text = gState.ToString();
        }

        if (!gState.Equals(oldjstate))
        {
            //txtJoystickDebug.Text = jState.ToString() + Environment.NewLine + " Axis 1 = " + jState.GetAxis(JoystickAxis.Axis1).ToString()
            //     + Environment.NewLine + " Throttle Axis 2 = " + jState.GetAxis(JoystickAxis.Axis2).ToString() + Environment.NewLine + " Axis 3 Rotation = " +
            //     jState.GetAxis(JoystickAxis.Axis3).ToString() + Environment.NewLine + " Axis 4 = " + jState.GetAxis(JoystickAxis.Axis4).ToString();
            for (int i = 0; i < 4; i++)
            {
                var state = Joystick.GetState(i);

                if (state.IsConnected)
                {

需要使用 SharpDX 或 SlimDX 获得类似的 Axis 或弄清楚如何在 windows 窗体上使用打开的 TK

float x = state.GetAxis(JoystickAxis.Axis0); float y = state.GetAxis(JoystickAxis.Axis1);

                    //Convert and round x and y
                    int X = (int)Math.Round(x);
                    int Y = (int)Math.Round(y);

                    //Set axis direction using integers X and Y
                    if (X == 0 && Y==1)
                    {
                        // N
                        txtJoystickDebug.Text = "North";
                    }

                    if (X == 1 && Y == 1)
                    {
                        // NE Up and right
                        txtJoystickDebug.Text = "North East";
                    }

                    if (X == 1 && Y == 0)
                    {
                        //E
                        txtJoystickDebug.Text = "East";
                    }

                    if (X == 1 && Y == -1)
                    {
                        //SE Down Right
                        txtJoystickDebug.Text = "South East";
                    }

                    if (X == 0 && Y == -1)
                    {
                        //S
                        txtJoystickDebug.Text = "South";
                    }

                    if (X == -1 && Y == -1)
                    {
                        // SW Down Left
                        txtJoystickDebug.Text = "South West";
                    }

                    if (X == -1 && Y == 0)
                    {
                        //W 
                        txtJoystickDebug.Text = "West";
                    }

                    if (X == -1 && Y == 1)
                    {
                        //NW Up or West
                        txtJoystickDebug.Text = "North West";

                    }

                    //Slider aka throttle axis
                    //float T = state.GetAxis(JoystickAxis.Axis2);

                    // Print the current state of the joystick
                    //Console.WriteLine(T);
                    //txtJoystickDebug.Text = "X = " + Math.Round(x).ToString() + Environment.NewLine + "Y = "  + Math.Round(y).ToString()
                    //    + Environment.NewLine + " Throttle = " + T.ToString();
                }
            }
        }

【问题讨论】:

    标签: c# opentk sharpdx joystick slimdx


    【解决方案1】:

    如果你使用 SharpDX,要访问游戏手柄,你需要使用 SharpDX.XInput 包。

    然后初始化一个控制器:

    var controller = new SharpDX.XInput.Controller(SharpDX.XInput.UserIndex.One);
    

    轮询控制器:

    if (controller.IsConnected)
    {
        SharpDX.XInput.Gamepad gamepad = controller.GetState().Gamepad;
    
        //Axis 
        //LeftThumbX, LeftThumbY, RightThumbX, RightThumbY
    
        //these are short so to get a float from 0 to 1 instead :
        float tx = gamepad.LeftThumbX / (float)short.MaxValue;
        
        //Buttons
        //GamepadButtonFlags Buttons
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      相关资源
      最近更新 更多