-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.java
More file actions
148 lines (128 loc) · 4.13 KB
/
Particle.java
File metadata and controls
148 lines (128 loc) · 4.13 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package funStuff;
import java.util.List;
import java.awt.*;
public class Particle {
double x = 0;
double y = 0;
double xVel = 0;
double yVel = 0;
double gForce;
double yForce;
double bForce;
double rForce;
double friction = .6;
double repulsiveForce = .5;
char type;
int forceConstant = 500;
public Particle (double gForce, double yForce, double bForce, double rForce, char type) {
this.gForce = gForce;
this.yForce = yForce;
this.bForce = bForce;
this.rForce = rForce;
this.type = type;
}
public static Particle createRandomParticle(double gForce, double yForce, double bForce, double rForce, char type) {
Particle p = new Particle(gForce, yForce, bForce, rForce, type);
p.x = Math.random() * 1400+50;
p.y = Math.random() * 750+50;
return p;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public char getType() {
return type;
}
public double getDistance(Particle particle) {
return Math.sqrt((particle.getX()-this.getX())*(particle.getX()-this.getX())+(particle.getY()-this.getY())*(particle.getY()-this.getY()));
}
public double getXDistance(Particle particle) {
return x-particle.getX();
}
public double getYDistance(Particle particle) {
return y-particle.getY();
}
public boolean isTouching(Particle particle) {
if(getDistance(particle) < 2) {
return true;
}
return false;
}
public double getNewXVel (List<Particle> particles) {
long newXVel = 0;
for(Particle particle : particles) {
if(!this.isTouching(particle)) {
if(particle.getType() == 'r') {
newXVel += ((rForce * forceConstant)/(getDistance(particle)))*(getXDistance(particle)/getDistance(particle));
} else if(particle.getType() == 'y') {
newXVel += ((yForce * forceConstant)/(getDistance(particle)))*(getXDistance(particle)/getDistance(particle));
} else if(particle.getType() == 'g') {
newXVel += ((gForce * forceConstant)/(getDistance(particle)))*(getXDistance(particle)/getDistance(particle));
} else {
newXVel += ((bForce * forceConstant)/(getDistance(particle)))*(getXDistance(particle)/getDistance(particle));
}
} else {
if (particle.getX() > x) {
newXVel -= repulsiveForce;
} else {
newXVel += repulsiveForce;
}
}
}
return newXVel+xVel;
}
public double getNewYVel (List<Particle> particles) {
long newYVel = 0;
for(Particle particle : particles) {
if(!this.isTouching(particle)) {
if(particle.getType() == 'r') {
newYVel += ((rForce * forceConstant)/(getDistance(particle)))*(getYDistance(particle)/getDistance(particle));
} else if(particle.getType() == 'y') {
newYVel += ((yForce*forceConstant)/(getDistance(particle)))*(getYDistance(particle)/getDistance(particle));
} else if(particle.getType() == 'g') {
newYVel += ((gForce*forceConstant)/(getDistance(particle)))*(getYDistance(particle)/getDistance(particle));
} else {
newYVel += ((bForce*forceConstant)/(getDistance(particle)))*(getYDistance(particle)/getDistance(particle));
}
} else {
if (particle.getY() > y) {
newYVel -= repulsiveForce;
} else {
newYVel += repulsiveForce;
}
}
}
return newYVel+yVel;
}
public void moveX(List<Particle> particles) {
xVel = getNewXVel(particles);
xVel *= friction;
if ((x >= 50 && x <= 1450) || (x < 50 && xVel > 0) || (x > 1450 && xVel < 0)) {
x += xVel;
}
x = Math.max(50, Math.min(x, 1400));
}
public void moveY(List<Particle> particles) {
yVel = getNewYVel(particles);
yVel *= friction;
if ((y >= 50 && y <= 750) || (y < 50 && yVel > 0) || (y > 750 && yVel < 0)) {
y += yVel;
}
y = Math.max(50, Math.min(y, 750));
}
public void draw(Graphics g) {
if(type == 'r') {
g.setColor(Color.RED);
} else if (type == 'g') {
g.setColor(Color.GREEN);
} else if (type == 'y') {
g.setColor(Color.YELLOW);
} else if (type == 'b') {
g.setColor(Color.BLUE);
}
g.fillOval((int) x, (int) y, 4, 4);
}
}