Scale controller axis values to match how Xinput reads them on the PC. Clients that support this must have proper deadzone support (incorrect deadzones will be noticeable) and set ControllerPacket.enableAxisScaling

This commit is contained in:
Cameron Gutman 2014-10-01 20:22:06 -07:00
parent ed8857552b
commit 431ba06742

View File

@ -46,6 +46,17 @@ public class ControllerPacket extends InputPacket {
public static final short PACKET_LENGTH = PAYLOAD_LENGTH + public static final short PACKET_LENGTH = PAYLOAD_LENGTH +
InputPacket.HEADER_LENGTH; InputPacket.HEADER_LENGTH;
// This is the highest value that is read as zero on the PC
public static final short ABS_LS_OFFSET = 7000;
public static final short ABS_RS_OFFSET = 7000;
public static final double ABS_LS_SCALE_FACTOR = 1 - (ABS_LS_OFFSET / 32768.0);
public static final double ABS_RS_SCALE_FACTOR = 1 - (ABS_RS_OFFSET / 32768.0);
// Set this flag if you want ControllerPacket to handle scaling for you
// Note: You MUST properly handle deadzones to use this flag
public static boolean enableAxisScaling = false;
short buttonFlags; short buttonFlags;
byte leftTrigger; byte leftTrigger;
byte rightTrigger; byte rightTrigger;
@ -63,10 +74,51 @@ public class ControllerPacket extends InputPacket {
this.buttonFlags = buttonFlags; this.buttonFlags = buttonFlags;
this.leftTrigger = leftTrigger; this.leftTrigger = leftTrigger;
this.rightTrigger = rightTrigger; this.rightTrigger = rightTrigger;
this.leftStickX = leftStickX; this.leftStickX = scaleLeftStickAxis(leftStickX);
this.leftStickY = leftStickY; this.leftStickY = scaleLeftStickAxis(leftStickY);
this.rightStickX = rightStickX; this.rightStickX = scaleRightStickAxis(rightStickX);
this.rightStickY = rightStickY; this.rightStickY = scaleRightStickAxis(rightStickY);
}
private static short scaleAxis(int axisValue, short offset, double factor) {
// Exit quit if it's zero
if (axisValue == 0) {
return 0;
}
// Remember the sign and remove it from the value
int sign = axisValue < 0 ? -1 : 1;
axisValue = Math.abs(axisValue);
// Scale the initial value
axisValue = (int)(axisValue * factor);
// Add the offset
axisValue += offset;
// Correct the value if it's over the limit
if (axisValue > 32767) {
axisValue = 32767;
}
// Restore sign and return
return (short)(sign * axisValue);
}
private static short scaleLeftStickAxis(short axisValue) {
if (enableAxisScaling) {
axisValue = scaleAxis(axisValue, ABS_LS_OFFSET, ABS_LS_SCALE_FACTOR);
}
return axisValue;
}
private static short scaleRightStickAxis(short axisValue) {
if (enableAxisScaling) {
axisValue = scaleAxis(axisValue, ABS_RS_OFFSET, ABS_RS_SCALE_FACTOR);
}
return axisValue;
} }
@Override @Override