From 0445092196ffedf087487f19ae1f54721a6a78cb Mon Sep 17 00:00:00 2001 From: Louis MALTERRE <malterre.louis@laposte.net> Date: Mon, 28 Nov 2022 23:27:47 +0100 Subject: [PATCH] ptn ouais j'ai normalement debugue les collisions bas (ct de la mrd) LM --- .../src/main/java/entity/Character.java | 55 +++++++++++++++---- MAVENProject/src/main/java/entity/Entity.java | 6 +- .../src/main/java/environnement/HitBox.java | 3 +- MAVENProject/src/main/java/jeu/Jeu.java | 10 ++-- MAVENProject/src/main/java/jeu/Painter.java | 22 ++++++-- MAVENProject/src/main/java/map/Obstacle.java | 32 ++++++++++- 6 files changed, 106 insertions(+), 22 deletions(-) diff --git a/MAVENProject/src/main/java/entity/Character.java b/MAVENProject/src/main/java/entity/Character.java index b1842f3..1b33b1f 100644 --- a/MAVENProject/src/main/java/entity/Character.java +++ b/MAVENProject/src/main/java/entity/Character.java @@ -17,16 +17,18 @@ public class Character extends Entity{ private double masse; private HitBox hitBox; private int alpha = 1; // gere les collisions avec le sol + private final double characterWIDTH = 30; // pour l'instant je les ai renommes, ca peut changer private final double characterHEIGHT= 60; private Map<String,Double> tableCommande; //table des commandes du perso //private BufferedImage sprite; - private Obstacle obstacleBeneath = null; + private Obstacle obstacleBeneath = Jeu.getSol(); public Character(Coordonnees c, double vitesseMax, double m, double impSaut){ super(c,vitesseMax,null); // comme tous les entities ont une hitbox, j'ai factorise this.masse = m; + this.oldCoord = this.getCoord(); this.impulsionSaut = impSaut; this.hitBox = new HitBox(c,characterHEIGHT,characterWIDTH); this.tableCommande = new HashMap<>(); @@ -42,23 +44,30 @@ public class Character extends Entity{ //on actualise les positions double newX,newZ; + this.oldCoord = this.getCoord(); - newX = tableCommande.get("CommandX")*delta + this.getCoord().getX(); // frottements enleves (le perso finissait par freiner tout seul donc tant que y'a pas d'interet je les enleve, j'ai tjr les equations de cote) + newX = tableCommande.get("CommandX")*delta + this.getCoord().getX(); - if ((tableCommande.get("CommandZ") == 0) && (this.getCoord().getZ() >= Jeu.getCoordSol().getZ()))newZ = obstacleBeneath.getHitbox().getExtremites().get("HautGauche").getZ(); - else newZ = ((1-alpha)*Physique.g - tableCommande.get("CommandZ"))/2*delta*delta + vitesseActuZ*delta + this.getCoord().getZ(); + + newZ = ((1-alpha)*Physique.g - tableCommande.get("CommandZ"))/2*delta*delta + vitesseActuZ*delta + this.getCoord().getZ(); + if (newZ+characterHEIGHT/2 >= obstacleBeneath.getHitbox().getExtremites().get("HautGauche").getZ()){ + newZ = obstacleBeneath.getHitbox().getExtremites().get("HautGauche").getZ()-characterHEIGHT/2; + //alpha = 1; + } - //System.out.println("écart de hauteur: "+this.getCoord().getZ()); + //(tableCommande.get("CommandZ") == 0) this.getCoord().getZ()(tableCommande.get("CommandZ") == 0) && this.setCoord(new Coordonnees(newX, newZ)); // on set les nouvelles coordonnees - + System.out.println("z = "+this.getCoord().getZ()); + //on actualise les vitesses vitesseActuX = tableCommande.get("CommandX"); - if ((tableCommande.get("CommandZ") == 0) && (alpha == 1)) vitesseActuZ = 0; - else vitesseActuZ = vitesseActuZ + 7*((1-alpha)*Physique.g - tableCommande.get("CommandZ"))*delta; + if (alpha == 1) vitesseActuZ = 0; + /*else*/ vitesseActuZ = vitesseActuZ + 7*((1-alpha)*Physique.g - tableCommande.get("CommandZ"))*delta; + System.out.println("vitesse z = "+vitesseActuZ); + - //System.out.println("vitesse z: "+vitesseActuZ); } @Override @@ -81,10 +90,33 @@ public class Character extends Entity{ } public void checkIfOnGround(){ // faut modifier la condition pour la gestion de colisions avec le sol - if (!Obstacle.collisionBas(this, Jeu.getObstacleTable()))alpha = 0; + obstacleBeneathCharacter(); // cherche l'obstacle en-dessous du personnage + if (!Obstacle.collisionBas(this)){ + alpha = 0; + } else alpha = 1; } + public void obstacleBeneathCharacter(){ + Obstacle obsMin = Jeu.getSol(); + int compteur = 0; + for (Obstacle obs : Jeu.getObstacleTable()){ + compteur++; + if (obs != Jeu.getSol() && obs.getHitbox().getExtremites().get("HautDroite").getZ() >= this.getCoord().getZ() && HitBox.collisionDroite(hitBox, obs.getHitbox()) && HitBox.collisionGauche(hitBox, obs.getHitbox())){ + if (obs.getHitbox().getExtremites().get("HautDroite").getZ() < obsMin.getHitbox().getExtremites().get("HautDroite").getZ()){ + obsMin = obs; + System.out.println("obstacle = "+compteur); + } + } + }obstacleBeneath = obsMin; + } + + public void checkIfNextToWall(){ + if ((Obstacle.collisionDroite(this, Jeu.getObstacleTable()) && tableCommande.get("CommandX") == -vitesseMax) || (Obstacle.collisionGauche(this, Jeu.getObstacleTable()) && tableCommande.get("CommandX") == vitesseMax)){ + this.setCoord(new Coordonnees(this.oldCoord.getX(), this.getCoord().getZ())); + } + } + public void resetCommand(){ for (String key : tableCommande.keySet()){ tableCommande.replace(key, (double)0); @@ -92,9 +124,12 @@ public class Character extends Entity{ } public void evolveCharacter(){ + System.out.println(alpha); getGameCommand(); checkIfOnGround(); + //checkIfNextToWall(); deplacements(); + //checkIfNextToWall(); resetCommand(); this.hitBox = new HitBox(this.getCoord(),characterHEIGHT,characterWIDTH); // mise à jour crade pour l'instant } diff --git a/MAVENProject/src/main/java/entity/Entity.java b/MAVENProject/src/main/java/entity/Entity.java index 928320c..3b4dbe4 100644 --- a/MAVENProject/src/main/java/entity/Entity.java +++ b/MAVENProject/src/main/java/entity/Entity.java @@ -4,7 +4,8 @@ import environnement.Coordonnees; import environnement.HitBox; public abstract class Entity { - private Coordonnees coord; + private Coordonnees coord; + protected Coordonnees oldCoord; final double vitesseMax; protected HitBox hitBox; @@ -30,5 +31,8 @@ public abstract class Entity { public double getEntityHEIGHT() { return entityHEIGHT; } + public Coordonnees getOldCoord() { + return oldCoord; + } } diff --git a/MAVENProject/src/main/java/environnement/HitBox.java b/MAVENProject/src/main/java/environnement/HitBox.java index 28b8dc7..f8e5c4f 100644 --- a/MAVENProject/src/main/java/environnement/HitBox.java +++ b/MAVENProject/src/main/java/environnement/HitBox.java @@ -44,7 +44,8 @@ public class HitBox { return !(A.extremites.get("HautDroite").getX() <= B.extremites.get("HautGauche").getX());// A est à gauche de B } public static Boolean collisionBas(HitBox A, HitBox B){ - return !(A.extremites.get("HautGauche").getZ() >= B.extremites.get("BasGauche").getZ());// A est en dessous de B + System.out.println(A.extremites.get("HautGauche").getZ()+" "+B.extremites.get("BasGauche").getZ()); + return !(A.extremites.get("HautGauche").getZ() > B.extremites.get("BasGauche").getZ());// A est en dessous de B } public static Boolean collisionHaut(HitBox A, HitBox B){ return !(A.extremites.get("BasGauche").getZ() <= B.extremites.get("HautGauche").getZ());// A est au dessus de B diff --git a/MAVENProject/src/main/java/jeu/Jeu.java b/MAVENProject/src/main/java/jeu/Jeu.java index 7262ea5..83cbab2 100644 --- a/MAVENProject/src/main/java/jeu/Jeu.java +++ b/MAVENProject/src/main/java/jeu/Jeu.java @@ -20,7 +20,7 @@ import loaders.CharacterLoader; public class Jeu implements Game{ //coordonnees du sol - private static Coordonnees coordSol = new Coordonnees(200,700); //coordonnees du sol (en realite juste Z nous interesse) + //private static Coordonnees coordSol = new Coordonnees(200,700); //coordonnees du sol (en realite juste Z nous interesse) //perso static Character gameCharacter; @@ -66,7 +66,7 @@ public class Jeu implements Game{ characterSkinPath = beginPath + movement + numberOfMovement + ".png"; // path premiere frame /*A = new ArrayList<Double>(); n = 0;*/ - gameCharacterLoader = new CharacterLoader(characterSkinPath,new Character(new Coordonnees(200, 300), 2000, 1,600000)); + gameCharacterLoader = new CharacterLoader(characterSkinPath,new Character(new Coordonnees(300, 300), 2000, 1,600000)); gameCharacter = gameCharacterLoader.getGameCharacter(); try { helpReader = new BufferedReader(new FileReader(source)); @@ -97,6 +97,8 @@ public class Jeu implements Game{ obstacleTable.add(sol); obstacleTable.add(new Obstacle(new HitBox(new Coordonnees(300,550),32,128))); + obstacleTable.add(new Obstacle(new HitBox(new Coordonnees(800, 625),150,50))); + obstacleTable.add(new Obstacle(new HitBox(new Coordonnees(600, 662.5),75,50))); } @@ -170,9 +172,9 @@ public class Jeu implements Game{ return false; } - public static Coordonnees getCoordSol(){ + /*public static Coordonnees getCoordSol(){ return coordSol; - } + }*/ public static Map<String, Boolean> getDirectionJeu() { return directionJeu; diff --git a/MAVENProject/src/main/java/jeu/Painter.java b/MAVENProject/src/main/java/jeu/Painter.java index 697bc22..c9db59e 100644 --- a/MAVENProject/src/main/java/jeu/Painter.java +++ b/MAVENProject/src/main/java/jeu/Painter.java @@ -29,7 +29,7 @@ public class Painter implements GamePainter{ Graphics2D crayon = (Graphics2D) im.getGraphics(); crayon.setColor(Color.blue); drawCharacter(crayon,gameCharacterLoader.getImageCharacter()); - drawObstacle(crayon, sol); + /*drawSol(crayon); if (!collision(gameCharacter.getHitBox(), test.getHitbox())){ crayon.setColor(Color.blue); } @@ -37,7 +37,9 @@ public class Painter implements GamePainter{ crayon.setColor(Color.red); } drawObstacle(crayon,test); - //drawQuadrillage(crayon); + //drawQuadrillage(crayon);*/ + drawMap(crayon); + drawSol(crayon); } @@ -55,7 +57,7 @@ public class Painter implements GamePainter{ int x = (int) gameCharacter.getCoord().getX(); int y = (int) gameCharacter.getCoord().getZ(); crayon.fillRect(x-(int) gameCharacter.getEntityWIDTH()/2,y-(int) gameCharacter.getEntityHEIGHT()/2, (int) gameCharacter.getEntityWIDTH(),(int) gameCharacter.getEntityHEIGHT()); - crayon.drawImage(imageCharac, x-(int) gameCharacter.getEntityWIDTH()/2, y- (int) gameCharacter.getEntityHEIGHT()/2, null, null); + crayon.drawImage(imageCharac, x-(int) gameCharacter.getEntityWIDTH()/2-10, y- (int) gameCharacter.getEntityHEIGHT()/2-3, null, null); } @@ -73,6 +75,18 @@ public class Painter implements GamePainter{ } private void drawSol(Graphics2D crayon){ - crayon.drawLine(0,(int) getCoordSol().getZ(),WIDTH,(int) getCoordSol().getZ()); + crayon.drawLine(0,(int) Jeu.getSol().getHitbox().getExtremites().get("HautDroite").getZ(),WIDTH,(int) Jeu.getSol().getHitbox().getExtremites().get("HautDroite").getZ()); + } + + private void drawMap(Graphics2D crayon){ + for (Obstacle obs : Jeu.getObstacleTable()){ + if (!collision(gameCharacter.getHitBox(), obs.getHitbox())){ + crayon.setColor(Color.blue); + } + else { + crayon.setColor(Color.red); + } + drawObstacle(crayon,obs); + } } } diff --git a/MAVENProject/src/main/java/map/Obstacle.java b/MAVENProject/src/main/java/map/Obstacle.java index bb2c884..b155441 100644 --- a/MAVENProject/src/main/java/map/Obstacle.java +++ b/MAVENProject/src/main/java/map/Obstacle.java @@ -3,6 +3,7 @@ package map; import java.util.List; import entity.Character; + import environnement.HitBox; import jeu.Jeu; @@ -19,12 +20,12 @@ public class Obstacle { return hitbox; } - public static Boolean collisionBas(Character charac,List<Obstacle> list){ + /*public static Boolean collisionBas(Character charac,List<Obstacle> list){ for (Obstacle obs : list){ if (obs != Jeu.getSol()){ - if (HitBox.collisionDroite(charac.getHitBox(),obs.getHitbox()) && HitBox.collisionGauche(charac.getHitBox(),obs.getHitbox()) && HitBox.collisionBas(obs.getHitbox(),charac.getHitBox())){ + if (charac.getCoord().getZ()+charac.getEntityHEIGHT() < obs.getHitbox().getExtremites().get("HautGauche").getZ() && HitBox.collisionDroite(charac.getHitBox(),obs.getHitbox()) && HitBox.collisionGauche(charac.getHitBox(),obs.getHitbox()) && HitBox.collisionBas(obs.getHitbox(),charac.getHitBox())){ charac.setObstacleBeneath(obs); return true; @@ -39,5 +40,32 @@ public class Obstacle { } }//a revoir return false; + }*/ + + public static Boolean collisionBas(Character charac){ + if (charac.getObstacleBeneath() != Jeu.getSol()){ + if (HitBox.collisionDroite(charac.getHitBox(),charac.getObstacleBeneath().getHitbox()) && HitBox.collisionGauche(charac.getHitBox(),charac.getObstacleBeneath().getHitbox()) && HitBox.collisionBas(charac.getObstacleBeneath().getHitbox(),charac.getHitBox())) { + return true; + } + }else{ + if (HitBox.collisionBas(charac.getObstacleBeneath().getHitbox(),charac.getHitBox())) return true; + } + return false; + } + + public static Boolean collisionGauche(Character charac,List<Obstacle> list){ + for (Obstacle obs : list){ + if (HitBox.collisionGauche(charac.getHitBox(), obs.getHitbox()) && obs != Jeu.getSol() && !HitBox.collisionHaut(obs.getHitbox(),charac.getHitBox())) return true; + + } + return false; + } + + public static Boolean collisionDroite(Character charac,List<Obstacle> list){ + for (Obstacle obs : list){ + if (HitBox.collisionDroite(charac.getHitBox(), obs.getHitbox()) && obs != Jeu.getSol() && !HitBox.collisionHaut(obs.getHitbox(),charac.getHitBox())) return true; + + } + return false; } } -- GitLab