-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluidSimulation3d.cpp
More file actions
562 lines (484 loc) · 21.3 KB
/
FluidSimulation3d.cpp
File metadata and controls
562 lines (484 loc) · 21.3 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include <SDL.h>
#include <vector>
#include <cmath>
#include <iostream>
#include <random>
#include <string>
#include <algorithm>
// Íàñòðîéêè
const int WIDTH = 600;
const int HEIGHT = 720;
const int FPS = 6000;
const float PARTICLE_RADIUS = 10.0f;
const float GRAVITY = 0.0981f;
const float REST_DENSITY = 0.01f;
const float GAS_CONSTANT = 0.95f;
const float VISCOSITY = 0.9f;
const int PARTICLE_CREATION_RATE = 10;
const float COHESION_STRENGTH = 0.0f;
const float DAMPING = 0.99f;
const float DRAG_COEFFICIENT = 0.1f;
const int GRAB_RADIUS = 100;
const float SPRING_CONSTANT = 0.25f;
const int MAX_PARTICLES = 150000; // Îãðàíè÷åíèå íà êîëè÷åñòâî ÷àñòèö
// Ïðåïÿòñòâèå (òåïåðü ñôåðà)
const float OBSTACLE_X = 400.0f;
const float OBSTACLE_Y = 300.0f;
const float OBSTACLE_Z = 200.0f;
const float OBSTACLE_RADIUS = 100.0f;
// Ñòåíû (òåïåðü îãðàíè÷èâàþùèé êóá)
const float WALL_THICKNESS = 0.0f;
const SDL_Color WALL_COLOR = { 255, 255, 255, 255 };
const float PAD_X = 20.0f;
const float PAD_Y = 20.0f;
const float PAD_Z = 20.0f;
// Ãðàäèåíò öâåòîâ
const int GRADIENT_STEPS = 256;
SDL_Color color_gradient[GRADIENT_STEPS];
const float max_speed = 25.0f;
// Óãëû êàìåðû
float cameraYaw = 0.0f; // Ïîâîðîò âîêðóã îñè Y
float cameraPitch = 0.0f; // Íàêëîí ââåðõ/âíèç
// Ïîçèöèÿ êàìåðû
float cameraX = 0.0f;
float cameraY = 0.0f;
float cameraZ = 0.0f;
// Ôóíêöèÿ äëÿ ñîçäàíèÿ ãðàäèåíòà öâåòîâ (íå èçìåíèëàñü)
void create_gradient(SDL_Color color1, SDL_Color color2, int steps, SDL_Color* gradient) {
for (int i = 0; i < steps; ++i) {
float t = i / (steps - 1.0f);
gradient[i].r = static_cast<Uint8>(color1.r * (1 - t) + color2.r * t);
gradient[i].g = static_cast<Uint8>(color1.g * (1 - t) + color2.g * t);
gradient[i].b = static_cast<Uint8>(color1.b * (1 - t) + color2.b * t);
gradient[i].a = 255;
}
}
// Ôóíêöèÿ äëÿ ðèñîâàíèÿ çàïîëíåííîãî êðóãà (òåïåðü ðèñóåò ñ ó÷åòîì ïåðñïåêòèâû, óãëîâ êàìåðû è ïîçèöèè êàìåðû)
void filledCircleRGBA(SDL_Renderer* renderer, float x, float y, float z, float radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
// Ïðèìåíåíèå ïîçèöèè êàìåðû
x -= cameraX;
y -= cameraY;
z -= cameraZ;
// Ïîâîðîò âîêðóã îñè Y (Yaw)
float rotatedX = x * cos(cameraYaw) - z * sin(cameraYaw);
float rotatedZ = x * sin(cameraYaw) + z * cos(cameraYaw);
// Íàêëîí ââåðõ/âíèç (Pitch) - íå ðåàëèçîâàí, òðåáóåò áîëåå ñëîæíîé ìàòðèöû ïîâîðîòà
// Ïðîñòàÿ ïðîåêöèÿ ïåðñïåêòèâû (ìîæíî óëó÷øèòü)
float perspectiveScale = 1.0f / (1.0f + rotatedZ / 500.0f);
int screenX = static_cast<int>(rotatedX * perspectiveScale + WIDTH / 2);
int screenY = static_cast<int>(y * perspectiveScale + HEIGHT / 2);
int screenRadius = static_cast<int>(radius * perspectiveScale);
for (int dy = -screenRadius; dy <= screenRadius; ++dy) {
for (int dx = -screenRadius; dx <= screenRadius; ++dx) {
if (dx * dx + dy * dy <= screenRadius * screenRadius) {
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_RenderDrawPoint(renderer, screenX + dx, screenY + dy);
}
}
}
}
// Êëàññ ÷àñòèöû (äîáàâëåí z)
class Particle {
public:
float x;
float y;
float z; // Íîâàÿ êîîðäèíàòà
float vx;
float vy;
float vz; // Íîâàÿ ñêîðîñòü
float density;
float pressure;
std::vector<Particle*> near_particles;
bool grabbed;
Particle(float x, float y, float z) : x(x), y(y), z(z), vx(0), vy(0), vz(0), density(0), pressure(0), grabbed(false) {}
void update() {
vy += GRAVITY;
x += vx;
y += vy;
z += vz; // Îáíîâëåíèå z
// Ñòîëêíîâåíèÿ ñ ãðàíèöàìè (êóá)
if (x < PARTICLE_RADIUS + PAD_X) {
x = PARTICLE_RADIUS + PAD_X;
vx *= -0.7f;
}
else if (x > WIDTH - PARTICLE_RADIUS - PAD_X) {
x = WIDTH - PARTICLE_RADIUS - PAD_X;
vx *= -0.7f;
}
if (y > HEIGHT - PARTICLE_RADIUS - PAD_Y) {
y = HEIGHT - PARTICLE_RADIUS - PAD_Y;
vy *= -0.7f;
}
// Äîáàâëåíû îãðàíè÷åíèÿ ïî z
if (z < PARTICLE_RADIUS + PAD_Z) {
z = PARTICLE_RADIUS + PAD_Z;
vz *= -0.7f;
}
else if (z > 400 - PARTICLE_RADIUS - PAD_Z) { // Ïðåäïîëàãàåìàÿ ìàêñèìàëüíàÿ ãëóáèíà
z = 400 - PARTICLE_RADIUS - PAD_Z;
vz *= -0.7f;
}
// Ñòîëêíîâåíèå ñ ïðåïÿòñòâèåì (ñôåðà)
float dx = x - OBSTACLE_X;
float dy = y - OBSTACLE_Y;
float dz = z - OBSTACLE_Z;
float distance_to_obstacle_sq = dx * dx + dy * dy + dz * dz; // 3D ðàññòîÿíèå
float radius_sum_sq = (PARTICLE_RADIUS + OBSTACLE_RADIUS) * (PARTICLE_RADIUS + OBSTACLE_RADIUS);
if (distance_to_obstacle_sq < radius_sum_sq) {
float distance_to_obstacle = std::sqrt(distance_to_obstacle_sq);
float overlap = (PARTICLE_RADIUS + OBSTACLE_RADIUS) - distance_to_obstacle;
x += overlap * dx / distance_to_obstacle;
y += overlap * dy / distance_to_obstacle;
z += overlap * dz / distance_to_obstacle; // Îáíîâëåíèå z
vx *= -0.7f;
vy *= -0.7f;
vz *= -0.7f; // Îáíîâëåíèå vz
}
// Äåìïôèðîâàíèå ñêîðîñòè
vx *= DAMPING;
vy *= DAMPING;
vz *= DAMPING; // Äåìïôèðîâàíèå vz
}
void draw(SDL_Renderer* renderer) {
// Èñïîëüçîâàíèå ãðàäèåíòà (íå èçìåíèëàñü)
float speed = std::sqrt(vx * vx + vy * vy + vz * vz); // 3D speed
int colorIndex = static_cast<int>(speed / max_speed * (GRADIENT_STEPS - 1));
colorIndex = std::min(colorIndex, GRADIENT_STEPS - 1);
// Ðèñîâàíèå êðóãà ñ ó÷åòîì ïåðñïåêòèâû
filledCircleRGBA(renderer, x, y, z, PARTICLE_RADIUS,
color_gradient[colorIndex].r, color_gradient[colorIndex].g,
color_gradient[colorIndex].b, color_gradient[colorIndex].a);
}
void reset_velocity() {
vx = 0;
vy = 0;
vz = 0; // Ñáðîñ vz
}
void apply_spring_force(Particle* other) {
float dx = x - other->x;
float dy = y - other->y;
float dz = z - other->z; // Ðàçíèöà ïî z
float distance_sq = dx * dx + dy * dy + dz * dz; // 3D ðàññòîÿíèå
float radius_sum_sq = (2 * PARTICLE_RADIUS) * (2 * PARTICLE_RADIUS);
if (distance_sq > radius_sum_sq) {
float distance = std::sqrt(distance_sq);
float force = SPRING_CONSTANT * (distance - 2 * PARTICLE_RADIUS);
float fx = force * dx / distance;
float fy = force * dy / distance;
float fz = force * dz / distance; // Ñèëà ïî z
vx -= fx / density;
vy -= fy / density;
vz -= fz / density; // Ïðèìåíåíèå ñèëû ïî z
other->vx += fx / other->density;
other->vy += fy / other->density;
other->vz += fz / other->density; // Ïðèìåíåíèå ñèëû ïî z
}
}
};
// Îïòèìèçèðîâàííàÿ ôóíêöèÿ äëÿ íàõîæäåíèÿ ñîñåäåé (èçìåíåíà äëÿ 3D)
void find_neighbors(std::vector<Particle>& particles) {
for (size_t i = 0; i < particles.size(); ++i) {
particles[i].near_particles.clear();
for (size_t j = i + 1; j < particles.size(); ++j) {
float dx = particles[i].x - particles[j].x;
float dy = particles[i].y - particles[j].y;
float dz = particles[i].z - particles[j].z; // Ðàçíèöà ïî z
float distance_sq = dx * dx + dy * dy + dz * dz; // 3D ðàññòîÿíèå
float radius_sum_sq = (2 * PARTICLE_RADIUS) * (2 * PARTICLE_RADIUS);
if (distance_sq < radius_sum_sq) {
particles[i].near_particles.push_back(&particles[j]);
particles[j].near_particles.push_back(&particles[i]);
}
}
}
}
// Îïòèìèçèðîâàííàÿ ôóíêöèÿ äëÿ âû÷èñëåíèÿ ïëîòíîñòè è äàâëåíèÿ (íå èçìåíèëàñü)
void calculate_density_pressure(std::vector<Particle>& particles) {
for (Particle& particle : particles) {
particle.density = 0;
for (Particle* other : particle.near_particles) {
float dx = particle.x - other->x;
float dy = particle.y - other->y;
float dz = particle.z - other->z; // Ðàçíèöà ïî z (íå èñïîëüçóåòñÿ)
float distance_sq = dx * dx + dy * dy + dz * dz; // 3D ðàññòîÿíèå
float radius_sum_sq = (2 * PARTICLE_RADIUS) * (2 * PARTICLE_RADIUS);
if (distance_sq < radius_sum_sq) {
particle.density += 1;
}
}
particle.density = std::max(particle.density, 0.1f);
particle.pressure = GAS_CONSTANT * (particle.density - REST_DENSITY);
}
}
// Îïòèìèçèðîâàííàÿ ôóíêöèÿ äëÿ âû÷èñëåíèÿ ñèë (èçìåíåíà äëÿ 3D)
void calculate_forces(std::vector<Particle>& particles) {
for (Particle& particle : particles) {
float dx = 0, dy = 0, dz = 0; // Äîáàâëåí dz
for (Particle* other : particle.near_particles) {
float dx_diff = particle.x - other->x;
float dy_diff = particle.y - other->y;
float dz_diff = particle.z - other->z; // Ðàçíèöà ïî z
float distance_sq = dx_diff * dx_diff + dy_diff * dy_diff + dz_diff * dz_diff; // 3D ðàññòîÿíèå
float radius_sum_sq = (2 * PARTICLE_RADIUS) * (2 * PARTICLE_RADIUS);
if (distance_sq < radius_sum_sq && distance_sq > 0) {
float distance = std::sqrt(distance_sq);
// Ñèëà äàâëåíèÿ
float pressure_force = (particle.pressure + other->pressure) / 2 * (1 - distance / (2 * PARTICLE_RADIUS));
dx += pressure_force * dx_diff / distance;
dy += pressure_force * dy_diff / distance;
dz += pressure_force * dz_diff / distance; // Ñèëà ïî z
// Ñèëà âÿçêîñòè
float vx_diff = other->vx - particle.vx;
float vy_diff = other->vy - particle.vy;
float vz_diff = other->vz - particle.vz; // Ðàçíèöà ïî vz
float viscosity_force = VISCOSITY * (vx_diff * dx_diff + vy_diff * dy_diff + vz_diff * dz_diff) / distance_sq; // 3D âÿçêîñòü
dx += viscosity_force * dx_diff;
dy += viscosity_force * dy_diff;
dz += viscosity_force * dz_diff; // Ñèëà ïî z
// Ñèëà ñöåïëåíèÿ
float cohesion_force = COHESION_STRENGTH / distance;
dx -= cohesion_force * dx_diff;
dy -= cohesion_force * dy_diff;
dz -= cohesion_force * dz_diff; // Ñèëà ïî z
// Ñèëà ïðóæèíû (óæå èçìåíåíà äëÿ 3D)
particle.apply_spring_force(other);
}
}
// Ïðèìåíåíèå ñèë
particle.vx += dx / particle.density;
particle.vy += dy / particle.density;
particle.vz += dz / particle.density; // Ïðèìåíåíèå ñèëû ïî z
}
}
// Ôóíêöèÿ äëÿ ïðåîáðàçîâàíèÿ ýêðàííûõ êîîðäèíàò â ìèðîâûå ñ ó÷åòîì ïåðñïåêòèâû
void screenToWorld(int screenX, int screenY, float depth, float& worldX, float& worldY, float& worldZ) {
// Îáðàòíàÿ ïðîåêöèÿ ïåðñïåêòèâû
float perspectiveScale = 500.0f / (500.0f - depth);
worldX = (screenX - WIDTH / 2) / perspectiveScale;
worldY = (screenY - HEIGHT / 2) / perspectiveScale;
worldZ = depth;
// Îáðàòíûé ïîâîðîò âîêðóã îñè Y
float tempX = worldX;
worldX = tempX * cos(-cameraYaw) - worldZ * sin(-cameraYaw);
worldZ = tempX * sin(-cameraYaw) + worldZ * cos(-cameraYaw);
// Ïðèìåíåíèå ïîçèöèè êàìåðû
worldX += cameraX;
worldY += cameraY;
worldZ += cameraZ;
}
int main(int argc, char* argv[]) {
// Èíèöèàëèçàöèÿ SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
// Ñîçäàíèå îêíà
SDL_Window* window = SDL_CreateWindow("3D Fluid Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
// Ñîçäàíèå ðåíäåðåðà
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) {
std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Ñîçäàíèå ãðàäèåíòà
SDL_Color color1 = { 0, 0, 255, 255 };
SDL_Color color2 = { 255, 0, 0, 255 };
create_gradient(color1, color2, GRADIENT_STEPS, color_gradient);
// Ñîçäàíèå ñïèñêà ÷àñòèö
std::vector<Particle> particles;
// Ôëàãè äëÿ êíîïîê ìûøè
bool mouse_pressed_left = false;
bool mouse_pressed_right = false;
// Ñïèñîê çàõâà÷åííûõ ÷àñòèö
std::vector<Particle*> grabbed_particles;
// Ãåíåðàòîð ñëó÷àéíûõ ÷èñåë
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> distrib(-PARTICLE_RADIUS * 2, PARTICLE_RADIUS * 2);
std::uniform_real_distribution<> z_distrib(PAD_Z, 400 - PAD_Z); // Ðàñïðåäåëåíèå ïî z
// Êîîðäèíàòû êóðñîðà
int cursorX, cursorY;
// Ãëàâíûé öèêë
bool running = true;
Uint32 last_time = SDL_GetTicks();
while (running) {
// Îáðàáîòêà ñîáûòèé
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
else if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
mouse_pressed_left = true;
}
else if (event.button.button == SDL_BUTTON_RIGHT) {
mouse_pressed_right = true;
SDL_GetMouseState(&cursorX, &cursorY);
// Çàõâàò ÷àñòèö ñ ó÷åòîì ïåðñïåêòèâû
for (Particle& particle : particles) {
float worldX, worldY, worldZ;
screenToWorld(cursorX, cursorY, particle.z, worldX, worldY, worldZ);
float dx = particle.x - worldX;
float dy = particle.y - worldY;
float dz = particle.z - worldZ;
float distance_sq = dx * dx + dy * dy + dz * dz;
float grab_radius_sq = GRAB_RADIUS * GRAB_RADIUS;
if (distance_sq <= grab_radius_sq) {
particle.grabbed = true;
grabbed_particles.push_back(&particle);
}
}
}
}
else if (event.type == SDL_MOUSEBUTTONUP) {
if (event.button.button == SDL_BUTTON_LEFT) {
mouse_pressed_left = false;
}
else if (event.button.button == SDL_BUTTON_RIGHT) {
mouse_pressed_right = false;
for (Particle* particle : grabbed_particles) {
particle->grabbed = false;
}
grabbed_particles.clear();
}
}
else if (event.type == SDL_MOUSEWHEEL) {
// Ïðèáëèæåíèå/îòäàëåíèå ñ ïîìîùüþ êîëåñèêà ìûøè
SDL_GetMouseState(&cursorX, &cursorY);
float worldX, worldY, worldZ;
screenToWorld(cursorX, cursorY, 0.0f, worldX, worldY, worldZ);
float directionX = worldX - cameraX;
float directionY = worldY - cameraY;
float directionZ = worldZ - cameraZ;
float length = sqrt(directionX * directionX + directionY * directionY + directionZ * directionZ);
if (event.wheel.y > 0) { // Ïðîêðóòêà ââåðõ
cameraX += directionX / length * 10;
cameraY += directionY / length * 10;
cameraZ += directionZ / length * 10;
}
else if (event.wheel.y < 0) { // Ïðîêðóòêà âíèç
cameraX -= directionX / length * 10;
cameraY -= directionY / length * 10;
cameraZ -= directionZ / length * 10;
}
}
else if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_r) { // Î÷èñòèòü êàðòó îò ÷àñòèö
particles.clear();
}
}
}
// Îáðàáîòêà óäåðæàíèÿ êëàâèø (äëÿ ïëàâíîãî äâèæåíèÿ êàìåðû)
const Uint8* keystate = SDL_GetKeyboardState(NULL);
if (keystate[SDL_SCANCODE_W]) { // Âïåðåä
cameraX += 10 * sin(cameraYaw);
cameraZ += 10 * cos(cameraYaw);
}
if (keystate[SDL_SCANCODE_S]) { // Íàçàä
cameraX -= 10 * sin(cameraYaw);
cameraZ -= 10 * cos(cameraYaw);
}
if (keystate[SDL_SCANCODE_A]) { // Âëåâî
cameraX -= 10 * cos(cameraYaw);
cameraZ += 10 * sin(cameraYaw);
}
if (keystate[SDL_SCANCODE_D]) { // Âïðàâî
cameraX += 10 * cos(cameraYaw);
cameraZ -= 10 * sin(cameraYaw);
}
if (keystate[SDL_SCANCODE_SPACE]) { // Ââåðõ
cameraY -= 10;
}
if (keystate[SDL_SCANCODE_LSHIFT]) { // Âíèç
cameraY += 10;
}
// Âðàùåíèå êàìåðû ñ ïîìîùüþ Ctrl + äâèæåíèå ìûøè
if (keystate[SDL_SCANCODE_LCTRL]) {
SDL_SetRelativeMouseMode(SDL_TRUE); // Âêëþ÷àåì îòíîñèòåëüíûé ðåæèì ìûøè
int mouseX, mouseY;
SDL_GetRelativeMouseState(&mouseX, &mouseY);
cameraYaw += mouseX * 0.01f;
cameraPitch += mouseY * 0.01f;
}
else {
SDL_SetRelativeMouseMode(SDL_FALSE); // Âûêëþ÷àåì îòíîñèòåëüíûé ðåæèì ìûøè
}
// Ñîçäàíèå íîâûõ ÷àñòèö (äîáàâëåíà z êîîðäèíàòà)
if (mouse_pressed_left && particles.size() < MAX_PARTICLES) {
for (int i = 0; i < PARTICLE_CREATION_RATE; ++i) {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
float worldX, worldY, worldZ;
screenToWorld(mouseX, mouseY, 200.0f, worldX, worldY, worldZ); // Èñïîëüçóåì 200.0f äëÿ worldZ
particles.emplace_back(worldX + distrib(gen), worldY + distrib(gen), worldZ);
}
}
// Ïåðåìåùåíèå çàõâà÷åííûõ ÷àñòèö ñ ó÷åòîì ïåðñïåêòèâû
if (mouse_pressed_right) {
SDL_GetMouseState(&cursorX, &cursorY);
for (Particle* particle : grabbed_particles) {
float worldX, worldY, worldZ;
screenToWorld(cursorX, cursorY, particle->z, worldX, worldY, worldZ);
float dx = worldX - particle->x;
float dy = worldY - particle->y;
float dz = worldZ - particle->z;
particle->vx = dx * DRAG_COEFFICIENT;
particle->vy = dy * DRAG_COEFFICIENT;
particle->vz = dz * DRAG_COEFFICIENT;
}
}
// Îáíîâëåíèå ôèçèêè
find_neighbors(particles);
calculate_density_pressure(particles);
calculate_forces(particles);
// Îáíîâëåíèå ÷àñòèö
for (Particle& particle : particles) {
particle.update();
}
// Îòðèñîâêà
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Ðèñóåì ïðåïÿòñòâèå (ñôåðà)
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// Ðèñîâàíèå ñôåðû (íå ðåàëèçîâàíî)
// ...
// Ðèñóåì ÷àñòèöû
for (Particle& particle : particles) {
particle.draw(renderer);
}
// Êîîðäèíàòû êóðñîðà
int cursorX = 0, cursorY = 0; // Èíèöèàëèçèðóåì îáå ïåðåìåííûå
// Ðèñóåì áåëóþ òî÷êó äëÿ êóðñîðà
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawPoint(renderer, cursorX, cursorY);
// Âûâîä èíôîðìàöèè â çàãîëîâîê îêíà
SDL_GetMouseState(&cursorX, &cursorY);
float worldX, worldY, worldZ;
screenToWorld(cursorX, cursorY, 0.0f, worldX, worldY, worldZ);
std::string title = "3D Fluid Simulation | Particles: " + std::to_string(particles.size()) + "/" + std::to_string(MAX_PARTICLES) +
" | Camera: (" + std::to_string(int(cameraX)) + ", " + std::to_string(int(cameraY)) + ", " + std::to_string(int(cameraZ)) +
") Yaw: " + std::to_string(int(cameraYaw * 180.0f / M_PI)) + "° Pitch: " + std::to_string(int(cameraPitch * 180.0f / M_PI)) + "°" +
" | Cursor: (" + std::to_string(int(worldX)) + ", " + std::to_string(int(worldY)) + ", " + std::to_string(int(worldZ)) + ")";
SDL_SetWindowTitle(window, title.c_str());
// Âûâîä FPS è êîëè÷åñòâà ÷àñòèö
Uint32 current_time = SDL_GetTicks();
float fps = 1000.0f / (current_time - last_time);
last_time = current_time;
SDL_RenderPresent(renderer);
SDL_Delay(1000 / FPS);
}
// Îñâîáîæäåíèå ðåñóðñîâ
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}