-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (84 loc) · 2.17 KB
/
main.cpp
File metadata and controls
105 lines (84 loc) · 2.17 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
#define SDL_MAIN_HANDLED
#include "Memake/Memake.h"
#include <math.h>
// RAII - Testing
#include "Memake/Timer.h"
Memake mmk(1500, 1000, "memake");
using namespace std;
class Ball {
public:
Ball(){}
int x, y, r;
int dx = 1, dy = 1;
Ball(int _x, int _y, int _r) {
x = _x;
y = _y;
r = _r;
}
void update() {
draw();
checkClamp();
move();
}
void draw() {
mmk.drawCircle(x, y, r, Colmake.beige);
}
void checkCollision(Ball &b) {
int distX = x - b.x;
int distY = y - b.y;
float distance = sqrt((distX * distX) + (distY * distY));
if (distance < r + b.r) {
if (random(0,1)) {
dy *= -1;
} else {
dx *= -1;
}
}
}
void move() {
x += dx;
y += dy;
}
void checkClamp() {
if (x <= 0 || x >= mmk.getScreenW()) {
if (random(0,1)) {
dy *= -1;
} else {
dx *= -1;
}
}
if (y <= 0 || y >= mmk.getScreenH()) {
if (random(0,1)) {
dy *= -1;
} else {
dx *= -1;
}
}
}
};
int main()
{
int numOfBall = 2000;
Ball b[numOfBall];
// generate ball to total X numOfBall
for (int i = 0; i < numOfBall; i++) {
b[i] = Ball(random(0, mmk.getScreenW()), random(0, mmk.getScreenH()), 2);
}
// animate ball
mmk.update([&](){
{
Timer timer;
for (int i = 0; i < numOfBall; i++) {
for (int j = 0; j < numOfBall; j++) {
// check collision, but don't check collision to itself
if (j != i) {
b[i].checkCollision(b[j]);
}
}
b[i].update();
}
}
cout << "one frame has ended" << endl << endl;
});
return 0;
}