forked from yozlet/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle.cpp
More file actions
244 lines (196 loc) · 9.33 KB
/
Copy pathparticle.cpp
File metadata and controls
244 lines (196 loc) · 9.33 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// particle.cpp
// interface
//
// Created by Seiji Emery on 9/4/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include "particle.h"
#define NUM_ELEMENTS 4
glm::vec3 color0(1,0,0); // Motionless particle
glm::vec3 color1(0,1,0); // Spring force
glm::vec3 color2(0,0,1);
glm::vec3 color3(0,1,1);
float radii[NUM_ELEMENTS] = {0.3, 0.5, 0.2, 0.4};
ParticleSystem::ParticleSystem(int num,
glm::vec3 box,
int wrap,
float noiselevel,
float setscale,
float setgravity) {
// Create and initialize particles
int i, element;
bounds = box;
count = num;
wrapBounds = false;
noise = noiselevel;
gravity = setgravity;
scale = setscale;
particles = new Particle[count];
for (i = 0; i < count; i++) {
particles[i].position.x = randFloat()*box.x;
particles[i].position.y = randFloat()*box.y;
particles[i].position.z = randFloat()*box.z;
// Constrain to a small box in center
//particles[i].position.x = randFloat()+box.x/2.0;
//particles[i].position.y = randFloat()+box.y/2.0;
//particles[i].position.z = randFloat()+box.z/2.0;
particles[i].velocity.x = 0;
particles[i].velocity.y = 0;
particles[i].velocity.z = 0;
particles[i].parent = 0;
particles[i].link *= 0;
element = 1; //rand()%NUM_ELEMENTS;
particles[i].element = element;
if (element == 0) particles[i].color = color0;
else if (element == 1) particles[i].color = color1;
else if (element == 2) particles[i].color = color2;
else if (element == 3) particles[i].color = color3;
particles[i].radius = 0.10; //radii[element]*scale;
particles[i].isColliding = false;
}
}
bool ParticleSystem::updateHand(glm::vec3 pos, glm::vec3 vel, float radius) {
handPos = pos;
handVel = vel;
handRadius = radius;
return handIsColliding;
}
void ParticleSystem::resetHand() {
handActive = false;
handIsColliding = false;
handPos = glm::vec3(0,0,0);
handVel = glm::vec3(0,0,0);
handRadius = 0;
}
void ParticleSystem::render() {
for (unsigned int i = 0; i < count; ++i) {
glPushMatrix();
glTranslatef(particles[i].position.x, particles[i].position.y, particles[i].position.z);
if (particles[i].numSprung == 0) glColor3f(1,1,1);
else if (particles[i].numSprung == 1) glColor3f(0,1,0);
else if (particles[i].numSprung == 2) glColor3f(1,1,0);
else if (particles[i].numSprung >= 3) glColor3f(1,0,0);
glutSolidSphere(particles[i].radius, 15, 15);
glPopMatrix();
}
}
void ParticleSystem::link(int child, int parent) {
particles[child].parent = parent;
particles[child].velocity *= 0.5;
particles[parent].velocity += particles[child].velocity;
particles[child].velocity *= 0.0;
particles[child].color = glm::vec3(1,1,0);
particles[child].link = particles[parent].position - particles[child].position;
}
void ParticleSystem::simulate (float deltaTime) {
int i, j;
for (i = 0; i < count; ++i) {
if (particles[i].element != 0) {
if (particles[i].parent == 0) {
// Move particles
particles[i].position += particles[i].velocity * deltaTime;
// Add gravity
particles[i].velocity.y -= gravity*deltaTime;
// Drag: decay velocity
const float CONSTANT_DAMPING = 0.1;
particles[i].velocity *= (1.f - CONSTANT_DAMPING*deltaTime);
// Add velocity from field
//Field::addTo(particles[i].velocity);
//particles[i].velocity += Field::valueAt(particles[i].position);
// Add noise
const float RAND_VEL = 0.05;
if (noise) {
if (1) {
particles[i].velocity += glm::vec3((randFloat() - 0.5)*RAND_VEL,
(randFloat() - 0.5)*RAND_VEL,
(randFloat() - 0.5)*RAND_VEL);
}
if (randFloat() < noise*deltaTime) {
particles[i].velocity += glm::vec3((randFloat() - 0.5)*RAND_VEL*100,
(randFloat() - 0.5)*RAND_VEL*100,
(randFloat() - 0.5)*RAND_VEL*100);
}
}
} else {
particles[i].position = particles[particles[i].parent].position + particles[i].link;
}
// Check for collision with manipulator hand
particles[i].isColliding = (glm::vec3(particles[i].position - handPos).length() <
(radius + handRadius));
// Check for collision with other balls
float separation;
const float HARD_SPHERE_FORCE = 100.0;
const float SPRING_FORCE = 10.0;
const float SPRING_DAMPING = 0.5;
float spring_length = 0.5; //2*radii[1];
float spring_range = spring_length * 1.2;
float contact;
particles[i].isColliding = false;
particles[i].numSprung = 0;
for (j = 0; j < count; j++) {
if ((j != i) &&
(!particles[i].parent)) {
separation = glm::distance(particles[i].position, particles[j].position);
contact = particles[i].radius + particles[j].radius;
// Hard Sphere Scattering
if (separation < contact) {
particles[i].velocity += glm::normalize(particles[i].position - particles[j].position)*deltaTime*HARD_SPHERE_FORCE*(contact - separation);
particles[i].isColliding = true;
}
// Spring Action
if ((particles[i].element == 1) && (separation < spring_range)) {
particles[i].velocity += glm::normalize(particles[i].position - particles[j].position)*deltaTime*SPRING_FORCE*(spring_length - separation);
particles[i].velocity *= (1.f - SPRING_DAMPING*deltaTime);
particles[i].numSprung++;
}
// Link!
if ((particles[i].parent == 0) &&
(particles[j].parent != i) &&
(separation > 0.9*(particles[j].radius + particles[i].radius)) &&
(separation < 1.0*(particles[j].radius + particles[i].radius)) ) {
// Link i to j!!
//link(i, j);
}
}
}
if (!particles[i].parent) {
if (wrapBounds) {
// wrap around bounds
if (particles[i].position.x > bounds.x)
particles[i].position.x -= bounds.x;
else if (particles[i].position.x < 0.0f)
particles[i].position.x += bounds.x;
if (particles[i].position.y > bounds.y)
particles[i].position.y -= bounds.y;
else if (particles[i].position.y < 0.0f)
particles[i].position.y += bounds.y;
if (particles[i].position.z > bounds.z)
particles[i].position.z -= bounds.z;
else if (particles[i].position.z < 0.0f)
particles[i].position.z += bounds.z;
} else {
// Bounce at bounds
if (particles[i].position.x > bounds.x
|| particles[i].position.x < 0.f) {
if (particles[i].position.x > bounds.x) particles[i].position.x = bounds.x;
else particles[i].position.x = 0.f;
particles[i].velocity.x *= -1;
}
if (particles[i].position.y > bounds.y
|| particles[i].position.y < 0.f) {
if (particles[i].position.y > bounds.y) particles[i].position.y = bounds.y;
else particles[i].position.y = 0.f;
particles[i].velocity.y *= -1;
}
if (particles[i].position.z > bounds.z
|| particles[i].position.z < 0.f) {
if (particles[i].position.z > bounds.z) particles[i].position.z = bounds.z;
else particles[i].position.z = 0.f;
particles[i].velocity.z *= -1;
}
}
}
}
}
}