-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFML.cpp
More file actions
128 lines (106 loc) · 4.1 KB
/
Copy pathSFML.cpp
File metadata and controls
128 lines (106 loc) · 4.1 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
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <Windows.h>
#include <time.h>
#include "Particle.h"
using namespace std;
#define WIDTH 1200
#define HEIGHT 800
const float GRAVITY = 10.0f;
const float TIME_STEP = 0.1f;
const float PARTICLE_RADIUS = 10.0f;
const int rows = 10;
const int cols = 10;
int main()
{
//Create window
sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT}), "Physics?");
window.setFramerateLimit(144);
//Vector of particles
vector<vector<Particle>> particlesGrid(rows, vector<Particle>());
float spacing = 30.0f;
float startX = WIDTH / 2 - 5 * spacing;
float startY = 100;
//Initializing the grid
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
bool pinned = (i == 0 && j % 5 == 0);
particlesGrid[i].emplace_back(startX + j * spacing, startY + i * spacing, pinned);
}
}
vector <Constraint> constraints;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j < cols - 1) {
constraints.emplace_back(&particlesGrid[i][j], &particlesGrid[i][j + 1]);
}
if (i < rows - 1) {
constraints.emplace_back(&particlesGrid[i][j], &particlesGrid[i + 1][j]);
}
}
}
sf::Vector2f force = { 0,GRAVITY};
while (window.isOpen())
{
window.clear();
while (const std::optional event = window.pollEvent())
{
if (event->is<sf::Event::Closed>())
window.close();
if (event->is<sf::Event::MouseButtonPressed>()) {
auto mouseEvent = event->getIf<sf::Event::MouseButtonPressed>();
sf::Vector2f mousePos(mouseEvent->position.x, mouseEvent->position.y);
for (int i = 0;i < rows;i++) {
for (int j = 0; j < cols;j++) {
float distance = hypot(
particlesGrid[i][j].position.x - mousePos.x,
particlesGrid[i][j].position.y - mousePos.y
);
if (distance <= PARTICLE_RADIUS) {
if (mouseEvent->button == sf::Mouse::Button::Left) {
particlesGrid[i][j].pinned = true;
particlesGrid[i][j].pinPosition = particlesGrid[i][j].position;
}
else if (mouseEvent->button == sf::Mouse::Button::Right)
particlesGrid[i][j].pinned = false;
break;
}
}
}
}
}
//satisfy the constraints
for (auto& constraint : constraints) {
constraint.satisfy();
}
//drawing the lines
for (auto& constraint : constraints) {
sf::Vertex line[2]= {
sf::Vertex(constraint.p1->position,sf::Color::White),
sf::Vertex(constraint.p2->position,sf::Color::White),
};
window.draw(line , 2, sf::PrimitiveType::Lines);
}
//applying gravity
for (int i = 0;i < rows;i++) {
for (int j = 0; j < cols;j++) {
particlesGrid[i][j].apply_force(force);
particlesGrid[i][j].update(TIME_STEP);
particlesGrid[i][j].inBounds(WIDTH, HEIGHT, PARTICLE_RADIUS);
}
}
//Drawing the particles
for (int i = 0;i < rows;i++) {
for (int j = 0; j < cols;j++) {
sf::CircleShape circle(PARTICLE_RADIUS);
particlesGrid[i][j].pinned ? circle.setFillColor(sf::Color::Red) : circle.setFillColor(sf::Color::White);
circle.setOutlineColor(sf::Color::Blue);
circle.setOrigin({ PARTICLE_RADIUS, PARTICLE_RADIUS });
circle.setPosition(particlesGrid[i][j].position);
window.draw(circle);
}
}
window.display();
}
}