-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
586 lines (496 loc) · 16.9 KB
/
main.cpp
File metadata and controls
586 lines (496 loc) · 16.9 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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//#include "functions.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
//initialisation parameters
float gravity = 0;
int gcount = 0;
int playermassMultiplier = 1;
int ballmassMultiplier = 1;
int ballsAmount = 10;
bool mPressed, bPressed, pPressed, infoToggle, EfieldToggle, freezeToggle = 0;
int antigravplayer = 1;
sf::String playermassString, ballsmassString, ballsnumberString, gravString;
struct particle
{
float mass;
float vx;
float vy;
float px;
float py;
float Ex;
float Ey;
}balls[101];
//FUNCTIONS
void checkCollision(sf::Sprite &ballSprite1, sf::Sprite &ballSprite2, particle &ball1, particle &ball2)
{
sf::FloatRect boundingBox = ballSprite1.getGlobalBounds();
sf::FloatRect boundingBox2 = ballSprite2.getGlobalBounds();
float workingvx1 = ball1.vx;
float workingvy1 = ball1.vy;
float workingvx2 = ball2.vx;
float workingvy2 = ball2.vy;
float reducedmass = (ball1.mass - ball2.mass) / (ball1.mass + ball2.mass);
float combinedmass = (ball1.mass + ball2.mass);
float xDiff = ballSprite2.getPosition().x - ballSprite1.getPosition().x;
float yDiff = ballSprite2.getPosition().y - ballSprite1.getPosition().y;
float angle = atan2f(yDiff, xDiff);
if (boundingBox.contains(ballSprite2.getPosition()))
{
//separate balls (sometimes they end up one inside the other)
ballSprite2.setPosition(ballSprite2.getPosition().x + 6*cosf(angle), ballSprite2.getPosition().y + 6 * sinf(angle));
//loss in energy due to collision
ball1.vx *= 0.9;
ball2.vx *= 0.9;
ball1.vy *= 0.9;
ball2.vy *= 0.9;
//resultant momentum exchange
ball1.vx = reducedmass*workingvx1 + (2 * workingvx2 * (ball2.mass) / combinedmass);
ball1.vy = reducedmass*workingvy1 + (2 * workingvy2 * (ball2.mass) / combinedmass);
ball2.vx = (2 * workingvx1 * (ball1.mass) / combinedmass) - reducedmass*workingvx2;
ball2.vy = (2 * workingvy1 * (ball1.mass) / combinedmass) - reducedmass*workingvy2;
}
}
void movePlayerball(sf::Sprite &ballSprite, particle &ball, const sf::RenderWindow &window)
{
//check collision with side walls, bounce ball off
if ((ballSprite.getPosition().x > window.getSize().x - 10) || (ballSprite.getPosition().x < 10))
{
ball.vx *= 0.9;
ball.vx = -ball.vx;
}
if ((ballSprite.getPosition().y > window.getSize().y - 10) || (ballSprite.getPosition().y < 10))
{
ball.vy *= 0.9;
ball.vy = -ball.vy;
}
//Fix issue of balls sticking to or sinking through walls sometimes
if (ballSprite.getPosition().x < 10)
ballSprite.setPosition(11, ballSprite.getPosition().y);
if (ballSprite.getPosition().x > window.getSize().x - 10)
ballSprite.setPosition(window.getSize().x - 10, ballSprite.getPosition().y);
if (ballSprite.getPosition().y < 10)
ballSprite.setPosition(ballSprite.getPosition().x, 11);
if (ballSprite.getPosition().y > window.getSize().y - 10)
{
ball.vy = -abs(ball.vy);
ball.vx *= 0.97; // friction so balls slow naturally on floor surface
}
//gravitational acceleration
if (ballSprite.getPosition().y < window.getSize().y - 10)
ball.vy = ball.vy + 0.05*gravity*antigravplayer;
//move ball
ballSprite.move(ball.vx, ball.vy);
}
void moveBalls(sf::Sprite &ballSprite, particle &ball, const sf::RenderWindow &window)
{
//check collision with side walls, bounce ball off
if ((ballSprite.getPosition().x > window.getSize().x - 10) || (ballSprite.getPosition().x < 10))
{
ball.vx *= 0.9; // energy loss
ball.vx = -ball.vx;
}
//check collision with ceiling and floor, bounce off
if ((ballSprite.getPosition().y > window.getSize().y - 10) || (ballSprite.getPosition().y < 10))
{
ball.vy *= 0.9; // energy loss
ball.vy = -ball.vy;
if (ballSprite.getPosition().y > window.getSize().y -10)
{
ball.vx *= 0.97; // friction so balls slow naturally on floor surface
ball.vy *= 0.9;
}
}
//Fix issue of balls sticking to or sinking through walls sometimes
if (ballSprite.getPosition().x < 9)//left wall
ballSprite.setPosition(9, ballSprite.getPosition().y);
if (ballSprite.getPosition().x > window.getSize().x - 9)//right wall
ballSprite.setPosition(window.getSize().x - 9, ballSprite.getPosition().y);
if (ballSprite.getPosition().y < 9)//ceiling
ballSprite.setPosition(ballSprite.getPosition().x, 9);
if (ballSprite.getPosition().y > window.getSize().y - 9)//floor
ballSprite.setPosition(ballSprite.getPosition().x, window.getSize().y - 9);
//gravitational acceleration
if (ballSprite.getPosition().y < window.getSize().y - 12) // no gravitational pull on floor
ball.vy = ball.vy + 0.05*gravity;
// stop ball completeley if moving moving very slow
if (abs(ball.vy) < 0.1*gcount)
ball.vy = 0;
if (abs(ball.vx) < 0.1)
ball.vx = 0;
//move ball
ballSprite.move(ball.vx, ball.vy);
}
void reset(sf::Sprite &ballSprite, particle& ball, int multiplier, const sf::RenderWindow &window)
{
//if more than 70 balls, limit this "resest circle" to the first 70. "reset2" function deals with the rest
float multip = multiplier < 70 ? multiplier : 70;
float ballsnum = ballsAmount < 70 ? ballsAmount : 70;
float circumf = 20 * ballsnum;
float radius = circumf / (2 * 3.1459);
float xPos;
float yPos;
float angle = (2*3.145)/ballsnum;
xPos = radius * sinf(angle*multip);
yPos = radius * cosf(angle*multip);
ball.vx = 1.0 * sinf(angle*multip);
ball.vy = 1.0 * cosf(angle*multip);
ballSprite.setPosition( (window.getSize().x/2) + xPos, (window.getSize().y / 2) + yPos);
}
void reset2(sf::Sprite &ballSprite, particle& ball, int multiplier, const sf::RenderWindow &window)
{
//For resets when there are more than 70 balls
//Makes smaller "reset circle" inside the larger circle of the first 70
float multip = multiplier - 70;
float ballsnum = ballsAmount - 70;
float circumf = 20 * ballsnum;
float radius = circumf / (2 * 3.1459);
float xPos;
float yPos;
float angle = (2 * 3.145) / ballsnum;
xPos = radius * sinf(angle*multip);
yPos = radius * cosf(angle*multip);
ball.vx = 1.0 * sinf(angle*multip);
ball.vy = 1.0 * cosf(angle*multip);
ballSprite.setPosition((window.getSize().x / 2) + xPos, (window.getSize().y / 2) + yPos);
}
void Efield(sf::Sprite &ballSprite1, sf::Sprite &ballSprite2, particle &ball1, particle &ball2, const sf::RenderWindow &window)
{
float xDiff = ballSprite1.getPosition().x - ballSprite2.getPosition().x;
float yDiff = ballSprite1.getPosition().y - ballSprite2.getPosition().y;
float radius = 40;
float massratio = (ball1.mass / ball2.mass);
if (massratio < 1) // if ball2 heavier, reduce collision recoil on ball 2
{
if ((xDiff < 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vx -= 100 / (xDiff*xDiff + yDiff*yDiff);
ball2.vx += 100 * massratio / (xDiff*xDiff + yDiff*yDiff);
}
if ((xDiff > 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vx += 100 / (xDiff*xDiff + yDiff*yDiff);
ball2.vx -= 100 * massratio / (xDiff*xDiff + yDiff*yDiff);
}
if ((yDiff < 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vy -= 100 / (xDiff*xDiff + yDiff*yDiff);
ball2.vy += 100 * massratio / (xDiff*xDiff + yDiff*yDiff);
}
if ((yDiff > 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vy += 100 / (xDiff*xDiff + yDiff*yDiff);
ball2.vy -= 100 * massratio / (xDiff*xDiff + yDiff*yDiff);
}
}
else
{ //else reduce recoil speed of ball 1
massratio = (ball2.mass / ball1.mass);
if ((xDiff < 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vx -= 100 * (massratio) / (xDiff*xDiff + yDiff*yDiff);
ball2.vx += 100 / (xDiff*xDiff + yDiff*yDiff);
}
if ((xDiff > 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vx += 100 * (massratio) / (xDiff*xDiff + yDiff*yDiff);
ball2.vx -= 100 / (xDiff*xDiff + yDiff*yDiff);
}
if ((yDiff < 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vy -= 100 * (massratio) / (xDiff*xDiff + yDiff*yDiff);
ball2.vy += 100 / (xDiff*xDiff + yDiff*yDiff);
}
if ((yDiff > 0) && (sqrtf(xDiff*xDiff + yDiff*yDiff) < radius))
{
ball1.vy += 100 * (massratio) / (xDiff*xDiff + yDiff*yDiff);
ball2.vy -= 100 / (xDiff*xDiff + yDiff*yDiff);
}
}
}
//MAIN
int main()
{
//create window
sf::RenderWindow window(sf::VideoMode(800, 600), "Particles");
window.setFramerateLimit(60);
//create text
sf::Font font;
font.loadFromFile("kongtext.ttf");
sf::Text texts[9];
for (int i = 0;i < 9;++i)
{
texts[i].setFont(font);
texts[i].setCharacterSize(10);
texts[i].setPosition(5, 5 + i * 12);
}
//rename text boxes for ease of coding
sf::Text &playermassText = texts[0];
sf::Text &ballsamountText = texts[1];
sf::Text &ballsmassText = texts[2];
sf::Text &gravityText = texts[3];
sf::Text &infotoggleText = texts[4];
sf::Text &infoText = texts[5];
sf::Text &antigravText = texts[6];
sf::Text &EfieldText = texts[7];
sf::Text &freezeText = texts[8];
//permanent text box strings and positions
infotoggleText.setString("\nPress I to toggle info");
infoText.setString("\n\n\n.....Particles in a box simulator.....\n\nMild energy loss on collision with side walls or with other particles\nMild friction on floor surface\n\nAccelerate red player ball with arrow keys\n\nPress P to increase player ball mass (max 1000)\nPress M to increase dummy balls mass (max 1000)\nPress B to increase number of balls (max 100)\nPress G to set gravity\nPress F to toggle 'Free' player ball (anti-gravity)\nPress S to freeze dummy balls\nPress Q to toggle electric fields around balls\nPress R to reset balls to centre\n\nPress Esc to quit");
antigravText.setPosition(5, window.getSize().y - 50);
antigravText.setString("Anti-grav player: On");
EfieldText.setPosition(5, window.getSize().y - 62);
EfieldText.setString("Electric fields: On");
freezeText.setPosition(5, window.getSize().y - 74);
freezeText.setString("Freeze dummy balls: On");
//create balls and initialise positions
sf::Texture ballsTexture;
ballsTexture.loadFromFile("bulletSprites.png");
sf::Sprite ballSprites[101];
for (int i = 0; i < 101; ++i)
{
if (i == 0)
{
ballSprites[i].setTexture(ballsTexture);
ballSprites[i].setTextureRect(sf::IntRect(173, 112, 11, 11));
ballSprites[i].setOrigin(5, 5);
ballSprites[i].setPosition(400, 300);
ballSprites[i].setScale(3, 3);
}
else
{
ballSprites[i].setTexture(ballsTexture);
ballSprites[i].setTextureRect(sf::IntRect(203, 112, 11, 11));
ballSprites[i].setOrigin(5, 5);
ballSprites[i].setPosition( 9 + (0.9* window.getSize().x/ballsAmount)*i , window.getSize().y -20);
}
}
//define ball[0] (sprite) as player, for ease of coding
sf::Sprite &playerSprite = ballSprites[0];
particle &player = balls[0];
//set mass of player ball
player.mass = playermassMultiplier * 100;
//initialise ball masses
for (int i = 1; i < ballsAmount; ++i)
{
balls[i].mass = ballmassMultiplier * 100;
}
//MAIN RUNNING WINDOW
while (window.isOpen())
{
//if mass or number of balls changed, reassign masses before new collisions happen
if (mPressed == 1 || bPressed == 1 || pPressed == 1)
{
for (int i = 1; i < ballsAmount; ++i)
{
balls[i].mass = ballmassMultiplier * 100;
}
player.mass = playermassMultiplier * 100;
mPressed = 0;
bPressed = 0;
pPressed = 0;
}
//limit max velocities (some particle collisions have chaotic results)
for (int i = 0;i < ballsAmount;++i)
{
if (balls[i].vx > 10)
balls[i].vx = 10;
if (balls[i].vy > 10)
balls[i].vy = 10;
}
//check collisions
for (int i = 0; i < ballsAmount; ++i)
{
for (int j = 0; j < ballsAmount; ++j)
{
if (j <= i)
continue;
checkCollision(ballSprites[i], ballSprites[j], balls[i], balls[j]);
}
}
//move balls (update ball positions)
if (freezeToggle == 0)
{
for (int i = 0; i < ballsAmount; ++i)
{
if (i == 0) //"0" index is player ball - has different movement if anti-grav turned on so keep separate
movePlayerball(playerSprite, balls[i], window);
else
moveBalls(ballSprites[i], balls[i], window);
}
}
else //freeze all other balls if freeze toggle = 1
{
for (int i = 1; i < ballsAmount; ++i)
{
balls[i].vx = 0;
balls[i].vy = 0;
}
movePlayerball(playerSprite, balls[0], window);
}
// E field effects
if (EfieldToggle == 1)
{
for (int i = 0; i < ballsAmount; ++i)
{
for (int j = 0; j < ballsAmount; ++j)
{
if (j <= i)
continue;
Efield(ballSprites[i], ballSprites[j], balls[i], balls[j], window);
}
}
}
//moving player
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
player.vx -= 0.1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
player.vx += 0.1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
player.vy -= 0.1 + 0.5*gcount*antigravplayer; //boost acceleration to counteract gravity
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
player.vy += 0.1;
//events, key presses, clicks, etc
sf::Event event;
while (window.pollEvent(event))
{
//close window
if (event.type == sf::Event::Closed)
window.close();
//key presses
if (event.type == sf::Event::KeyPressed)
{
//quit
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
//reset balls to centre
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
{
playerSprite.setPosition(window.getSize().x / 2, 20 );
player.vx = 0;
player.vy = 0;
for (int i = 1; i < ballsAmount;++i)
{
if (i<70)
reset(ballSprites[i], balls[i], i, window);
else
reset2(ballSprites[i], balls[i], i, window);
}
}
//gravity toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::G))
{
++gcount;
if (gcount > 5)
gcount = 0;
if (gcount == 1)//reset gravity after the '0' setting
gravity = 9.81;
gravity *= gcount;
}
//balls toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::B))
{
++ballsAmount;
if (ballsAmount > 101)
ballsAmount = 2;
//resets balls to middle (avoids new balls being drawn over each other on first creation)
playerSprite.setPosition(window.getSize().x / 2, 20);
player.vx = 0;
player.vy = 0;
for (int i = 1; i < ballsAmount;++i)
{
if(i<70)
reset(ballSprites[i], balls[i], i, window);
else
reset2(ballSprites[i], balls[i], i, window);
}
bPressed = 1;
}
//mass toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M))
{
ballmassMultiplier += 1;
if (ballmassMultiplier > 10)
ballmassMultiplier = 1;
mPressed = 1;
}
//player mass toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P))
{
playermassMultiplier += 1;
if (playermassMultiplier > 10)
playermassMultiplier = 1;
pPressed = 1;
}
// anti-grav player toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{
if (antigravplayer == 0)
antigravplayer = 1;
else
antigravplayer = 0;
}
// electric field toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
if (EfieldToggle == 0)
EfieldToggle = 1;
else
EfieldToggle = 0;
}
// freeze toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
if (freezeToggle == 0)
freezeToggle = 1;
else
freezeToggle = 0;
}
// info toggle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::I))
{
if (infoToggle == 0)
infoToggle = 1;
else
infoToggle = 0;
}
}
}
//update text (in-game info)
playermassString = "Player mass: " + std::to_string(playermassMultiplier * 100);
playermassText.setString(playermassString);
ballsnumberString = "Number of dummy balls: " + std::to_string(ballsAmount - 1);
ballsamountText.setString(ballsnumberString);
ballsmassString = "Dummy balls mass: " + std::to_string(ballmassMultiplier * 100);
ballsmassText.setString(ballsmassString);
gravString = "Gravity = " + std::to_string(gcount) + "g";
gravityText.setString(gravString);
//draw display, sprites etc...
window.clear();
window.draw(playerSprite);
//draw balls
for (int i = 1; i < ballsAmount; ++i)
{
window.draw(ballSprites[i]);
}
//draw updated text
for (int i = 0; i < 5;++i)
{
window.draw(texts[i]);
}
//draw info text if toggled on
if (infoToggle == 1)
window.draw(infoText);
//draw E-field text if toggled on
if (EfieldToggle == 1)
window.draw(EfieldText);
//draw 'anti-grav' text if toggle on
if (antigravplayer == 0)
window.draw(antigravText);
//draw frozen balls text
if (freezeToggle == 1)
window.draw(freezeText);
window.display();
}
return 0;
}