Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (157 loc) · 5.96 KB
/
Copy pathmain.cpp
File metadata and controls
192 lines (157 loc) · 5.96 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
#include <SFML/Graphics.hpp>
#include "Forces3.h"
#include <iostream>
void splash(sf::Color& color) {
float time = 0.f;
float halfDuration = 1 / 2.f;
sf::Color oldColor = color;
sf::Clock clock;
while (time < 1) {
time = clock.getElapsedTime().asSeconds();
float ratio = pow(halfDuration,5 * time); //time * halfDuration;
//if (ratio > 1.f)
// ratio = 2.f - ratio;
sf::Color lerpedColor(
50 * ratio,
50 * ratio,
50 * ratio
);
color = oldColor + lerpedColor;
}
color = oldColor;
}
int main() {
size_t width = 1200, height = 800;
std::string str = "Forces 3";
unsigned int followed = 0;
float speed = 1;
bool follow = false;
bool moving = false;
bool running = true;
sf::Vector2f oldpos;
float zoom = 1.f;
sf::RenderWindow window(sf::VideoMode(width, height), str);
sf::View mainView = window.getDefaultView();
const sf::Color background(20, 5, 35);
sf::Color color = background;
sf::Font font;
sf::Text text;
font.loadFromFile("C:/Windows/Fonts/Calibri.ttf");
text.setFont(font);
text.setCharacterSize(24);
text.setFillColor(sf::Color::White);
Space v;
v.addBody(Body({ 300,300 }, Vector2f(0.2, 0), Vector2f(0, 0), 15000, 1, sf::Color::Green));
v.addBody(Body({ 300,300 - 110 - 20 }, Vector2f(7, 0), Vector2f(0, 0), 70, 1, sf::Color::Cyan));
v.addBody(Body({ 300,300 - 110 }, Vector2f(7, 0), Vector2f(0, 0), 100, 1, sf::Color::Blue));
v.addBody(Body({ 300,300 - 110 + 7 }, Vector2f(10.2, 0), Vector2f(0, 0), 15, 1, sf::Color::Green));
Thread t(splash, std::ref(color));
Thread calc([&v, &window, &speed, &running]() {
while (window.isOpen()) {
if (running) {
v.calculateForces();
v.update();
sleep(milliseconds(10 / speed));
}
}
});
calc.launch();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed: {
window.close();
} break;
case sf::Event::MouseButtonPressed: {
if (event.key.code == Mouse::Left) {
oldpos = window.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
moving = true;
follow = false;
}
else if (event.key.code == Mouse::Right) {
t.terminate();
t.launch();
color = background;
}
} break;
case sf::Event::MouseButtonReleased: {
if (event.key.code == Mouse::Left) {
moving = false;
}
} break;
case sf::Event::MouseMoved: {
if (!moving) break;
sf::Vector2f newpos = window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y));
sf::Vector2f dpos = oldpos - newpos;
mainView.setCenter(mainView.getCenter() + dpos);
window.setView(mainView);
oldpos = window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y));
} break;
case sf::Event::MouseWheelScrolled: {
if (event.mouseWheelScroll.delta <= -1)
zoom += .1f;
else if (event.mouseWheelScroll.delta >= 1)
zoom = std::max(0.1f, zoom - .1f);
mainView.setSize(window.getDefaultView().getSize());
mainView.zoom(zoom);
window.setView(mainView);
} break;
case sf::Event::KeyPressed: {
if (event.key.code == sf::Keyboard::Space) {
running = !running;
}
else if (event.key.code >= sf::Keyboard::Numpad0 && event.key.code <= sf::Keyboard::Numpad9) {
if (event.key.code - sf::Keyboard::Numpad0 < v.size()) {
followed = event.key.code - sf::Keyboard::Numpad0;
follow = true;
}
}
else if (event.key.code == sf::Keyboard::Add) {
speed *= 2;
}
else if (event.key.code == sf::Keyboard::Subtract) {
speed /= 2;
}
} break;
}
}
if (follow) {
Body b = v[followed];
mainView.setCenter(b.getPosition() + b.getRadius());
}
std::string status =
(follow ? "following" : "not following") +
std::string("\nfollowed body: ") +
std::to_string(followed) +
std::string("\ncoords: ") +
std::to_string(v[followed].getPosition().x) + "\t" + std::to_string(v[followed].getPosition().y) +
std::string("\nvelocity: ") +
std::to_string(getLength(v[followed].getVelocity())) +
std::string("\nsim speed: ") +
std::to_string(speed);
text.setString(status);
window.clear(color);
window.setView(mainView);
v.printAll(&window);
window.setView(window.getDefaultView());
if (follow) {
sf::Vertex velocity[]{
sf::Vertex(window.getDefaultView().getCenter()),
sf::Vertex(window.getDefaultView().getCenter() + v[followed].getVelocity() * 10.f)
};
sf::Vertex acceleration[]{
sf::Vertex(window.getDefaultView().getCenter()),
sf::Vertex(window.getDefaultView().getCenter() + v[followed].getAcceleration() * 100.f)
};
window.draw(velocity, 2, sf::Lines);
window.draw(acceleration, 2, sf::Lines);
}
window.draw(text);
window.display();
window.setView(mainView);
}
return 0;
}