Skip to content
Snippets Groups Projects
Commit 0a57b4e1 authored by bosskkev's avatar bosskkev
Browse files

Nouvelle physique pour les rebonds des balles sur les raquettes

parent 6800db13
No related branches found
No related tags found
No related merge requests found
Pipeline #25146 failed
...@@ -22,7 +22,7 @@ public class PhysicEngine implements Engine { ...@@ -22,7 +22,7 @@ public class PhysicEngine implements Engine {
Coordinates2D previousPosition = pongObjectEntityHashMap.get(pongObject).getPosition(); Coordinates2D previousPosition = pongObjectEntityHashMap.get(pongObject).getPosition();
pongObjectEntityHashMap.get(pongObject).updatePosition();// Updating of the associated movable position pongObjectEntityHashMap.get(pongObject).updatePosition();// Updating of the associated movable position
if(pongObject.getName().equals("Ball")){ if(pongObject.getName().equals("Ball")){
System.out.println("Ball position: " + pongObjectEntityHashMap.get(pongObject).getPosition().getX() + " ; " + pongObjectEntityHashMap.get(pongObject).getPosition().getY()); //System.out.println("Ball position: " + pongObjectEntityHashMap.get(pongObject).getPosition().getX() + " ; " + pongObjectEntityHashMap.get(pongObject).getPosition().getY());
//System.out.println("Ball init position: " + pongObjectEntityHashMap.get(pongObject).getInitPosition()); //System.out.println("Ball init position: " + pongObjectEntityHashMap.get(pongObject).getInitPosition());
for (PongObject po2: pongObjectEntityHashMap.keySet() for (PongObject po2: pongObjectEntityHashMap.keySet()
) { ) {
...@@ -34,12 +34,13 @@ public class PhysicEngine implements Engine { ...@@ -34,12 +34,13 @@ public class PhysicEngine implements Engine {
Kernel.isCollision = true; Kernel.isCollision = true;
System.out.println("Collision " + i); System.out.println("Collision " + i);
pongObjectEntityHashMap.get(pongObject).setPosition(previousPosition); pongObjectEntityHashMap.get(pongObject).setPosition(previousPosition);
if(i % 2 == 0){ if(i % 10 == 0){
pongObjectEntityHashMap.get(pongObject).getSpeed().add(new Coordinates2D(1, 1)); pongObjectEntityHashMap.get(pongObject).getSpeed().mul(2);
} }
pongObject.setSpeed(pongObjectEntityHashMap.get(pongObject).getSpeed());
pongObjectEntityHashMap.get(pongObject).getSpeed().mul(-1); //pongObjectEntityHashMap.get(pongObject).getSpeed().mul(-1);
//TODO: implements pong object collision physics pongObject.updateSpeed(po2.doItHitHalf(pongObject.getPosition()), po2.isLeftRacket());
pongObjectEntityHashMap.get(pongObject).setSpeed(pongObject.getSpeed());
pongObjectEntityHashMap.get(pongObject).updatePosition(); pongObjectEntityHashMap.get(pongObject).updatePosition();
} }
......
...@@ -35,10 +35,10 @@ public class PongApp { ...@@ -35,10 +35,10 @@ public class PongApp {
int racketHeight = 120; int racketHeight = 120;
int ballWidth = 32; int ballWidth = 32;
PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, height/2 - racketHeight/2), racketWidth, racketHeight); PongRacket leftRacket = new PongRacket("Left racket", "src/main/resources/pong/raquette.png", new Coordinates2D(widthDiff + 5, height/2 - racketHeight/2), racketWidth, racketHeight, true);
components.add(leftRacket); components.add(leftRacket);
PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, height/2 - racketHeight/2), racketWidth, racketHeight); PongRacket rightRacket = new PongRacket("Right racket", "src/main/resources/pong/raquette.png", new Coordinates2D(rectWidth + widthDiff - 5 - racketWidth, height/2 - racketHeight/2), racketWidth, racketHeight, false);
components.add(rightRacket); components.add(rightRacket);
PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth); PongBall pongBall = new PongBall("Ball", "src/main/resources/pong/ball.png", new Coordinates2D(width/2, height/2), ballWidth, ballWidth);
......
...@@ -10,6 +10,8 @@ public class PongBall implements PongObject { ...@@ -10,6 +10,8 @@ public class PongBall implements PongObject {
private final int width; private final int width;
private final int height; private final int height;
private Coordinates2D position; private Coordinates2D position;
private Coordinates2D speed;
private Coordinates2D initPosition; private Coordinates2D initPosition;
private final Image image; private final Image image;
...@@ -27,6 +29,11 @@ public class PongBall implements PongObject { ...@@ -27,6 +29,11 @@ public class PongBall implements PongObject {
return this.position; return this.position;
} }
@Override
public Coordinates2D getSpeed() {
return this.speed;
}
@Override @Override
public Image getImage() { public Image getImage() {
...@@ -53,6 +60,50 @@ public class PongBall implements PongObject { ...@@ -53,6 +60,50 @@ public class PongBall implements PongObject {
this.position = position; this.position = position;
} }
@Override
public void setSpeed(Coordinates2D speed) {
this.speed = speed;
}
/**
* Update the speed of the ball when it hits a paddle.
* If the ball hits the upper half of the paddle, the ball will go up
* If the ball hits the lower half of the paddle, the ball will go down
* @param isUpperHalf
*/
@Override
public void updateSpeed(boolean isUpperHalf, boolean isLeftRacket){
System.out.println("updateSpeed");
if(!isLeftRacket){
System.out.println("isLeftRacket");
if(isUpperHalf){
if(this.speed.getY() > 0){
this.speed.setY(this.speed.getY() * -1);
}
}else{
if (this.speed.getY() < 0){
this.speed.setY(this.speed.getY() * -1);
}
}
if (this.speed.getX() > 0){
this.speed.setX(this.speed.getX() * -1);
}
} else {
if(isUpperHalf){
if(this.speed.getY() > 0){
this.speed.setY(this.speed.getY() * -1);
}
}else{
if (this.speed.getY() < 0){
this.speed.setY(this.speed.getY() * -1);
}
}
if (this.speed.getX() < 0){
this.speed.setX(this.speed.getX() * -1);
}
}
}
@Override @Override
public String getName() { public String getName() {
return name; return name;
...@@ -62,4 +113,14 @@ public class PongBall implements PongObject { ...@@ -62,4 +113,14 @@ public class PongBall implements PongObject {
public Coordinates2D getInitPosition() { public Coordinates2D getInitPosition() {
return this.initPosition; return this.initPosition;
} }
@Override
public boolean doItHitHalf(Coordinates2D ballPosition) {
return false;
}
@Override
public boolean isLeftRacket() {
return false;
}
} }
...@@ -6,13 +6,25 @@ import java.awt.*; ...@@ -6,13 +6,25 @@ import java.awt.*;
public interface PongObject { public interface PongObject {
public Coordinates2D getPosition(); public Coordinates2D getPosition();
public Coordinates2D getSpeed();
public Image getImage(); public Image getImage();
public int getWidth(); public int getWidth();
public int getHeight(); public int getHeight();
public int getResistance(); public int getResistance();
public void setPosition(Coordinates2D position); public void setPosition(Coordinates2D position);
public void setSpeed(Coordinates2D speed);
void updateSpeed(boolean isUpperHalf, boolean isLeftRacket);
public String getName(); public String getName();
Coordinates2D getInitPosition(); Coordinates2D getInitPosition();
boolean doItHitHalf(Coordinates2D ballPosition);
boolean isLeftRacket();
} }
...@@ -12,13 +12,18 @@ public class PongRacket implements PongObject { ...@@ -12,13 +12,18 @@ public class PongRacket implements PongObject {
private Coordinates2D position; private Coordinates2D position;
private final Coordinates2D initPosition; private final Coordinates2D initPosition;
private final Image image; private final Image image;
public PongRacket(String name, String imagePath, Coordinates2D position, int width, int height) { private Coordinates2D speed;
private boolean isLeftRacket;
public PongRacket(String name, String imagePath, Coordinates2D position, int width, int height, boolean isLeftRacket) {
this.name = name; this.name = name;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.image = Toolkit.getDefaultToolkit().getImage(imagePath); this.image = Toolkit.getDefaultToolkit().getImage(imagePath);
this.position = position; this.position = position;
this.initPosition = position; this.initPosition = position;
this.isLeftRacket = isLeftRacket;
} }
@Override @Override
...@@ -26,6 +31,11 @@ public class PongRacket implements PongObject { ...@@ -26,6 +31,11 @@ public class PongRacket implements PongObject {
return position; return position;
} }
@Override
public Coordinates2D getSpeed() {
return this.speed;
}
@Override @Override
public Image getImage() { public Image getImage() {
return this.image; return this.image;
...@@ -51,6 +61,16 @@ public class PongRacket implements PongObject { ...@@ -51,6 +61,16 @@ public class PongRacket implements PongObject {
this.position = position; this.position = position;
} }
@Override
public void setSpeed(Coordinates2D speed) {
this.speed = speed;
}
@Override
public void updateSpeed(boolean isUpperHalf, boolean isLeftRacket) {
}
@Override @Override
public String getName() { public String getName() {
return name; return name;
...@@ -60,4 +80,18 @@ public class PongRacket implements PongObject { ...@@ -60,4 +80,18 @@ public class PongRacket implements PongObject {
public Coordinates2D getInitPosition() { public Coordinates2D getInitPosition() {
return initPosition; return initPosition;
} }
/**
* @param ballPosition
* @return true if the ball hit the upper half of the racket
*/
@Override
public boolean doItHitHalf(Coordinates2D ballPosition) {
return ballPosition.getY() < this.position.getY() + this.height / 2;
}
@Override
public boolean isLeftRacket() {
return isLeftRacket;
}
} }
package engine;
import engine.physic.Coordinates2D;
import engine.physic.Movable;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PongObjectTest {
@Test
public void testPositionUpdating() {
Movable newMovable = new Movable(1, new Coordinates2D(0, 0), new Coordinates2D(0, 0), new Coordinates2D(0, 0), 0);
//With constant acceleration (not (0, 0))
int delta = 1;
while(delta <= 5){
if(delta % 2 == 0) {
newMovable.updatePosition(new Coordinates2D(1, 1));
//System.out.println("Position: " + newHero.getPosition().getRow() + ", " + newHero.getPosition().getCol());
}
delta++;
}
//The position supposed to be (3, 3)
assertEquals(3, newMovable.getPosition().getX());
assertEquals(3, newMovable.getPosition().getY());
//With variable acceleration (croissant)
delta = 1;
while(delta <= 5){
if(delta % 2 == 0) {
newMovable.updatePosition(new Coordinates2D(delta, delta));
//System.out.println("Position: " + newHero.getPosition().getRow() + ", " + newHero.getPosition().getCol());
}
delta++;
}
//The position supposed to be (15, 15)
assertEquals( 15, newMovable.getPosition().getX());
assertEquals( 15, newMovable.getPosition().getY());
//With variable acceleration (decroissant)
delta = 5;
while(delta >= 1){
if(delta % 2 == 0) {
newMovable.updatePosition(new Coordinates2D(delta, delta));
//System.out.println("Position: " + newHero.getPosition().getRow() + ", " + newHero.getPosition().getCol());
}
delta--;
}
//The position supposed to be (41, 41)
assertEquals( 41, newMovable.getPosition().getX());
assertEquals( 41, newMovable.getPosition().getY());
//With negative acceleration
delta = 1;
while(delta <= 5){
if(delta % 2 == 0) {
newMovable.updatePosition(new Coordinates2D(-delta, -delta));
//System.out.println("Position: " + newHero.getPosition().getRow() + ", " + newHero.getPosition().getCol());
}
delta++;
}
//The position supposed to be (61, 61)
assertEquals( 61, newMovable.getPosition().getX());
assertEquals( 61, newMovable.getPosition().getY());
//La vitesse actuelle est un vecteur (8, 8)
// Test pour vérifier si la vitesse est constante quand l'accélération est nulle
delta = 1;
while (delta <= 5){
if (delta % 2 == 0){
newMovable.updatePosition(new Coordinates2D(0, 0));
}
delta++;
}
assertEquals(8,newMovable.getSpeed().getX());
assertEquals(8,newMovable.getSpeed().getY());
assertEquals(77,newMovable.getPosition().getY()); // La position est censée être (77, 77)
assertEquals(77,newMovable.getPosition().getX());
}
@Test
public void testHalt(){
Movable newMovable = new Movable(1, new Coordinates2D(0, 0), new Coordinates2D(0, 0), new Coordinates2D(0, 0), 0);
int delta = 1;
newMovable.updatePosition(new Coordinates2D(1, 1));
while(delta <= 5){
if(delta % 2 == 0) {
if(delta == 2){
newMovable.halt(); //With a null speed, the position is not modified
}
newMovable.updatePosition(new Coordinates2D(0, 0)); // Without acceleration the speed is not modified but the position is
//System.out.println("Position: " + newHero.getPosition().getRow() + ", " + newHero.getPosition().getCol());
}
delta++;
}
//The position supposed to be (1, 1) because the hero stopped at (1, 1)
assertEquals( 3, newMovable.getPosition().getX());
assertEquals( 3, newMovable.getPosition().getY());
}
@Test
public void testGraphiqueInterface(){
}
}
\ No newline at end of file
package pong;
import engine.physic.Coordinates2D;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PongBallTest {
/**
* Test if the speed of the ball is updated correctly when the ball hits the upper half of the paddle
* If we hit the upper half of the paddle, the ball should go up (-x, -y) and
* if we hit the lower half of the paddle, the ball should go down (-x, y)
*/
@Test
void testUpdateSpeedUpperHalfHit() {
PongBall pongBall = new PongBall("ball", "src/main/resources/ball.png", new Coordinates2D(0, 0), 10, 10);
pongBall.setSpeed(new Coordinates2D(1, 1));
pongBall.updateSpeed(true, true);
assertEquals(-1, pongBall.getSpeed().getY());
assertEquals(-1 , pongBall.getSpeed().getX());
pongBall.setSpeed(new Coordinates2D(1, -1));
pongBall.updateSpeed(false, true);
assertEquals(-1, pongBall.getSpeed().getX());
assertEquals(1 , pongBall.getSpeed().getY());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment