Set a dead zone on gamepad

Greetings,

I am currently experimenting with GamepadManager, which works great but of course the values of the left and right sticks are going crazy when I implement their event listeners like such

gamepad.onleftstickchanged((values) => {
console.log(values.x + " " + values.y);
});

I found somebody using BABYLON.UserInputOptions.GamepadDeadStickValue on this old thread Physics Movement - Page 2 - Questions & Answers - HTML5 Game Devs Forum

Does such a function exists? I cannot find it in the docs.

Iwould like to use the built-in functions from babylon if possible. I am using the ES6 version of the library and perhaps I am just too blind to find the UserInput.js file in the node_modules/@babylonjs/core/ folder…

Does anyone knows how to set a dead zone on a gamepad and the path I need to use to import it in my script?

Thanks!

I don’t believe this function exists as it a bit specific. However, it’s a simple solution to use the existing functions to run a delta on your X and Y values. Simply set a value for any X and Y and subtract this from the overall value - which is usually ±50. Then scale your joystick value to be 100 at any position. I hope this makes sense. If not, I can write a simple script for you if I know why you need to achieve this. And always remember to average/blend your values.

Galen

1 Like

Ok I got it working by doing pretty much what you explained and I found this awesome blog post that is a definitive reference for using the Gamepad API. I’ll post the solution here, in case someone from the future generations needs it:

export default function setDeadZone(value) {
    const DEADZONE = 0.2;

    // If stick value is smaller than dead zone, give it a value of 0
    // Math.abs() makes it work regardless of positive or negative value
    if (Math.abs(value) < DEADZONE) {

        return value = 0;

    } else {
        // We are outside the dead zone
        value = value - Math.sign(value) * DEADZONE;

        // Normalize the values between 0 and 1
        value /= (1.0 - DEADZONE);

        return value;

    }
}

And then in the gamepad updating function:

import setDeadZone from "./GamepadDeadZone";

// ...
gamepad.onleftstickchanged((values) => {

let x = values.x;
let y = values.y;
x = setDeadZone(x);
y = setDeadZone(y);
});

2 Likes