diff --git a/src/Cercle.java b/src/Cercle.java index 1519197..cca6dec 100644 --- a/src/Cercle.java +++ b/src/Cercle.java @@ -3,7 +3,6 @@ package linea; import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Stroke; import java.awt.geom.Arc2D; public class Cercle extends ObjetGraphique{ // il s'agit plutôt d'arcs de cercle @@ -124,17 +123,29 @@ public class Cercle extends ObjetGraphique{ // il s'agit plutôt d'arcs de cercl } y+=depY; } - - boolean EnCollision(Segment segCourant){ - double t = (x - segCourant.x) / (x - segCourant.x + segCourant.xLong); - double yLigne = segCourant.y + segCourant.yLong * t; + public void ResterDansLigne(Ligne li){ double collisionHautPos = y + rayon; double collisionBasPos = y - rayon; - return yLigne < collisionBasPos || yLigne > collisionHautPos; + double yLigne = li.GetHauteurPointCercle(); + + if (yLigne < collisionBasPos){ + y = yLigne + rayon; + if (vitesse > 0) vitesse = 0; + } + if (yLigne > collisionHautPos){ + y = yLigne - rayon; + if (vitesse < 0) vitesse = 0; + } } - - + public boolean VerifierCollision(Ligne li){ + double collisionHautPos = y + rayon; + double collisionBasPos = y - rayon; + + double yLigne = li.GetHauteurPointCercle(); + + return yLigne <= collisionBasPos || yLigne >= collisionHautPos; + } } diff --git a/src/Jeu.java b/src/Jeu.java index 94abaf6..e53d2eb 100644 --- a/src/Jeu.java +++ b/src/Jeu.java @@ -149,7 +149,10 @@ public class Jeu implements KeyListener, ActionListener{ public void actionPerformed(ActionEvent e) { ecran.traiterBoucleAnimation(); - if (demiCercleArriere.EnCollision(lili.SegCourant)) + demiCercleArriere.ResterDansLigne(lili); + demiCercleAvant.ResterDansLigne(lili); + + if (demiCercleArriere.VerifierCollision(lili)) RecommencerPartie(); } diff --git a/src/Ligne.java b/src/Ligne.java index 57bbf07..93fe37a 100644 --- a/src/Ligne.java +++ b/src/Ligne.java @@ -52,12 +52,12 @@ public class Ligne extends ObjetGraphique { for (Segment s : listeSegments) { s.Afficher(g); if (SegCourant == null) { - if (xCercle < s.x + s.xLong && xCercle > s.x) { + if (xCercle <= s.x + s.xLong && xCercle >= s.x) { SegCourant = s; } } else { if ((SegCourant.x + SegCourant.xLong) < xCercle) { - if (s.x <= xCercle && (s.xLong) >= xCercle) { + if (s.x <= xCercle && (s.x + s.xLong) >= xCercle) { SegCourant = s; } } @@ -72,4 +72,10 @@ public class Ligne extends ObjetGraphique { s.x -= 10; } } + + public double GetHauteurPointCercle(){ + double t = (400 - SegCourant.x) / SegCourant.xLong; + + return SegCourant.y + SegCourant.yLong * t; + } } \ No newline at end of file