Skip to content

Commit f212b46

Browse files
committed
updated collision with paddle mechanics
1 parent fbbebe5 commit f212b46

File tree

6 files changed

+67
-41
lines changed

6 files changed

+67
-41
lines changed

src/main/java/com/gustavolr/engine/entity/Entity.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public abstract class Entity {
88
protected Vector position;
99
protected int width;
1010
protected int height;
11+
int collision_timer = 0;
12+
int max_timer = 60;
1113

1214
public Entity(Vector position, int width, int height) {
1315
this.position = position;
@@ -35,18 +37,28 @@ public void setHeight(int height) {
3537
}
3638

3739
public void update() {
38-
40+
if(collision_timer < max_timer) {
41+
collision_timer++;
42+
}
43+
System.out.println("collision " + collision_timer);
3944
}
4045

4146
public void render(Graphics g) {
4247

4348
}
4449

45-
public boolean isColidding(Entity e1) {
46-
50+
public boolean isColidding(Entity e1) {
51+
if (collision_timer < max_timer) {
52+
return false;
53+
}
54+
4755
Rectangle e1Mask = new Rectangle(e1.position.x, e1.position.y, e1.width, e1.height);
4856
Rectangle e2Mask = new Rectangle(this.position.x, this.position.y, this.width, this.height);
4957

50-
return e1Mask.intersects(e2Mask);
58+
boolean response = e1Mask.intersects(e2Mask);
59+
if(response)
60+
collision_timer = 0;
61+
62+
return response;
5163
}
5264
}

src/main/java/com/gustavolr/engine/window/GameWindowConstants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
public final class GameWindowConstants {
66

7-
public static final int DEFAULT_WINDOW_WIDTH = 260;
8-
public static final int DEFAULT_WINDOW_HEIGHT = 140;
7+
public static final int DEFAULT_WINDOW_WIDTH = 320;
8+
public static final int DEFAULT_WINDOW_HEIGHT = 180;
99
public static final int DEFAULT_WINDOW_SCALE = 4;
1010

1111
public static final Color BACKGROUND_COLOR = new Color(28, 28, 28);

src/main/java/com/gustavolr/game_entities/Ball.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,39 @@
99

1010
public class Ball extends Entity {
1111

12-
private int speed;
1312
private Color color = Color.WHITE;
1413

15-
float dx = 1;
16-
float dy = 1;
14+
public double dx,dy,speed;
1715

1816
public Ball(Vector position, int width, int height) {
1917
super(position, width, height);
20-
speed = 1;
18+
speed = 3;
19+
dx = 1;
20+
dy = 1;
2121
}
2222

23-
public void invertDX() {
24-
dx *= -1;
25-
}
26-
27-
public void increaseSpeed() {
28-
this.speed += 1;
29-
}
23+
@Override
24+
public void update() {
3025

31-
public int getSpeed() {
32-
return speed;
33-
}
26+
if(position.y < 0 || position.y > GameWindow.getWindowHeight() - this.height)
27+
dy *= -1;
3428

35-
public void setSpeed(int speed) {
36-
this.speed = speed;
29+
position.x += dx;
30+
position.y += dy;
31+
super.update();
3732
}
3833

39-
@Override
40-
public void update() {
34+
public void calculateAngleAfterColisionWith(Entity paddle) {
35+
double relativeInsersectY = (paddle.getPosition().y + (paddle.getHeight()/2.0)) - (this.getPosition().y + (this.getHeight() / 2.0));
36+
double normalIntersectY = relativeInsersectY / (paddle.getHeight() / 2.0);
37+
double theta = Math.toRadians(normalIntersectY * 45);
4138

42-
position.x += dx * speed;
43-
position.y += dy * speed;
39+
double newDx = Math.abs(Math.cos(theta)) * this.speed;
40+
double newDy = (-Math.sin(theta)) * this.speed;
4441

45-
if(position.y < 0 || position.y > GameWindow.getWindowHeight() - this.height)
46-
dy *= -1;
42+
double oldSign = Math.signum(this.dx);
43+
this.dx = newDx * (-1.0 * oldSign);
44+
this.dy = newDy;
4745
}
4846

4947
@Override

src/main/java/com/gustavolr/game_entities/Enemy.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.gustavolr.engine.entity.Entity;
77
import com.gustavolr.engine.entity.Vector;
8+
import com.gustavolr.engine.window.GameWindow;
89

910
public class Enemy extends Entity {
1011

@@ -19,15 +20,17 @@ public Enemy(Vector position, int width, int height) {
1920
public void render(Graphics g) {
2021
g.setColor(this.color);
2122
g.fillRect(this.position.x, this.position.y, this.width, this.height);
23+
super.update();
2224
}
2325

2426
public void ballMoved(Vector ballPosition) {
2527

2628
Vector direction = new Vector();
2729

28-
if (ballPosition.y < position.y) {
30+
if (ballPosition.y < position.y && position.y > 0) {
2931
direction.y -= speed;
30-
} else {
32+
33+
} else if (ballPosition.y > position.y && position.y < GameWindow.getWindowHeight() - this.height ) {
3134
direction.y += speed;
3235
}
3336

src/main/java/com/gustavolr/game_entities/Player.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ else if (GameInput.isKeyPressed(KeyEvent.VK_DOWN) && position.y < GameWindow.get
3131
}
3232

3333
this.position = this.position.add(direction);
34+
super.update();
3435
}
3536

3637
@Override

src/main/java/com/gustavolr/game_scenes/MainScene.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public final class MainScene implements Scene {
3131
private final List<Entity> entities;
3232

3333
public MainScene() {
34-
p = new Player(PLAYER_START_POSITION.clone(), 5, 25);
35-
e = new Enemy(ENEMY_START_POSITION.clone(), 5, 25);
34+
p = new Player(PLAYER_START_POSITION.clone(), 4, 35);
35+
e = new Enemy(ENEMY_START_POSITION.clone(), 4, 35);
3636
b = new Ball(BALL_START_POSITION.clone(),7,7);
3737

3838
this.enemyScore = 0;
@@ -59,6 +59,9 @@ public void update() {
5959
e.update();
6060
}
6161

62+
System.out.println("b.dx = " + b.dx);
63+
System.out.println("b.dy = " + b.dy);
64+
6265
e.ballMoved(b.getPosition());
6366

6467
checkIfBallCollidedWithPaddles();
@@ -67,17 +70,26 @@ public void update() {
6770
}
6871

6972
public void checkIfBallCollidedWithPaddles() {
70-
if (b.isColidding(e)) {
71-
b.invertDX();
72-
} else if(b.isColidding(p)) {
73-
b.invertDX();
73+
if (b.isColidding(e) || b.isColidding(p)) {
74+
75+
System.out.println("colisao com paddle");
76+
Entity collidedEntity;
77+
78+
if(b.isColidding(e)) {
79+
collidedEntity = e;
80+
} else {
81+
collidedEntity = p;
82+
}
83+
84+
b.calculateAngleAfterColisionWith(collidedEntity);
7485
}
7586
}
7687

7788
public void checkForScoring() {
7889
Vector ballPosition = b.getPosition();
90+
int width = b.getWidth();
7991

80-
if (enemyScored(ballPosition.x)) {
92+
if (enemyScored(ballPosition.x, width)) {
8193
enemyScore += 1;
8294
enemyScoreText.setLabel(Integer.toString(enemyScore));
8395
updateBallPosition();
@@ -93,14 +105,14 @@ public boolean playerScored(int ball_x) {
93105
return ball_x > GameWindow.getWindowWidth();
94106
}
95107

96-
public boolean enemyScored(int ball_x) {
97-
return ball_x < 0;
108+
public boolean enemyScored(int ball_x, int ball_width) {
109+
return ball_x < -ball_width;
98110
}
99111

100112
public void updateBallPosition() {
101113
b.setPosition(BALL_START_POSITION.clone());
102-
b.setSpeed(1);
103-
b.invertDX();
114+
b.dx = 1;
115+
b.dy = 1;
104116
}
105117

106118
@Override

0 commit comments

Comments
 (0)