-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle.c
392 lines (339 loc) · 9.36 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
384
385
386
387
388
389
390
391
392
#include <SDL2/SDL_timer.h>
#include <stdbool.h>
#include "particle.h"
#include "common.h"
void particle_init(Particle *p, float x, float y, int radius, SDL_Color color) {
p->current.x = x;
p->current.y = y;
p->previous.x = x;
p->previous.y = y;
p->acceleration.x = 0;
p->acceleration.y = 0;
p->radius = radius;
p->color = color;
}
void particles_init(Particles *p, size_t size)
{
p->array = malloc(size * sizeof(Particle));
p->used = 0;
p->size = size;
}
void particles_add(Particles *a, Particle p)
{
if (a->used == a->size)
{
a->size *= 2;
a->array = realloc(a->array, a->size * sizeof(Particle));
}
a->array[a->used++] = p;
}
void particles_free(Particles *p)
{
free(p->array);
p->array = NULL;
p->used = p->size = 0;
}
void particle_update_position(Particle *p, float dt)
{
const Vec2 velocity = {
.x = p->current.x - p->previous.x,
.y = p->current.y - p->previous.y
};
p->previous = p->current;
p->current.x = p->current.x + velocity.x + p->acceleration.x * dt * dt;
p->current.y = p->current.y + velocity.y + p->acceleration.y * dt * dt;
p->acceleration.x = 0;
p->acceleration.y = 0;
}
void particle_accelerate(Particle *p, Vec2 acc)
{
p->acceleration.x += acc.x;
p->acceleration.y += acc.y;
}
void particle_apply_gravity(Particle *p)
{
Vec2 gravity = {0.0f, 1000.0f};
particle_accelerate(p, gravity);
}
void particle_add_pos_force(Particles *particles, int x, int y)
{
for (size_t i = 0; i < particles->used; i++)
{
Particle *p = &particles->array[i];
Vec2 disp = {.x = x - p->current.x, .y = y - p->current.y};
float dist = sqrt((disp.x*disp.x)+ disp.y+disp.y);
if (dist > 0)
{
float nx = disp.x/dist;
float ny = disp.y/dist;
p->acceleration.x += p->acceleration.x + nx * 5000.0f;
p->acceleration.y += p->acceleration.y + ny * 5000.0f;
}
}
}
void particle_add_neg_force(Particles *particles, int x, int y)
{
for (size_t i = 0; i < particles->used; i++)
{
Particle *p = &particles->array[i];
Vec2 disp = {.x = x - p->current.x , .y = y - p->current.y};
float dist = sqrt((disp.x*disp.x)+ disp.y+disp.y);
if (dist > 0)
{
float nx = disp.x/dist;
float ny = disp.y/dist;
p->acceleration.x += p->acceleration.x - nx * 10000.0f;
p->acceleration.y += p->acceleration.y - ny * 10000.0f;
}
}
}
void particle_apply_constraint(Particle *p)
{
const float damp = 0.3f;
const Vec2 velocity = {
.x = p->current.x - p->previous.x,
.y = p->current.y - p->previous.y
};
if (p->current.y + p->radius >= SCREEN_H)
{
if (velocity.y > 0.05)
{
p->current.y = SCREEN_H - p->radius;
p->previous.y = p->current.y + velocity.y * damp;
} else {
p->current.y = SCREEN_H - p->radius;
p->previous.y = p->current.y;
}
}
if (p->current.y + p->radius <= 0)
{
p->current.y = 0 + p->radius;
p->previous.y = p->current.y + velocity.y * damp;
}
if (p->current.x + p->radius >= SCREEN_W)
{
p->current.x = SCREEN_W - p->radius;
p->previous.x = p->current.x + velocity.x * damp;
}
if (p->current.x + p->radius <= 0)
{
p->current.x = 0 - p->radius;
p->previous.x = p->current.x + velocity.x * damp;
}
}
void particle_solve_collision(Particle *a, Particle *b)
{
if (!a || !b) return;
if (a == b) return;
const Vec2 axis = {
.x = a->current.x - b->current.x,
.y = a->current.y - b->current.y
};
float dist = sqrt((axis.x * axis.x) + (axis.y * axis.y));
if (dist < a->radius + b->radius)
{
float nx = axis.x / dist;
float ny = axis.y / dist;
float delta = a->radius + b->radius - dist;
a->current.x += 0.5f * delta * nx;
a->current.y += 0.5f * delta * ny;
b->current.x -= 0.5f * delta * nx;
b->current.y -= 0.5f * delta * ny;
}
}
void particles_solve_collisions(Particles *p)
{
for (size_t i = 0; i < p->used; i++)
{
for (size_t j = i+1; j < p->used; j++)
{
particle_solve_collision(&p->array[i], &p->array[j]);
}
}
}
void particle_colorize_velocity(Particle *p)
{
float velocity_x = p->current.x - p->previous.x;
float velocity_y = p->current.y - p->previous.y;
float velocity = velocity_x + velocity_y;
p->color.r = fabs(velocity*2*255);
p->color.g = 0;
p->color.b = 0;
}
void particles_update(Particles *particles, Grid *g, float dt)
{
for (size_t i = 0; i < particles->used; i++)
{
Particle *p = &particles->array[i];
particle_apply_gravity(p);
particle_apply_constraint(p);
particle_update_position(p, dt);
//change_color(p);
}
particles_solve_collisions(particles);
//grid_clean_tiles(g);
//grid_populate_tiles(g, particles);
//grid_find_collisions(g);
}
void particles_render(SDL_Renderer *r, Particles *p, Grid *g, float dt)
{
const int sub_steps = VERLET_SUB_STEPS;
const float sub_dt = dt / (float)sub_steps;
for (int i = 0; i < sub_steps; i++)
{
particles_update(p, g, sub_dt);
}
for (size_t i = 0; i < p->used; i++)
{
particle_draw(r, &p->array[i]);
}
}
void particle_draw(SDL_Renderer *r, Particle *p)
{
SDL_SetRenderDrawColor(r, p->color.r, p->color.g, p->color.b, p->color.a);
for (int y = -p->radius; y <= p->radius; ++y)
{
int x_max = (int) sqrt((p->radius * p->radius) - (y*y));
SDL_RenderDrawLine(r, (int)(p->current.x - x_max), (int)(p->current.y + y),
(int)(p->current.x + x_max), (int)(p->current.y + y));
}
}
void particles_draw(SDL_Renderer *r, Particles *p)
{
for (size_t i = 0; i < p->used; i++)
{
particle_draw(r, &p->array[i]);
}
}
Grid* grid_init()
{
Grid *g = (Grid*)malloc(sizeof(Grid));
grid_get_tile_len(g, SCREEN_W, SCREEN_H, MAX_RADIUS);
g->cols = (SCREEN_W / g->tile_len);
g->rows = (SCREEN_H / g->tile_len);
g->tiles = (Tile*)malloc(sizeof(Tile) * (g->rows * g->cols));
for (int i = 0; i < g->rows; i++)
{
for (int j = 0; j < g->cols; j++)
{
int id = i * g->cols + j;
g->tiles[id].id = id;
g->tiles[id].pos.x = j * g->tile_len;
g->tiles[id].pos.y = i * g->tile_len;
g->tiles[id].p[0] = (Particle*)malloc(sizeof(Particle) * 8);
grid_clean_tiles(g);
//printf("id: %d, x: %d, y: %d\n", id, j*g->tile_len, i*g->tile_len);
}
}
//printf("rows: %d, cols: %d\n", g->rows, g->cols);
return g;
}
void grid_get_tile_len(Grid *g, int w, int h, int max_radius)
{
int size = max_radius;
while ((w % size != 0 || h % size != 0) && (size < w && size < h))
{
size++;
}
g->tile_len = size;
}
Tile* grid_get_tile_by_pos(Grid *g, int x, int y)
{
int id = g->cols * y + x;
return &g->tiles[id];
}
void grid_free(Grid *g)
{
free(g->tiles);
g->tiles = NULL;
free(g);
}
void grid_debug_draw(SDL_Renderer *r, Grid *g)
{
if (g == NULL) return;
SDL_SetRenderDrawColor(r, 255, 255, 255, 0);
int x = g->tile_len;
int y = g->tile_len;
while (x < SCREEN_W)
{
SDL_RenderDrawLine(r, x, 0, x, SCREEN_H);
x += g->tile_len;
}
while (y < SCREEN_H)
{
SDL_RenderDrawLine(r, 0, y, SCREEN_W, y);
y += g->tile_len;
}
}
void tiles_check_collisions(Tile *t1, Tile *t2)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
particle_solve_collision(t1->p[i], t2->p[j]);
}
}
}
void grid_populate_tiles(Grid *g, Particles *p)
{
for (size_t i = 0; i < p->used; i++)
{
Particle *check = &p->array[i];
int x = check->current.x / g->tile_len;
int y = check->current.y / g->tile_len;
Tile *t = grid_get_tile_by_pos(g, (int)x, (int)y);
bool assigned = false;
int idx = 0;
while (!assigned)
{
if (idx > 7)
{
break;
}
if (t->p[idx] == NULL)
{
t->p[idx] = check;
assigned = true;
}
idx++;
}
}
}
void grid_find_collisions(Grid *g)
{
int start = SDL_GetTicks();
for (int x = 0; x < g->cols; x++)
{
for (int y = 0; y < g->rows; y++)
{
Tile *t = grid_get_tile_by_pos(g, x, y);
int dx_min = -1;
int dx_max = 1;
int dy_min = -1;
int dy_max = 1;
if (x == 0) dx_min = 0;
if ((int)x == g->cols-1) dx_max = 0;
if (y == 0) dy_min = 0;
if ((int)y == g->rows-1) dy_max = 0;
for (int dx = dx_min; dx <= dx_max; dx++)
{
for (int dy = dy_min; dy <= dy_max; dy++)
{
Tile *t2 = grid_get_tile_by_pos(g, x + dx, y + dy);
tiles_check_collisions(t, t2);
}
}
}
}
}
void grid_clean_tiles(Grid *g)
{
for (int i = 0; i < g->rows * g->cols; i++)
{
for (int j = 0; j < 8; j++)
{
g->tiles[i].p[j] = NULL;
}
}
}