-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.cpp
75 lines (63 loc) · 2.53 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
#include <iostream>
#include <SFML/Graphics.hpp>
#include "solver.hpp"
#include "renderer.hpp"
#include "utils/number_generator.hpp"
#include "utils/math.hpp"
static sf::Color getRainbow(float t)
{
const float r = sin(t);
const float g = sin(t + 0.33f * 2.0f * Math::PI);
const float b = sin(t + 0.66f * 2.0f * Math::PI);
return {static_cast<uint8_t>(255.0f * r * r),
static_cast<uint8_t>(255.0f * g * g),
static_cast<uint8_t>(255.0f * b * b)};
}
int32_t main(int32_t, char*[])
{
// Create window
constexpr int32_t window_width = 1000;
constexpr int32_t window_height = 1000;
sf::ContextSettings settings;
settings.antialiasingLevel = 1;
sf::RenderWindow window(sf::VideoMode(window_width, window_height), "Verlet", sf::Style::Default, settings);
const uint32_t frame_rate = 60;
window.setFramerateLimit(frame_rate);
Solver solver;
Renderer renderer{window};
// Solver configuration
solver.setConstraint({static_cast<float>(window_width) * 0.5f, static_cast<float>(window_height) * 0.5f}, 450.0f);
solver.setSubStepsCount(8);
solver.setSimulationUpdateRate(frame_rate);
// Set simulation attributes
const float object_spawn_delay = 0.025f;
const float object_spawn_speed = 1200.0f;
const sf::Vector2f object_spawn_position = {500.0f, 200.0f};
const float object_min_radius = 1.0f;
const float object_max_radius = 20.0f;
const uint32_t max_objects_count = 1000;
const float max_angle = 1.0f;
sf::Clock clock;
// Main loop
while (window.isOpen()) {
sf::Event event{};
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
}
if (solver.getObjectsCount() < max_objects_count && clock.getElapsedTime().asSeconds() >= object_spawn_delay) {
clock.restart();
auto& object = solver.addObject(object_spawn_position, RNGf::getRange(object_min_radius, object_max_radius));
const float t = solver.getTime();
const float angle = max_angle * sin(t) + Math::PI * 0.5f;
solver.setObjectVelocity(object, object_spawn_speed * sf::Vector2f{cos(angle), sin(angle)});
object.color = getRainbow(t);
}
solver.update();
window.clear(sf::Color::White);
renderer.render(solver);
window.display();
}
return 0;
}