locked
RawGameController Comes up 0,0 RRS feed

  • Question

  • I have a WPF dotnetCore3.1 Desktop application. In part, it uses a joystick to control an RS232 serial device. When a joystick is plugged in, the user can select it from a dropdown, then the software begins polling it, averaging it, and sending controls to the serial device. This use System.Windows.Gaming.Input.RawGameController

    Selecting from the dropdown causes this initializer

    private void UpdateController()
    {
    	if (selectedController != null)
    	{
    		buttonStates = new bool[selectedController.ButtonCount];
    		axisPositions = new double[selectedController.AxisCount];
    		switchpositions = new GameControllerSwitchPosition[selectedController.SwitchCount];
    		selectedController.GetCurrentReading(buttonStates, switchpositions, axisPositions);
    	}
    	else
    	{
    		buttonStates = Array.Empty<bool>();
    		axisPositions = Array.Empty<double>();
    		switchpositions = Array.Empty<GameControllerSwitchPosition>();
    	}
    }


    The polling looks like this

    private async void ControlAsync()
    {
    	// If axiscount < 2 then don't bother
    	if (selectedController != null && selectedController.AxisCount > 1)
    	{
    		selectedController.GetCurrentReading(buttonStates, switchpositions, axisPositions);
    		controlBuffer[0] += axisPositions[0];
    		controlBuffer[1] += axisPositions[1];
    		// Debug.WriteLine($"Controller: {axisPositions[0]}, {axisPositions[1]}");
    	}
    
    	// 0.5 is the neutral position
    	else
    	{
    		controlBuffer[0] += 0.5;
    		controlBuffer[1] += 0.5;
    	}
    
    	if (++controlCounter == controlCount)
    	{
    		controlCounter = 0;
    		PositionerController.PanAxisPositon = controlBuffer[0] / controlCount;
    		PositionerController.TiltAxisPositon = controlBuffer[1] / controlCount;
    
    		controlBuffer[0] = 0;
    		controlBuffer[1] = 0;
    
    		await PositionerController.Control().ConfigureAwait(true);
    	}
    }

    The problem is many joysticks come up reporting (0.0, 0.0) (full down left), regardless of where they are, while the neutral position is (0.5, 0.5). This will not update correctly unless and until the joystick is moved, which means that the serial device, a physical device, will take off on the user.

    I have not been able to come up with a reliable way to fix this. I had some catches that sort of worked, e.g., locking the input until something other than 0,0 is seen, but it seems like there should be a cleaner / more standard way to do this.


    • Edited by Major_Major Tuesday, November 10, 2020 10:49 PM
    Tuesday, November 10, 2020 10:48 PM

All replies

  • Hi Major_Major,

    Thank you for posting here.

    I have no experience in programming joysticks and no equipment for me to test. So I can only look for some possible related links, please see if it will help you.

    Taking input from a joystick with C# .NET

    Obtaining Input Form a Joystick with C# and DirectX Form A to Z

    Or see if these nuget packages are useful for you.

    Top 20 NuGet gamepad Packages

    In addition, questions about WPF should be raised in Microsoft Q&A.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 11, 2020 2:34 AM