-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave_atom_2d.cpp
More file actions
235 lines (201 loc) · 6.26 KB
/
wave_atom_2d.cpp
File metadata and controls
235 lines (201 loc) · 6.26 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
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <vector>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace glm;
using namespace std;
// --- variables ---
float orbitDistance = 200.0f;
// --- engine ---
vec2 mouseWorld(0.0f);
struct Engine {
GLFWwindow* window;
int WIDTH = 800, HEIGHT = 600;
Engine () {
// --- Init GLFW ---
if (!glfwInit()) {
cerr << "failed to init glfw, LOL";
exit(EXIT_FAILURE);
}
// --- Create Window ---
window = glfwCreateWindow(WIDTH, HEIGHT, "2D atom sim by andrebuilds", nullptr, nullptr);
if (!window) {
cerr << "failed to create window, LOLOLOL";
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
int fbWidth, fbHeight;
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
glViewport(0, 0, fbWidth, fbHeight);
}
void run() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// set origin to centre
double halfWidth = WIDTH / 2.0f, halfHeight = HEIGHT / 2.0f;
glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
};
Engine engine;
// --- particles ---
struct Particle {
vec2 pos;
int charge;
float angle = M_PI;
float energy = -13.6f;
float numOscillations = 1.0f;
int n = 1;
float excitedTimer = 0.0f;
Particle(vec2 pos, int charge) : pos(pos), charge(charge) {}
void draw (vec2 centre, int segments = 50) {
// --- draw outline ---
if (charge == -1) {
segments = 5000;
glLineWidth(0.4f);
glBegin(GL_LINE_LOOP);
glColor3f(0.4f, 0.4f, 0.4f);
float numOsolations = numOscillations;
float baseOrbit = orbitDistance;
float amplitude = 50.0f;
for (int i = 0; i <= segments; i++) {
float loop_angle = 2.0f * M_PI * i / segments;
float r = baseOrbit + amplitude * sin(numOsolations * loop_angle);
float x = cos(loop_angle) * r;
float y = sin(loop_angle) * r;
glVertex2f(x + centre.x, y + centre.y);
}
glEnd();
}
// --- draw particles ---
float r;
if (charge == -1) { r = 10; glColor3f(0.0f, 1.0f, 1.0f); }
else if (charge == 1) { r = 50; glColor3f(1.0f, 0.0f, 0.0f); }
else { r = 10; glColor3f(0.5f, 0.5f, 0.5f); }
glBegin(GL_TRIANGLE_FAN);
glVertex2f(pos.x, pos.y);
for (int i = 0; i <= segments; i++) {
float angle = 2.0f * M_PI * i/segments;
float x = cos(angle) * r;
float y = sin(angle) * r;
glVertex2f(x + pos.x, y + pos.y);
}
glEnd();
}
void update (vec2 c) {
// --- set radius with oscillation ---
if (energy > -0.01f) energy = -0.01f;
numOscillations = -13.6f / energy;
float baseOrbit = orbitDistance;
float amplitude = 50.0f;
float r = baseOrbit + amplitude * sin(numOscillations * angle);
angle += 0.05;
// --- update pos with new angle and radius ---
pos = vec2( cos(angle) * r + c.x,
sin(angle) * r + c.y
);
}
};
// --- atom struct ---
struct Atom {
vec2 pos;
vec2 v = vec2(0.0f);
vector<Particle> particles = { };
Atom(vec2 p) : pos(p) {
particles.emplace_back(pos, 1); // proton
particles.emplace_back(vec2(pos.x - orbitDistance, pos.y), -1); // electron
}
};
vector<Atom> atoms {
Atom(vec2(0.0f, 0.0f))
};
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS || action == GLFW_REPEAT)
{
if (key == GLFW_KEY_W)
{
for (Atom &a : atoms) {
for (Particle &p : a.particles) {
p.energy += 0.01f;
p.angle = 0;
cout << "Particle energy: " << p.energy << endl;
}
}
}
else if (key == GLFW_KEY_S)
{
for (Atom &a : atoms) {
for (Particle &p : a.particles) {
p.energy -= 0.01f;
cout << "Particle energy: " << p.energy << endl;
}
}
}
if (key == GLFW_KEY_E)
{
for (Atom &a : atoms) {
for (Particle &p : a.particles) {
p.energy += 0.1f;
cout << "Particle energy: " << p.energy << endl;
}
}
}
else if (key == GLFW_KEY_D)
{
for (Atom &a : atoms) {
for (Particle &p : a.particles) {
p.energy -= 0.1f;
cout << "Particle energy: " << p.energy << endl;
}
}
}
if (key == GLFW_KEY_R)
{
for (Atom &a : atoms) {
for (Particle &p : a.particles) {
p.energy += 1.0f;
cout << "Particle energy: " << p.energy << endl;
}
}
}
else if (key == GLFW_KEY_F)
{
for (Atom &a : atoms) {
for (Particle &p : a.particles) {
p.energy -= 1.0f;
cout << "Particle energy: " << p.energy << endl;
}
}
}
}
}
// --- main ---
int main () {
glfwSetKeyCallback(engine.window, key_callback);
while (!glfwWindowShouldClose(engine.window)) {
engine.run();
// --- Draw Particles ---
for (Atom &a : atoms) {
for (Particle &p : a.particles) {
p.draw(a.pos);
if (p.charge == -1)
p.update(a.pos);
}
}
glfwSwapBuffers(engine.window);
glfwPollEvents();
}
}