Created
February 28, 2014 18:31
-
-
Save videlais/9276856 to your computer and use it in GitHub Desktop.
Gamepad axis 'moved' function for CocoonJS on the Ouya using a high-precision radial deadzone algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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