From 62b888da92f6b2289b2d74de7f0d97b00a61063b Mon Sep 17 00:00:00 2001 From: Antoine Date: Tue, 1 Jun 2021 22:22:10 +0200 Subject: [PATCH 1/4] getAxisDiscreet && getAxisContinuous added --- src/engine/Engine.java | 14 ++-- src/engine/input/GamepadInput.java | 111 +++++++++++++++++++-------- src/engine/input/inputConst.java | 17 ++++ src/engine/input/inputVariables.java | 7 -- src/engine/math/Vector3f.java | 5 ++ 5 files changed, 110 insertions(+), 44 deletions(-) create mode 100644 src/engine/input/inputConst.java delete mode 100644 src/engine/input/inputVariables.java 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; From cf7a1ed85b77b17b280dc74d811fcf378e36fd53 Mon Sep 17 00:00:00 2001 From: Antoine Date: Tue, 1 Jun 2021 22:33:11 +0200 Subject: [PATCH 2/4] Oune poquito de docito + Rename de certaine classe pour qu'elles respectent les conventions --- src/engine/Engine.java | 5 ++-- src/engine/input/GamepadInput.java | 29 ++++++++----------- .../{inputConst.java => InputConst.java} | 2 +- .../input/{Input.java => KeyboardInput.java} | 10 +++---- 4 files changed, 20 insertions(+), 26 deletions(-) rename src/engine/input/{inputConst.java => InputConst.java} (95%) rename src/engine/input/{Input.java => KeyboardInput.java} (92%) diff --git a/src/engine/Engine.java b/src/engine/Engine.java index dcebdcb..ade39db 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -1,7 +1,6 @@ package engine; -import engine.input.Input; -//import engine.input.gamepadInput; +import engine.input.KeyboardInput; import engine.input.GamepadInput; import engine.math.*; import engine.object.*; @@ -73,7 +72,7 @@ public class Engine { // On met la fenêtre au centre de l'écran principale glfwSetWindowPos(getWindow(), (vidmode.width() - width) / 2, (vidmode.height() - height) / 2); - glfwSetKeyCallback(getWindow(), new Input()); + glfwSetKeyCallback(getWindow(), new KeyboardInput()); glfwSetInputMode(getWindow(), GLFW_STICKY_KEYS, GLFW_TRUE); // Contexte = zone cible des rendus diff --git a/src/engine/input/GamepadInput.java b/src/engine/input/GamepadInput.java index 319bc31..9b68c50 100644 --- a/src/engine/input/GamepadInput.java +++ b/src/engine/input/GamepadInput.java @@ -5,7 +5,7 @@ import engine.math.Vector3f; import java.nio.ByteBuffer; import java.nio.FloatBuffer; -import static engine.input.inputConst.*; +import static engine.input.InputConst.*; import static org.lwjgl.glfw.GLFW.*; /* Buttons 0 : Croix / A @@ -36,6 +36,8 @@ import static org.lwjgl.glfw.GLFW.*; public class GamepadInput { private final int gamepadNum; + private ByteBuffer gamepadButton; + private FloatBuffer gamepadAxes; public boolean buttonA_pressed ; public boolean buttonB_pressed ; @@ -97,9 +99,10 @@ public class GamepadInput { this.RT_pressed = false; } - public void inputRefresh() { - ByteBuffer gamepadButton = glfwGetJoystickButtons(this.gamepadNum); - FloatBuffer gamepadAxes = glfwGetJoystickAxes(this.gamepadNum); + public void inputRefresh() { + + this.gamepadButton = glfwGetJoystickButtons(this.gamepadNum); + this.gamepadAxes = glfwGetJoystickAxes(this.gamepadNum); assert gamepadAxes != null; assert gamepadButton != null; @@ -153,7 +156,7 @@ public class GamepadInput { } public boolean checkPressed(int keyCode){ - ByteBuffer gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1); + this.gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1); assert gamepadButton != null; @@ -161,16 +164,12 @@ public class GamepadInput { } /** - * - * @param axisId - * @return + * Envoie la position du stick pointé par axisId sous forme de direction général (9 directions) + * @param axisId l'identifiant du joystick (AXE X) + * @return la direction du stick valeur possible : RIGHT, UPPER_RIGHT, UP, UPPER_LEFT, LEFT, LOWER_LEFT, DOWN, LOWER_RIGHT, CENTER; */ 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); @@ -191,16 +190,12 @@ public class GamepadInput { } /** - * + * Envoie la position du stick pointé par axisID (AXE X) sous forme de Vecteur * @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); diff --git a/src/engine/input/inputConst.java b/src/engine/input/InputConst.java similarity index 95% rename from src/engine/input/inputConst.java rename to src/engine/input/InputConst.java index 4fb4fc1..e45db72 100644 --- a/src/engine/input/inputConst.java +++ b/src/engine/input/InputConst.java @@ -1,6 +1,6 @@ package engine.input; -public class inputConst { +public class InputConst { /* Button ID */ diff --git a/src/engine/input/Input.java b/src/engine/input/KeyboardInput.java similarity index 92% rename from src/engine/input/Input.java rename to src/engine/input/KeyboardInput.java index e734f2d..25fe04c 100644 --- a/src/engine/input/Input.java +++ b/src/engine/input/KeyboardInput.java @@ -12,7 +12,7 @@ import java.nio.FloatBuffer; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; -public class Input extends GLFWKeyCallback { +public class KeyboardInput extends GLFWKeyCallback { public static boolean[] keys = new boolean[65536]; @@ -89,18 +89,18 @@ public class Input extends GLFWKeyCallback { public static void keyboardInput(ObjectGl token, int speed) { boolean keyPressed = false; - if (Input.isKeyDown(GLFW.GLFW_KEY_S)) { + if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_S)) { token.setTextureWrap(161,260,56,59, ObjectGl.STICK_BOTTOM); keyPressed = true; - } else if (Input.isKeyDown(GLFW.GLFW_KEY_W)) { + } else if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_W)) { keyPressed = true; } - if (Input.isKeyDown(GLFW.GLFW_KEY_A)) { + if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_A)) { token.translate(new Vector3f (speed * -5.0f, 0.0f, 0.0f)); token.setTextureWrap(121,0,57,82, ObjectGl.STICK_TOP); keyPressed = true; } - else if (Input.isKeyDown(GLFW.GLFW_KEY_D)) { + else if (KeyboardInput.isKeyDown(GLFW.GLFW_KEY_D)) { token.translate(new Vector3f (speed * 5.0f, 0.0f, 0.0f)); token.setTextureWrap(178,0,62,82, ObjectGl.STICK_TOP); keyPressed = true; From c22f4266d185f1b80f00e3bd357097b60936c036 Mon Sep 17 00:00:00 2001 From: Boyeau Indy Date: Wed, 2 Jun 2021 08:42:46 +0000 Subject: [PATCH 3/4] Update config.json --- src/configuration/config.json | 55 ++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/configuration/config.json b/src/configuration/config.json index 4c4b7fd..fbd5c52 100644 --- a/src/configuration/config.json +++ b/src/configuration/config.json @@ -1,40 +1,55 @@ { - "test": "test valide", "arena": [ + "random", "arena1.png", - "arena2.png", - "random" + "arena2.png" ], "nb_players": [ "1", "2" ], "character1": [ + "random", "character1.png", - "character2.png", - "random" + "character2.png" ], "character2": [ + "random", "character1.png", "character1_swapcolor.png", "character2.png", - "character2_swapcolor.png", - "random" + "character2_swapcolor.png" ], "resolution": [ - "1280 x 1024", - "1680 x 1050", - "1920 x 1080", - "800 x 600" - ], + { + "width": "800", + "height": "600" + }, + { + "width": "1280", + "height": "1024" + }, + { + "width": "1680", + "height": "1050" + }, + { + "width": "1920", + "height": "1080" + }, + { + "persoWidth": "800", + "persoHeight": "600" + } + ], "button": [ - "up", - "down", - "rigth", - "left", - "a", - "b", - "c", - "d" + "UP", + "DOWN", + "RIGTH", + "LEFT", + "A", + "B", + "X", + "Y" ] } From bce22204532d8e7ca4ce67be9d9707de762c9f31 Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 2 Jun 2021 15:23:44 +0200 Subject: [PATCH 4/4] Exemple utilisation Input --- src/engine/Engine.java | 41 +++++++++++++++++++++--------- src/engine/input/GamepadInput.java | 2 ++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/engine/Engine.java b/src/engine/Engine.java index ade39db..0ce61be 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -1,7 +1,6 @@ package engine; -import engine.input.KeyboardInput; -import engine.input.GamepadInput; +import engine.input.*; import engine.math.*; import engine.object.*; import engine.sound.*; @@ -206,24 +205,42 @@ public class Engine { long lastFrame; int frame = 0; boolean nextFrame = false; - boolean present = glfwJoystickPresent(GLFW_JOYSTICK_1); - - + boolean Joystick1Present = glfwJoystickPresent(GLFW_JOYSTICK_1); + + /* + * Création des manettes / action + */ + + GamepadInput gamepad1 = null; + Button jump = null; + + if (Joystick1Present){ + gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); + gamepad1.inputRefresh(); + List listJump = new ArrayList<>(); + listJump.add(InputConst.buttonA); + jump = new Button("jump", listJump, gamepad1); + + + + } + + while (engine.isRunning()) { lastFrame = System.currentTimeMillis(); // Game logic should fit here - if (present) { - GamepadInput gamepad1 = new GamepadInput(GLFW_JOYSTICK_1); + if (Joystick1Present) { gamepad1.inputRefresh(); - -// 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)); + + // Check si le personnage a sauté + if (jump.isButtonPressed()){ + // Le personnage saute + System.out.println(" JE SAUTE "); + } } // Input.keyboardInput(zangief, speed); diff --git a/src/engine/input/GamepadInput.java b/src/engine/input/GamepadInput.java index 9b68c50..78bce7e 100644 --- a/src/engine/input/GamepadInput.java +++ b/src/engine/input/GamepadInput.java @@ -73,6 +73,8 @@ public class GamepadInput { public GamepadInput (int gamepadNum){ this.gamepadNum = gamepadNum; + this.gamepadAxes = null; + this.gamepadButton = null; this.buttonA_pressed = false; this.buttonB_pressed = false; this.buttonY_pressed = false;