diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 7792cde..dcebdcb 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -215,14 +215,16 @@ public class Engine { lastFrame = System.currentTimeMillis(); // Game logic should fit here - if (present) { //sprite //bindings - GamepadInput gamepad1 = new GamepadInput(GLFW_JOYSTICK_1, false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false); + if (present) { + GamepadInput gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); gamepad1.inputRefresh(); - System.out.println( " \n left :" + gamepad1.leftJoyLeft + - " \n right :" + gamepad1.leftJoyRight + - " \n down :" + gamepad1.leftJoyDown + - " \n up :" + gamepad1.leftJoyUp); +// System.out.println( " \n left :" + gamepad1.leftJoyLeft + +// " \n right :" + gamepad1.leftJoyRight + +// " \n down :" + gamepad1.leftJoyDown + +// " \n up :" + gamepad1.leftJoyUp); + + System.out.println(gamepad1.getAxisDiscreet(GLFW_GAMEPAD_AXIS_LEFT_X)); } // Input.keyboardInput(zangief, speed); diff --git a/src/engine/input/GamepadInput.java b/src/engine/input/GamepadInput.java index 8d807a2..319bc31 100644 --- a/src/engine/input/GamepadInput.java +++ b/src/engine/input/GamepadInput.java @@ -1,9 +1,11 @@ package engine.input; +import engine.math.Vector3f; + import java.nio.ByteBuffer; import java.nio.FloatBuffer; -import static engine.input.inputVariables.*; +import static engine.input.inputConst.*; import static org.lwjgl.glfw.GLFW.*; /* Buttons 0 : Croix / A @@ -67,35 +69,32 @@ public class GamepadInput { public boolean LT_pressed ; public boolean RT_pressed; - public GamepadInput (int gamepadNum, boolean buttonA_pressed,boolean buttonB_pressed,boolean buttonY_pressed, boolean buttonX_pressed, boolean LB_pressed, - boolean RB_pressed, boolean select_pressed, boolean start_pressed,boolean R_JoyClick_pressed, boolean L_JoyClick_pressed , boolean up_pressed, - boolean down_pressed,boolean left_pressed,boolean right_pressed,boolean leftJoyRight,boolean leftJoyLeft,boolean leftJoyDown, boolean leftJoyUp, - boolean rightJoyLeft, boolean rightJoyRight, boolean rightJoyDown,boolean rightJoyUp,boolean LT_pressed, boolean RT_pressed){ + public GamepadInput (int gamepadNum){ this.gamepadNum = gamepadNum; - this.buttonA_pressed = buttonA_pressed; - this.buttonB_pressed = buttonB_pressed; - this.buttonY_pressed = buttonY_pressed; - this.buttonX_pressed =buttonX_pressed; - this.LB_pressed =LB_pressed; - this.RB_pressed =RB_pressed; - this.select_pressed =select_pressed; - this.start_pressed =start_pressed; - this.R_JoyClick_pressed =R_JoyClick_pressed; - this.L_JoyClick_pressed =L_JoyClick_pressed; - this.up_pressed =up_pressed; - this.down_pressed =down_pressed; - this.left_pressed =left_pressed; - this.right_pressed =right_pressed; - this.leftJoyRight =leftJoyRight; - this.leftJoyLeft =leftJoyLeft; - this.leftJoyDown =leftJoyDown; - this.leftJoyUp =leftJoyUp; - this.rightJoyLeft =rightJoyLeft; - this.rightJoyRight =rightJoyRight; - this.rightJoyDown =rightJoyDown; - this.rightJoyUp =rightJoyUp; - this.LT_pressed =LT_pressed; - this.RT_pressed =RT_pressed; + this.buttonA_pressed = false; + this.buttonB_pressed = false; + this.buttonY_pressed = false; + this.buttonX_pressed = false; + this.LB_pressed = false; + this.RB_pressed = false; + this.select_pressed = false; + this.start_pressed = false; + this.R_JoyClick_pressed = false; + this.L_JoyClick_pressed = false; + this.up_pressed = false; + this.down_pressed = false; + this.left_pressed = false; + this.right_pressed = false; + this.leftJoyRight = false; + this.leftJoyLeft = false; + this.leftJoyDown = false; + this.leftJoyUp = false; + this.rightJoyLeft = false; + this.rightJoyRight = false; + this.rightJoyDown = false; + this.rightJoyUp = false; + this.LT_pressed = false; + this.RT_pressed = false; } public void inputRefresh() { @@ -155,12 +154,62 @@ public class GamepadInput { public boolean checkPressed(int keyCode){ ByteBuffer gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1); - FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1); - assert gamepadAxes != null; assert gamepadButton != null; return gamepadButton.get(keyCode) == 1; } + + /** + * + * @param axisId + * @return + */ + public int getAxisDiscreet(int axisId){ + + FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1); + + assert gamepadAxes != null; + + float x = gamepadAxes.get(axisId); + float y = gamepadAxes.get(axisId + 1); + + float magnitude = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); + float angle = (float) Math.toDegrees(2 * Math.atan(y /(x + magnitude))); + + if (magnitude < 0.1) return CENTER; + if (angle < 22.5 && angle > -22.5) return RIGHT; + else if (angle > -67.5 && angle < -22.5) return UPPER_RIGHT; + else if (angle > -112.5 && angle < -67.5) return UP; + else if (angle > -157.5 && angle < -112.5) return UPPER_LEFT; + else if (angle < -157.5 || angle > 157.5) return LEFT; + else if (angle < 157.5 && angle > 112.5) return LOWER_LEFT; + else if (angle < 112.5 && angle > 67.5) return DOWN; + else if (angle < 67.5 && angle > 22.5) return LOWER_RIGHT; + + return -1; // TEST + } + + /** + * + * @param axisId l'identifiant du joystick (AXE X) + * @return un Vecteur représentant la position actuel du joystick + */ + public Vector3f getAxisContinuous(int axisId){ + + FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1); + + assert gamepadAxes != null; + + float x = gamepadAxes.get(axisId); + float y = gamepadAxes.get(axisId + 1); + + if (x < 0.1 && x > -0.1) x = 0; + if (y < 0.1 && y > -0.1) y = 0; + + System.out.println("x: " + x + " y: " + y); + + return new Vector3f(x,y); + } } diff --git a/src/engine/input/inputConst.java b/src/engine/input/inputConst.java new file mode 100644 index 0000000..4fb4fc1 --- /dev/null +++ b/src/engine/input/inputConst.java @@ -0,0 +1,17 @@ +package engine.input; + +public class inputConst { + /* + Button ID + */ + public final static int buttonA=0, buttonB=1, buttonX=2, buttonY=3, LB = 4, RB = 5, select = 6, start = 7, + L_JoystickClick = 8, R_JoystickClick =9, up = 10, right = 11, down = 12, left = 13; + + /* + Axis ID + */ + public final static int leftJoyX_Axe = 0, leftJoyY_Axe = 1, rightJoyX_Axe = 2, rightJoyY_Axe = 3, LT = 4, RT = 5; + + public final static int RIGHT = 0, UPPER_RIGHT = 1, UP = 2, UPPER_LEFT = 3, LEFT = 4, LOWER_LEFT = 5, + DOWN = 6, LOWER_RIGHT = 7, CENTER = 8; +} diff --git a/src/engine/input/inputVariables.java b/src/engine/input/inputVariables.java deleted file mode 100644 index d8af4f4..0000000 --- a/src/engine/input/inputVariables.java +++ /dev/null @@ -1,7 +0,0 @@ -package engine.input; - -public class inputVariables { - public final static int buttonA=0, buttonB=1, buttonX=2, buttonY=3, LB = 4, RB = 5, select = 6, start = 7, - L_JoystickClick = 8, R_JoystickClick =9, up = 10, right = 11, down = 12, left = 13, - leftJoyX_Axe = 0, leftJoyY_Axe = 1, rightJoyX_Axe = 2, rightJoyY_Axe = 3, LT = 4, RT = 5; -} diff --git a/src/engine/math/Vector3f.java b/src/engine/math/Vector3f.java index 4f7c98b..4f81fee 100644 --- a/src/engine/math/Vector3f.java +++ b/src/engine/math/Vector3f.java @@ -10,6 +10,11 @@ public class Vector3f { z = 0.0f; } + public Vector3f(float x, float y){ + this.x = x; + this.y = y; + } + public Vector3f(float x, float y, float z){ this.x = x; this.y = y;