-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystem.pde
More file actions
153 lines (132 loc) · 4.59 KB
/
Copy pathParticleSystem.pde
File metadata and controls
153 lines (132 loc) · 4.59 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
149
150
151
152
153
class ParticleSystem
{
ArrayList<Particle> _particles;
int _n;
// Kd : Rozamiento entre bolas y superficie mesa
// Linealmente proporcional a la velocidad de las bolas
// Cr1: Entre 0 y 1
// Pérdida de energía por cada colisión de cada bola con las bandas de la mesa
// Cr2: Entre 0 y 1
// Pérdida de eneergía entre colisión de bolas
float Kd = 0.09;
float Cr1 = 0.95;
float Cr2 = 0.95;
//Argumentos añadidos por nosotras
ParticleSystem()
{
_particles = new ArrayList<Particle>();
_n = 0;
Kd = 0.09;
}
void addParticle(int id, PVector initPos, PVector initVel, float mass, float radius)
{
Particle p = new Particle(this, id, initPos, initVel, mass, radius);
_particles.add(p);
_n++;
}
void restart()
{
restartSimulation();
}
int getNumParticles()
{
return _n;
}
ArrayList<Particle> getParticleArray()
{
return _particles;
}
void run()
{
for (int i = _n - 1; i >= 0; i--)
{
Particle p = _particles.get(i);
p.update();
}
}
void computeCollisions(ArrayList<PlaneSection> planes, boolean computeParticleCollision)
{
if (computeParticleCollision)
for(int i = 0; i < getNumParticles(); i++)
{
_particles.get(i).planeCollision(planes);
for(int j = 0; j < getNumParticles(); j++)
if (_particles.get(i).getId() != _particles.get(j).getId())
_particles.get(i).particleCollisionVelocityModel(_particles.get(j));
}
}
void display()
{
for (int i = _n - 1; i >= 0; i--)
{
Particle p = _particles.get(i);
p.display();
}
}
void randomVelocity()
{
PVector velocity;
float velocityX, velocityY;
for (int i = 0; i < getNumParticles(); i++)
{
Particle p = _particles.get(i);
velocityX = random (-1000, 1000);
velocityY = random (-1000, 1000);
velocity = new PVector(velocityX, velocityY);
p._v = velocity;
}
}
void clickBall(PVector ballMouse)
{
// Comprobamos si el usuario clicka una bola o quiere crear una
boolean createBall = true;
for (int i = 0; i < getNumParticles(); i++)
{
Particle p = _particles.get(i);
PVector position = p.getPosition();
// Comprobamos no solo el centro de la bola sino sus 4 partes
PVector positionAndRadio1 = new PVector(position.x + p.getRadius(), position.y + p.getRadius());
PVector positionAndRadio2 = new PVector(position.x + p.getRadius(), position.y - p.getRadius());
PVector positionAndRadio3 = new PVector(position.x - p.getRadius(), position.y - p.getRadius());
PVector positionAndRadio4 = new PVector(position.x - p.getRadius(), position.y + p.getRadius());
if ((ballMouse.x >= position.x && ballMouse.x <= positionAndRadio1.x && ballMouse.y >= position.y && ballMouse.y <= positionAndRadio1.y) ||
(ballMouse.x >= position.x && ballMouse.x <= positionAndRadio2.x && ballMouse.y <= position.y && ballMouse.y >= positionAndRadio2.y) ||
(ballMouse.x <= position.x && ballMouse.x >= positionAndRadio3.x && ballMouse.y <= position.y && ballMouse.y >= positionAndRadio3.y) ||
(ballMouse.x <= position.x && ballMouse.x >= positionAndRadio4.x && ballMouse.y >= position.y && ballMouse.y <= positionAndRadio4.y))
{
PVector velocity;
float velocityX, velocityY;
velocityX = random (-1000, 1000);
velocityY = random (-1000, 1000);
velocity = new PVector (velocityX, velocityY);
p._v = velocity;
createBall = false;
}
}
// Si el usuario no ha clickado una bola, creamos una nueva
if (createBall)
{
float velocityX = random (-1000, 1000);
float velocityY = random (-1000, 1000);
PVector velocity = new PVector (velocityX, velocityY);
float radio = 0.03075;
radio = worldToPixels(radio);
float masa = 0.210;
addParticle(getNumParticles()+1, ballMouse, velocity, masa, radio);
}
}
void goToCorner()
{
PVector velocity;
float velocityX, velocityY;
// Recorremos todas las bolas para cambiar su velocidad hacia la esquina de abajo izquierda
for (int i = 0; i < getNumParticles(); i++)
{
Particle p = _particles.get(i);
velocityX = mesaX - p._s.x;
velocityY = alto+mesaY - p._s.y;
velocity = new PVector(velocityX, velocityY);
p._v.set(velocity);
}
}
}