-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
172 lines (135 loc) · 5.44 KB
/
main.cpp
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
#include "raylib.h"
#include <vector>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "config.h"
std::vector<object *> universe;
float dist(vec a, vec b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
// The actual update function
void step(float dt) {
// Update each object in the universe
for (auto obj : universe) {
// Check if the object went above the screen, and make it go back
if(obj->position.y + obj->radius > screenHeight) {
obj->position.y = screenHeight - obj->radius; // reset the object to the top of the screen
// in case it went above and beyond
obj->velocity.y *= -obj->bounciness; // bounce it off the wall according to its elasticity
obj->force.y += -k * obj->velocity.y; // friction, we have f = k*v
obj->force.x += -k * obj->velocity.x;
}
// Check if the object went below the screen, and make it go back
if(obj->position.y - obj->radius < 0) {
obj->position.y = obj->radius; // Same explanation as before
obj->velocity.y *= -obj->bounciness;
obj->force.y += -k * obj->velocity.y;
obj->force.x += -k * obj->velocity.x;
}
// Check if the object went off the right of the screen
if(obj->position.x + obj->radius > screenWidth) {
obj->position.x = screenWidth - obj->radius;
obj->velocity.x *= -obj->bounciness;
obj->force.y += -k * obj->velocity.y;
obj->force.x += -k * obj->velocity.x;
}
// Check if the object went off the left of the screen
if(obj->position.x - obj->radius < 0) {
obj->position.x = obj->radius;
obj->velocity.x *= -obj->bounciness;
obj->force.y += -k * obj->velocity.y;
obj->force.x += -k * obj->velocity.x;
}
for (auto other : universe) {
if(obj == other) continue;
float distance = dist(obj->position, other->position);
if (distance < obj->radius + other->radius) {
vec n;
n.x = (other->position.x - obj->position.x) / distance;
n.y = (other->position.y - obj->position.y) / distance;
float p = 2 * (obj->velocity.x * n.x + obj->velocity.y * n.y - other->velocity.x * n.x - other->velocity.y * n.y) / (obj->mass + other->mass);
obj->velocity.x -= p * obj->mass * n.x;
obj->velocity.y -= p * obj->mass * n.y;
other->velocity.x += p * obj->mass * n.x;
other->velocity.y += p * obj->mass * n.y;
}
}
// Add the effect of gravity to the Y-force, we have: P = m*g
obj->force.y += obj->mass * gravity.y;
// Add the effect of velocity to the force
obj->force.x += obj->velocity.x * dt;
obj->force.y += obj->velocity.y * dt;
// Make the force work, changes into velocity
// we have F = m*a => a = (F / m)
obj->velocity.x += (obj->force.x / obj->mass) * dt;
obj->velocity.y += (obj->force.y / obj->mass) * dt;
// Update the position according to the current velocity
obj->position.x += obj->velocity.x * dt;
obj->position.y += obj->velocity.y * dt;
// Reset net force to 0
obj->force.x = 0;
obj->force.y = 0;
}
}
int main(void) {
// Raylib stuff
InitWindow(screenWidth, screenHeight, "physics");
SetTargetFPS(60);
//universe.push_back(&circle1);
//universe.push_back(&circle2);
//universe.push_back(&circle3);
// Initialize seed
std::srand(std::time(nullptr));
// Show info or not
bool extra_info = false;
// The update loop
while(!WindowShouldClose()) {
BeginDrawing();
// Draw current FPS
DrawFPS(10, 10);
ClearBackground(BLACK);
step(0.1); // We chose dt = 0.1 as a nice step for our update function
// it gave the most natural feeling physics out of all the
// powers of 10
// Loop over all the objects in the universe, and draw a circle at their position,
// with their radius and their color
for (auto obj : universe) {
Vector2 ballPosition = { obj->position.x, obj->position.y };
DrawCircleV(ballPosition, obj->radius, obj->color);
// Draw some info about each object (if the option is enabled)
if(extra_info) {
DrawText(("Mass: " + std::to_string(obj->mass) + "kg").c_str(), obj->position.x + obj->radius, obj->position.y - obj->radius, obj->radius / 2, obj->color);
DrawText(("Elasticity: " + std::to_string(obj->bounciness)).c_str(), obj->position.x + obj->radius, obj->position.y - 1.5 * obj->radius, obj->radius / 2, obj->color);
// Current velocity:
DrawText(("Velocity: (" + std::to_string(obj->velocity.x) + "," + std::to_string(obj->velocity.y) + ")").c_str(), obj->position.x + obj->radius, obj->position.y - 2 * obj->radius, obj->radius / 2, obj->color);
}
}
// If the left mouse button is pressed, spawn a new object randomly
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
object *new_obj = new object {
{ GetMousePosition().x, GetMousePosition().y },
{ 0, 0 },
{ 0, 0 },
rand() % 30 + 20,
rand() % 999 + 1,
((float)rand() / RAND_MAX),
(Color){rand() % 255, rand() % 255, rand() % 255, 255}
};
universe.push_back(new_obj);
std::cout << "CLICKED\n";
}
// Toggle info
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
if(extra_info) extra_info = false;
else extra_info = true;
}
EndDrawing();
}
// Free memory
for(auto obj : universe) {
delete obj;
}
return 0;
}