I am working with the library JInput to detect controller input in a java game. However, in order to get the direction the axis is moving, I use this:
gamePadContr.getComponent(Identifier.Axis.X).getPollData();
This gives me a decimal between -1 and 1, which tell the direction. I want the player to be able to move in more than just up, down, left, and right, so there are 40 if statements in my gameloop that check if the number is a certain set of numbers, and increasing the x and y coordinates accordingly. Here is an example:
if(x > .1 && x < .2 && y == 1){
// do stuff
} else
if(x > .2 && x < .3 && y == 1{
// do stuff
}
I have to check each one to make sure that most directions are accounted for.
My point is, 40 if statements is lagging. Is there anything I can do to optimize this?
A few edits to answer questions:
Didn't think it was relevant, but the reason increments of .1 have 40 different if statements is because I am checking the X and Y axis of the joystick. The possible combinations are (1, -1) (1, 1) (-1, 1) (-1, -1), giving me negatives and positives in 40 different combinations at increments of .1. (I don't know if that made sense.
//do stuff is just increasing the x and y coordinates of the player by certain amounts.