Skip to content
Snippets Groups Projects
Commit 04450921 authored by Louis MALTERRE's avatar Louis MALTERRE
Browse files

ptn ouais j'ai normalement debugue les collisions bas (ct de la mrd) LM

parent 4b4fb889
No related branches found
No related tags found
No related merge requests found
...@@ -17,16 +17,18 @@ public class Character extends Entity{ ...@@ -17,16 +17,18 @@ public class Character extends Entity{
private double masse; private double masse;
private HitBox hitBox; private HitBox hitBox;
private int alpha = 1; // gere les collisions avec le sol 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 characterWIDTH = 30; // pour l'instant je les ai renommes, ca peut changer
private final double characterHEIGHT= 60; private final double characterHEIGHT= 60;
private Map<String,Double> tableCommande; //table des commandes du perso private Map<String,Double> tableCommande; //table des commandes du perso
//private BufferedImage sprite; //private BufferedImage sprite;
private Obstacle obstacleBeneath = null; private Obstacle obstacleBeneath = Jeu.getSol();
public Character(Coordonnees c, double vitesseMax, double m, double impSaut){ public Character(Coordonnees c, double vitesseMax, double m, double impSaut){
super(c,vitesseMax,null); // comme tous les entities ont une hitbox, j'ai factorise super(c,vitesseMax,null); // comme tous les entities ont une hitbox, j'ai factorise
this.masse = m; this.masse = m;
this.oldCoord = this.getCoord();
this.impulsionSaut = impSaut; this.impulsionSaut = impSaut;
this.hitBox = new HitBox(c,characterHEIGHT,characterWIDTH); this.hitBox = new HitBox(c,characterHEIGHT,characterWIDTH);
this.tableCommande = new HashMap<>(); this.tableCommande = new HashMap<>();
...@@ -42,23 +44,30 @@ public class Character extends Entity{ ...@@ -42,23 +44,30 @@ public class Character extends Entity{
//on actualise les positions //on actualise les positions
double newX,newZ; 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 this.setCoord(new Coordonnees(newX, newZ)); // on set les nouvelles coordonnees
System.out.println("z = "+this.getCoord().getZ());
//on actualise les vitesses //on actualise les vitesses
vitesseActuX = tableCommande.get("CommandX"); vitesseActuX = tableCommande.get("CommandX");
if ((tableCommande.get("CommandZ") == 0) && (alpha == 1)) vitesseActuZ = 0; if (alpha == 1) vitesseActuZ = 0;
else vitesseActuZ = vitesseActuZ + 7*((1-alpha)*Physique.g - tableCommande.get("CommandZ"))*delta; /*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 @Override
...@@ -81,10 +90,33 @@ public class Character extends Entity{ ...@@ -81,10 +90,33 @@ public class Character extends Entity{
} }
public void checkIfOnGround(){ // faut modifier la condition pour la gestion de colisions avec le sol 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; 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(){ public void resetCommand(){
for (String key : tableCommande.keySet()){ for (String key : tableCommande.keySet()){
tableCommande.replace(key, (double)0); tableCommande.replace(key, (double)0);
...@@ -92,9 +124,12 @@ public class Character extends Entity{ ...@@ -92,9 +124,12 @@ public class Character extends Entity{
} }
public void evolveCharacter(){ public void evolveCharacter(){
System.out.println(alpha);
getGameCommand(); getGameCommand();
checkIfOnGround(); checkIfOnGround();
//checkIfNextToWall();
deplacements(); deplacements();
//checkIfNextToWall();
resetCommand(); resetCommand();
this.hitBox = new HitBox(this.getCoord(),characterHEIGHT,characterWIDTH); // mise à jour crade pour l'instant this.hitBox = new HitBox(this.getCoord(),characterHEIGHT,characterWIDTH); // mise à jour crade pour l'instant
} }
......
...@@ -4,7 +4,8 @@ import environnement.Coordonnees; ...@@ -4,7 +4,8 @@ import environnement.Coordonnees;
import environnement.HitBox; import environnement.HitBox;
public abstract class Entity { public abstract class Entity {
private Coordonnees coord; private Coordonnees coord;
protected Coordonnees oldCoord;
final double vitesseMax; final double vitesseMax;
protected HitBox hitBox; protected HitBox hitBox;
...@@ -30,5 +31,8 @@ public abstract class Entity { ...@@ -30,5 +31,8 @@ public abstract class Entity {
public double getEntityHEIGHT() { public double getEntityHEIGHT() {
return entityHEIGHT; return entityHEIGHT;
} }
public Coordonnees getOldCoord() {
return oldCoord;
}
} }
...@@ -44,7 +44,8 @@ public class HitBox { ...@@ -44,7 +44,8 @@ public class HitBox {
return !(A.extremites.get("HautDroite").getX() <= B.extremites.get("HautGauche").getX());// A est à gauche de B return !(A.extremites.get("HautDroite").getX() <= B.extremites.get("HautGauche").getX());// A est à gauche de B
} }
public static Boolean collisionBas(HitBox A, HitBox 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){ 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 return !(A.extremites.get("BasGauche").getZ() <= B.extremites.get("HautGauche").getZ());// A est au dessus de B
......
...@@ -20,7 +20,7 @@ import loaders.CharacterLoader; ...@@ -20,7 +20,7 @@ import loaders.CharacterLoader;
public class Jeu implements Game{ public class Jeu implements Game{
//coordonnees du sol //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 //perso
static Character gameCharacter; static Character gameCharacter;
...@@ -66,7 +66,7 @@ public class Jeu implements Game{ ...@@ -66,7 +66,7 @@ public class Jeu implements Game{
characterSkinPath = beginPath + movement + numberOfMovement + ".png"; // path premiere frame characterSkinPath = beginPath + movement + numberOfMovement + ".png"; // path premiere frame
/*A = new ArrayList<Double>(); /*A = new ArrayList<Double>();
n = 0;*/ 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(); gameCharacter = gameCharacterLoader.getGameCharacter();
try { try {
helpReader = new BufferedReader(new FileReader(source)); helpReader = new BufferedReader(new FileReader(source));
...@@ -97,6 +97,8 @@ public class Jeu implements Game{ ...@@ -97,6 +97,8 @@ public class Jeu implements Game{
obstacleTable.add(sol); obstacleTable.add(sol);
obstacleTable.add(new Obstacle(new HitBox(new Coordonnees(300,550),32,128))); 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{ ...@@ -170,9 +172,9 @@ public class Jeu implements Game{
return false; return false;
} }
public static Coordonnees getCoordSol(){ /*public static Coordonnees getCoordSol(){
return coordSol; return coordSol;
} }*/
public static Map<String, Boolean> getDirectionJeu() { public static Map<String, Boolean> getDirectionJeu() {
return directionJeu; return directionJeu;
......
...@@ -29,7 +29,7 @@ public class Painter implements GamePainter{ ...@@ -29,7 +29,7 @@ public class Painter implements GamePainter{
Graphics2D crayon = (Graphics2D) im.getGraphics(); Graphics2D crayon = (Graphics2D) im.getGraphics();
crayon.setColor(Color.blue); crayon.setColor(Color.blue);
drawCharacter(crayon,gameCharacterLoader.getImageCharacter()); drawCharacter(crayon,gameCharacterLoader.getImageCharacter());
drawObstacle(crayon, sol); /*drawSol(crayon);
if (!collision(gameCharacter.getHitBox(), test.getHitbox())){ if (!collision(gameCharacter.getHitBox(), test.getHitbox())){
crayon.setColor(Color.blue); crayon.setColor(Color.blue);
} }
...@@ -37,7 +37,9 @@ public class Painter implements GamePainter{ ...@@ -37,7 +37,9 @@ public class Painter implements GamePainter{
crayon.setColor(Color.red); crayon.setColor(Color.red);
} }
drawObstacle(crayon,test); drawObstacle(crayon,test);
//drawQuadrillage(crayon); //drawQuadrillage(crayon);*/
drawMap(crayon);
drawSol(crayon);
} }
...@@ -55,7 +57,7 @@ public class Painter implements GamePainter{ ...@@ -55,7 +57,7 @@ public class Painter implements GamePainter{
int x = (int) gameCharacter.getCoord().getX(); int x = (int) gameCharacter.getCoord().getX();
int y = (int) gameCharacter.getCoord().getZ(); 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.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{ ...@@ -73,6 +75,18 @@ public class Painter implements GamePainter{
} }
private void drawSol(Graphics2D crayon){ 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);
}
} }
} }
...@@ -3,6 +3,7 @@ package map; ...@@ -3,6 +3,7 @@ package map;
import java.util.List; import java.util.List;
import entity.Character; import entity.Character;
import environnement.HitBox; import environnement.HitBox;
import jeu.Jeu; import jeu.Jeu;
...@@ -19,12 +20,12 @@ public class Obstacle { ...@@ -19,12 +20,12 @@ public class Obstacle {
return hitbox; return hitbox;
} }
public static Boolean collisionBas(Character charac,List<Obstacle> list){ /*public static Boolean collisionBas(Character charac,List<Obstacle> list){
for (Obstacle obs : list){ for (Obstacle obs : list){
if (obs != Jeu.getSol()){ 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); charac.setObstacleBeneath(obs);
return true; return true;
...@@ -39,5 +40,32 @@ public class Obstacle { ...@@ -39,5 +40,32 @@ public class Obstacle {
} }
}//a revoir }//a revoir
return false; 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;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment