This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.java
165 lines (151 loc) · 3.32 KB
/
Particle.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class Particle {
private float x;
private float y;
private float xvel;
private float yvel;
private float xacc;
private float yacc;
private int size;
public Rectangle interactor;
private Random rand;
private Color myColor;
private boolean alive;
private static int isAttracted;
public Particle(int x, int y,int size, boolean life) {
interactor = new Rectangle(x, y, size+3, size+3);
this.size = size;
rand = new Random();
this.x = x;
this.y = y;
this.xvel = 0;
this.yvel = 0;
this.xacc = 0;
this.yacc = 0;
alive = life;
isAttracted = 0;
myColor = new Color((int)(y*3)%255, (int)x%255, 255, 255);
}
public void update(int j, int k) {
if(alive) {
interactor.x = (int)x;
interactor.y = (int)y;
int d = Functions.distance((int)x, (int)y, j, k);
x += 0.5 * xvel;
y += 0.5 * yvel;
if(isAttracted==1) {
xvel += rand.nextInt(3)*(j-x)/(d+1);
yvel += rand.nextInt(3)*(k-y)/(d+1);
}
if(isAttracted==-1) {
xvel -= rand.nextInt(3)*(j-x)/(d+1);
yvel -= rand.nextInt(3)*(k-y)/(d+1);
}
xvel /= 1.02;
yvel /= 1.02;
if(xvel > 35) xvel = 35;
if(xvel < -35) xvel = -35;
if(yvel > 35) yvel = 35;
if(yvel < -35) yvel = -35;
respectBounds();
}
}
public void respectBounds() {
if(x < 10) {
xvel = -xvel/2;
x = 11;
}
if(x > 780) {
xvel = -xvel/2;
x = 780;
}
if(y < 10) {
yvel = -yvel/2;
y = 11;
}
if(y > 760) {
yvel = -yvel/2;
y = 760;
}
}
public void interactOnRect(Rectangle a, Graphics g) {
//Rectangle a = new Rectangle(b.x -2, b.y -2, b.width +4, b.height +4);
Rectangle judge = interactor.intersection(a);
if(interactor.intersects(a)) {
if(judge.height > judge.width) {
if(interactor.x > a.getCenterX()){
xvel = -xvel/2;
x+=2;
}
if(interactor.x < a.getCenterX()){
xvel = -xvel/2;
x -=2;
}
}else if(judge.height < judge.width){
if(interactor.y > a.getCenterY()){
yvel = -yvel/2;
y += 2;
}
if(interactor.y < a.getCenterY()){
yvel = -yvel/2;
y -= 2;
}
}else if(judge.height == judge.width) {
double case1 = Math.abs(interactor.x - a.getCenterX());
double case2 = Math.abs(interactor.y - a.getCenterY());
if(case2 > case1) {
if(interactor.x > a.getCenterX()){
xvel = -xvel/2;
x = a.x + a.width + judge.width/100;
}
if(interactor.x < a.getCenterX()){
xvel = -xvel/2;
x = a.x - interactor.width - judge.width/100;
}
} else {
if(interactor.y > a.getCenterY()){
yvel = -yvel/2;
y = a.y + a.height + judge.height/100;
}
if(interactor.y < a.getCenterY()){
yvel = -yvel/2;
y = a.y - interactor.height - judge.height/100;
}
}
}
}
}
public int getX() {
return (int)x;
}
public int getY() {
return (int)y;
}
public int getXvel() {
return (int)xvel;
}
public int getYvel() {
return (int)yvel;
}
public int getXacc() {
return (int)xacc;
}
public int getYacc() {
return (int)yacc;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public static void setAttraction(int a) {
isAttracted = a;
}
public Color getColor() {
return this.myColor;
}
}