collisions, cercle reste dans ligne

This commit is contained in:
MOISOIU Stefan-Mihai
2026-02-23 10:07:29 +01:00
parent b8270ede9d
commit 9e18b47a66
3 changed files with 31 additions and 11 deletions

View File

@@ -3,7 +3,6 @@ package linea;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Arc2D; import java.awt.geom.Arc2D;
public class Cercle extends ObjetGraphique{ // il s'agit plutôt d'arcs de cercle 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; 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 collisionHautPos = y + rayon;
double collisionBasPos = 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;
}
} }

View File

@@ -149,7 +149,10 @@ public class Jeu implements KeyListener, ActionListener{
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
ecran.traiterBoucleAnimation(); ecran.traiterBoucleAnimation();
if (demiCercleArriere.EnCollision(lili.SegCourant)) demiCercleArriere.ResterDansLigne(lili);
demiCercleAvant.ResterDansLigne(lili);
if (demiCercleArriere.VerifierCollision(lili))
RecommencerPartie(); RecommencerPartie();
} }

View File

@@ -52,12 +52,12 @@ public class Ligne extends ObjetGraphique {
for (Segment s : listeSegments) { for (Segment s : listeSegments) {
s.Afficher(g); s.Afficher(g);
if (SegCourant == null) { if (SegCourant == null) {
if (xCercle < s.x + s.xLong && xCercle > s.x) { if (xCercle <= s.x + s.xLong && xCercle >= s.x) {
SegCourant = s; SegCourant = s;
} }
} else { } else {
if ((SegCourant.x + SegCourant.xLong) < xCercle) { if ((SegCourant.x + SegCourant.xLong) < xCercle) {
if (s.x <= xCercle && (s.xLong) >= xCercle) { if (s.x <= xCercle && (s.x + s.xLong) >= xCercle) {
SegCourant = s; SegCourant = s;
} }
} }
@@ -72,4 +72,10 @@ public class Ligne extends ObjetGraphique {
s.x -= 10; s.x -= 10;
} }
} }
public double GetHauteurPointCercle(){
double t = (400 - SegCourant.x) / SegCourant.xLong;
return SegCourant.y + SegCourant.yLong * t;
}
} }