Skip to content

Instantly share code, notes, and snippets.

@videlais
Created Feb 28, 2014
Embed
What would you like to do?
Gamepad axis 'moved' function for CocoonJS on the Ouya using a high-precision radial deadzone algorithm
Phaser.Plugin.CocoonJSOuyaGamepad.prototype.moved = function(pad, axisId) {
var movedAmount = 0;
var magitudeLeft = 0;
var magitudeRight = 0;
if (_gamepads[pad]) {
magitudeLeft = Math.sqrt((_gamepads[pad].axes[0] * _gamepads[pad].axes[0]) +
(_gamepads[pad].axes[1] * _gamepads[pad].axes[1]));
magitudeRight = Math.sqrt((_gamepads[pad].axes[2] * _gamepads[pad].axes[2]) +
(_gamepads[pad].axes[3] * _gamepads[pad].axes[3]));
if (axisId === "AXIS_0" && _gamepads[pad].axes[0] <= 1) {
if (magitudeLeft < settings.LEFTAXISTHRESHOLD) {
movedAmount = 0;
}
else {
movedAmount = (_gamepads[pad].axes[0] / magitudeLeft) *
((magitudeLeft - settings.LEFTAXISTHRESHOLD) / (1 - settings.LEFTAXISTHRESHOLD));
}
} else if (axisId === "AXIS_1" && _gamepads[pad].axes[1] <= 1) {
if (magitudeLeft < settings.LEFTAXISTHRESHOLD) {
movedAmount = 0;
}
else {
movedAmount = (_gamepads[pad].axes[1] / magitudeLeft) *
((magitudeLeft - settings.LEFTAXISTHRESHOLD) / (1 - settings.LEFTAXISTHRESHOLD));
}
} else if (axisId === "AXIS_2" && _gamepads[pad].axes[2] <= 1) {
if (magitudeRight < settings.RIGHTAXISTHRESHOLD) {
movedAmount = 0;
}
else {
movedAmount = (_gamepads[pad].axes[2] / magitudeRight) *
((magitudeRight - settings.RIGHTAXISTHRESHOLD) / (1 - settings.RIGHTAXISTHRESHOLD));
}
} else if (axisId === "AXIS_3" && _gamepads[pad].axes[3] <= 1) {
if (magitudeRight < settings.RIGHTAXISTHRESHOLD) {
movedAmount = 0;
}
else {
movedAmount = (_gamepads[pad].axes[3] / magitudeRight) *
((magitudeRight - settings.RIGHTAXISTHRESHOLD) / (1 - settings.RIGHTAXISTHRESHOLD));
}
}
return movedAmount;
} else {
return movedAmount;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment