-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.c
383 lines (370 loc) · 10.6 KB
/
particle.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* particle.c
*
* Created on: Mar 8, 2013
* Author: mathijs
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "particle.h"
#include "helper.h"
#include "mainwindow.h"
static const float STEP_SIZE = (0.01f);
const enum BOUNDS CONST_BOUND = NONE;
const float CONST_MASS_GRAVITY = 0.05f;
const float CONST_GRAVITY = 0.00f;
const float CONST_MASS = 1.5f;
const float CONST_SPEED = 0.0f;
const double CONST_RESTITUTION = 0.85f;
const double CONST_VMIN = 1E-20;
const int CONST_COLLISION = 1;
const int NUM_PARTICLES = 250;
const int TRACE_LENGTH = 1000;
static void init(void);
static struct Particle *particles = NULL;
struct Particle* particle()
{
if (particles == NULL)
{
init();
}
return particles;
}
void particle_draw(struct Particle *a)
{
#if MUTEX
pthread_mutex_lock(&(a->mutex));
#endif
glBegin(GL_LINE_LOOP);
glColor4f(a->color.r, a->color.g, a->color.b, a->color.a);
for (float i = 0; i <= (2 * M_PI) + STEP_SIZE; i += STEP_SIZE)
{
glVertex2f(a->x + (sin(i) * a->r), a->y + (cos(i) * a->r));
}
glEnd();
#if MUTEX
pthread_mutex_unlock(&(a->mutex));
#endif
}
int particle_interact(struct Particle *a, struct Particle *b)
{
#if MUTEX
pthread_mutex_lock(&(a->mutex));
pthread_mutex_lock(&(b->mutex));
#endif
if ((a->alive != 1) || (b->alive != 1))
{
return -1;
}
/* Detect center of interaction */
double p = b->x - a->x;
double q = b->y - a->y;
if (CONST_BOUND == WRAP)
{
/* Wrap around, use shortest distance */
if (p > (WIN_WIDTH_F / 2.0f))
{
p -= WIN_WIDTH_F;
}
else if (p < (-WIN_WIDTH_F / 2.0f))
{
p += WIN_WIDTH_F;
}
if (q > (WIN_HEIGHT_F / 2.0f))
{
q -= WIN_HEIGHT_F;
}
else if (q < (-WIN_HEIGHT_F / 2.0f))
{
q += WIN_HEIGHT_F;
}
}
const double h2 = (p * p) + (q * q);
if (CONST_COLLISION != 0)
{
/* Collisions enabled */
double h = sqrt(h2);
if (h < (a->r + b->r))
{
/* Collision detected */
a->hit = b->hit = 1;
#if 0
if (Constants.MUSH)
{
/*
* When mush is enabled, merge both particles, where the
* heaviest particle survives
*/
/*
* Detect lightest particle, merge with heaviest and flag
* for deletion
*/
if (m < b.m)
{
/* Delte this particle */
double t = m / (b.m + m);
b.x += p * t;
b.y += q * t;
b.vx += (vx - b.vx) * t;
b.vy += (vy - b.vy) * t;
b.m += m;
b.z = Math.sqrt((b.m / b.d) / Math.PI);
delete = true;
}
else
{
/* Delete other particle */
double t = b.m / (b.m + m);
x += p * t;
y += q * t;
vx += (b.vx - vx) * t;
vy += (b.vy - vy) * t;
m += b.m;
z = Math.sqrt((m / d) / Math.PI);
b.delete = true;
}
return true;
}
else
#endif
if (h > 1e-10)
{
/* Compute the elastic collision of two particles */
/* http://en.wikipedia.org/wiki/Elastic_collision */
double v1, v2, r1, r2, s, t, v;
/* Normalized impact direction */
p /= h;
q /= h;
/* Impact velocity */
v1 = a->vx * p + a->vy * q;
v2 = b->vx * p + b->vy * q;
/* Remainder velocity */
r1 = a->vx * q - a->vy * p;
r2 = b->vx * q - b->vy * p;
if (v1 < v2)
{
#if MUTEX
pthread_mutex_unlock(&(b->mutex));
pthread_mutex_unlock(&(a->mutex));
#endif
return 0;
}
/* Combine mass */
s = a->m + b->m;
if (s == 0)
{
#if MUTEX
pthread_mutex_unlock(&(b->mutex));
pthread_mutex_unlock(&(a->mutex));
#endif
return 0;
}
t = (v1 * a->m + v2 * b->m) / s;
v = t + CONST_RESTITUTION * (v2 - v1) * b->m / s;
a->vx = v * p + r1 * q;
a->vy = v * q - r1 * p;
v = t + CONST_RESTITUTION * (v1 - v2) * a->m / s;
b->vx = v * p + r2 * q;
b->vy = v * q - r2 * p;
}
}
}
if (CONST_MASS_GRAVITY != 0 && h2 > 1e-10 && (a->hit != 1) && (b->hit != 1))
{
/* When gravity is enabled, "drop" particles to bottom */
double dv;
dv = CONST_MASS_GRAVITY * b->m / h2;
a->vx += dv * p;
a->vy += dv * q;
dv = CONST_MASS_GRAVITY * a->m / h2;
b->vx -= dv * p;
b->vy -= dv * q;
}
#if MUTEX
pthread_mutex_unlock(&(b->mutex));
pthread_mutex_unlock(&(a->mutex));
#endif
return 0;
}
int particle_move(struct Particle *a)
{
#if MUTEX
pthread_mutex_lock(&(a->mutex));
#endif
a->x += a->vx;
a->y += a->vy;
if (CONST_BOUND != NONE)
{
/* The particles should react to the screen boundary */
if (a->x + a->r > WIN_WIDTH_F)
{
if (CONST_BOUND == WRAP)
{
a->x -= WIN_WIDTH_F;
}
else if (CONST_BOUND == BOUNCE)
{
if (a->vx > 0)
{
a->vx *= CONST_RESTITUTION; /* restitution */
}
a->vx = -fabs(a->vx) - CONST_VMIN; /* reverse velocity */
a->hit = 1;
/* Check if location is completely off screen */
if (a->x - a->r > WIN_WIDTH_F)
{
a->x = WIN_WIDTH_F + a->r;
}
}
}
/* Display x */
if (a->x - a->r < 0.0f)
{
if (CONST_BOUND == WRAP)
{
a->x += WIN_WIDTH_F;
}
else if (CONST_BOUND == BOUNCE)
{
if (a->vx < 0)
{
a->vx *= CONST_RESTITUTION;
}
a->vx = fabs(a->vx) + CONST_VMIN;
a->hit = 1;
if (a->x + a->r < 0.0f)
{
a->x = -a->r;
}
}
}
if (a->y + a->r > WIN_HEIGHT_F)
{
if (CONST_BOUND == WRAP)
{
a->y -= WIN_HEIGHT_F;
}
else if (CONST_BOUND == BOUNCE)
{
if (a->vy > 0)
{
a->vy *= CONST_RESTITUTION;
}
a->vy = -fabs(a->vy) - CONST_VMIN;
a->hit = 1;
if (a->y - a->r > WIN_HEIGHT_F)
{
a->y = WIN_HEIGHT_F + a->r;
}
}
}
/* Display y */
if (a->y - a->r < 0.0f)
{
if (CONST_BOUND == WRAP)
{
a->y += WIN_HEIGHT_F;
}
else if (CONST_BOUND == BOUNCE)
{
if (a->vy < 0)
{
a->vy *= CONST_RESTITUTION;
}
a->vy = fabs(a->vy) + CONST_VMIN;
a->hit = 1;
if (a->y + a->r < 0)
{
a->y = -a->r;
}
}
}
}
if (a->hit != 1)
{
/* if not hit, exert gravity */
a->vy += CONST_GRAVITY;
}
a->hit = 0;
#if TRACE
a->trace[a->trace_idx].x = a->x;
a->trace[a->trace_idx].y = a->y;
a->trace[a->trace_idx].init = 1;
a->trace_idx = (a->trace_idx + 1) % TRACE_LENGTH;
#endif
#if MUTEX
pthread_mutex_unlock(&(a->mutex));
#endif
return 0;
}
void particle_draw_trace(struct Particle *a)
{
#if TRACE
#if MUTEX
pthread_mutex_lock(&(a->mutex));
#endif
int idx;
glColor4f(a->color.r, a->color.g, a->color.b, a->color.a);
glBegin(GL_LINES);
for (idx = 0; idx < TRACE_LENGTH - 1; idx++)
{
int trace_idx = (a->trace_idx + idx) % TRACE_LENGTH;
int trace_idx_next = (a->trace_idx + idx + 1) % TRACE_LENGTH;
if ((a->trace[trace_idx].init != 0) && (a->trace[trace_idx_next].init != 0))
{
float x = a->trace[trace_idx].x;
float y = a->trace[trace_idx].y;
glVertex2f(x, y);
x = a->trace[trace_idx_next].x;
y = a->trace[trace_idx_next].y;
glVertex2f(x, y);
}
}
glEnd();
#if MUTEX
pthread_mutex_unlock(&(a->mutex));
#endif
#endif
}
static void init(void)
{
int idx;
int size_of = sizeof(struct Particle);
particles = malloc(NUM_PARTICLES * size_of);
for (idx = 0; idx < NUM_PARTICLES; idx++)
{
const float r = get_random_float(2.0f, 5.0f);
particles[idx].pid = idx;
particles[idx].x = get_random_float(0.0f + r, WIN_WIDTH_F - r);
particles[idx].y = get_random_float(0.0f + r, WIN_HEIGHT_F - r);
particles[idx].r = r;
particles[idx].m = CONST_MASS;
particles[idx].vx = get_random_float(0.0f, CONST_SPEED);
particles[idx].vy = get_random_float(0.0f, CONST_SPEED);
particles[idx].color.r = get_random_float(0.5f, 1.0f);
particles[idx].color.g = get_random_float(0.5f, 1.0f);
particles[idx].color.b = get_random_float(0.5f, 1.0f);
particles[idx].color.a = 1.0f;
particles[idx].alive = 1;
particles[idx].hit = 0;
#if TRACE
particles[idx].trace_idx = 0;
particles[idx].trace = malloc(TRACE_LENGTH * sizeof(struct Particle_Trace));
for (int ndx = 0; ndx < TRACE_LENGTH; ndx++)
{
particles[idx].trace[ndx].x = -1.0f;
particles[idx].trace[ndx].y = -1.0f;
particles[idx].trace[ndx].init = 0;
}
#endif
#if MUTEX
pthread_mutex_init(&(particles[idx].mutex), NULL);
#endif
printf("[%d] x %4.2f y %4.2f r %4.2f Cr %4.2f Cg %4.2f Cb %4.2f Ca %4.2f \n", idx, particles[idx].x, particles[idx].y,
particles[idx].r, particles[idx].color.r, particles[idx].color.g, particles[idx].color.b, particles[idx].color.a);
}
}