Search code examples
javajinput

40 If-Statements in a Loop, How to Avoid?


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:

  1. 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.

  2. //do stuff is just increasing the x and y coordinates of the player by certain amounts.


Solution

  • The best approach might be to discover a rule whereby you can directly compute the player's coordinate change directly from the joystick coordinates. For that, we would need details of what // do stuff is for each branch.

    Another possibility, which is good if you don't have a simple relationship, is to convert your tests to a look-up by transforming the x and y values to array indexes

    int xIndex = (int) (10 * x + 10); // an int between 0 and 20
    int yIndex = (int) (10 * y + 10); // ditto
    

    Then you can look up the player's coordinate changes in a 2-D array that you compute once ahead of time. More generally, you could have an array of Runnable objects, look up the appropriate Runnable based on the indexes, and run() it.

    P.S. If each joystick coordinate can fall into any of 20 different ranges, then you have 400 cases, not 40. That is, unless you are always ignoring one or the other of the axes.