This repository was archived by the owner on Sep 17, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBall.java
More file actions
132 lines (107 loc) · 3.39 KB
/
Copy pathBall.java
File metadata and controls
132 lines (107 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package Gprocessing.breakout;
import Gprocessing.ecs.GameObject;
import Gprocessing.ecs.SpriteRenderer;
import Gprocessing.graphics.Color;
import Gprocessing.physics.Transform;
import Gprocessing.physics.Vector2;
import Gprocessing.util.Utils;
import static Gprocessing.breakout.Breakout.paddle;
public class Ball {
private GameObject ballGo;
boolean isAlive = true;
private Vector2 velocity = new Vector2(Utils.random(-3, 3), -3);
private boolean firstLoop = true;
public Ball (Transform t, Color c) {
this.ballGo = new GameObject(t);
ballGo.addComponent(new SpriteRenderer("src/assets/images/ball.png"));
ballGo.getComponent(SpriteRenderer.class).setColor(c);
}
public Transform getTransform () {
return ballGo.getTransform();
}
public void setTransform (Transform t) {
ballGo.setTransform(t);
}
public Color getColor () {
return ballGo.getComponent(SpriteRenderer.class).getColor();
}
public void setColor (Color c) {
ballGo.getComponent(SpriteRenderer.class).setColor(c);
}
public void update () {
if (firstLoop) {
paddle.getTransform().addX(paddle.getTransform().scale.x/2);
paddle.getTransform().addX(-30);
firstLoop = false;
}
getTransform().addX(velocity.x);
getTransform().addY(velocity.y);
if (circleRectCollision(new Transform(-10, 0, 1280, 10))) {
flipVelocityY(); // Ceiling Collision
}
if (circleRectCollision(new Transform(0, 674, 1280, 10))) {
isAlive = false;
}
if (circleRectCollision(new Transform(-10, 0, 10, 674))) {
flipVelocityX(); // Left side
}
if (circleRectCollision(new Transform(1280, 0, 10, 674))) {
flipVelocityX(); // Right side
}
}
public void setVelocity (float x, float y) {
velocity.x = x;
velocity.y = y;
}
public void flipVelocityX () {
velocity.x *= -1;
}
public void flipVelocityY () {
velocity.y *= -1;
}
public void setVelocityX (float a) {
velocity.x = a;
}
public Vector2 getVelocity () {
return this.velocity;
}
public boolean circleRectCollision (Transform t) {
/**
checks for a collision between a the radius surrounding a point (a circle) and a rectangle
*/
float circleX = getTransform().position.x + getTransform().scale.x/2;
float circleY = getTransform().position.y + getTransform().scale.y/2;
float radius = getTransform().scale.x/2;
float rectX = t.position.x;
float rectY = t.position.y;
float rectWidth = t.scale.x;
float rectHeight = t.scale.y;
// The closest edge on the rect to the circle
float closestEdgeX = circleX;
float closestEdgeY = circleY;
// Find the closest edge
if (circleX < rectX) {
// left edge
closestEdgeX = rectX;
} else if (circleX > rectX + rectWidth) {
// right edge
closestEdgeX = rectX + rectWidth;
}
if (circleY < rectY) {
// top edge
closestEdgeY = rectY;
} else if (circleY > rectY + rectHeight) {
// bottom edge
closestEdgeY = rectY + rectHeight;
}
// Get distance from closest edge
float distanceX = circleX - closestEdgeX;
float distanceY = circleY - closestEdgeY;
float distance = (float) Math.sqrt((distanceX * distanceX) + (distanceY * distanceY));
// if the distance from the center of the circle to the closest edge of the rect is less than the radius, collision!
if (distance <= radius) {
return true;
}
return false;
}
}