Skip to content

Commit

Permalink
Use convex shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
xorz57 committed Jan 30, 2024
1 parent 4d9790d commit 8644eb8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Application {
sf::ContextSettings mSettings{24u, 8u, 8u, 3u, 3u};
sf::RenderWindow mWindow;
sf::View mView;
glm::vec2 mGravitationalAcceleration{0.0f, 9.8f};
std::vector<SoftBody> mSoftBodies;
glm::vec2 mGravitationalAcceleration{0.0f, 9.8f};
bool mIsSoftBodySelected{false};
bool mIsParticleSelected{false};
size_t mSelectedSoftBodyIndex{0};
Expand Down
36 changes: 29 additions & 7 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Spring.hpp"

#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/ConvexShape.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Window/Event.hpp>

Expand All @@ -15,20 +16,20 @@

Application::Application() {
mWindow.create(mMode, mTitle, mStyle, mSettings);
mWindow.setFramerateLimit(120u);
mWindow.setFramerateLimit(240u);
mView = mWindow.getDefaultView();
}

const unsigned int rows = 16u;
const unsigned int cols = 16u;
const float padding = 16.0f;
void Application::Run() {
const unsigned int rows = 32u;
const unsigned int cols = 32u;
const float padding = 8.0f;
const glm::vec2 position = 0.5f * glm::vec2(mMode.width, mMode.height) - 0.5f * padding * glm::vec2(cols - 1, rows - 1);
mSoftBodies.emplace_back(Cloth(position, rows, cols, padding));
}

void Application::Run() {
(void) ImGui::SFML::Init(mWindow);

const sf::Time fixedDeltaTime = sf::seconds(1.0f / 64.0f);
const sf::Time fixedDeltaTime = sf::seconds(1.0f / 128.0f);
float timeScale = 5.0f;
sf::Time accumulator = sf::Time::Zero;
sf::Clock deltaClock;
Expand Down Expand Up @@ -70,6 +71,27 @@ void Application::Run() {

mWindow.clear(sf::Color(25, 25, 25));

for (const SoftBody &softBody: mSoftBodies) {
sf::VertexArray clothSurface(sf::Triangles);
for (unsigned int i = 0; i < rows - 1; ++i) {
for (unsigned int j = 0; j < cols - 1; ++j) {
const unsigned int index1 = i * cols + j;
const unsigned int index2 = (i + 1) * cols + j;
const unsigned int index3 = i * cols + (j + 1);
const unsigned int index4 = (i + 1) * cols + (j + 1);
const sf::Color color(80u, 80u, 80u);
clothSurface.append(sf::Vertex(sf::Vector2f(softBody.particles[index1].position.x, softBody.particles[index1].position.y), color));
clothSurface.append(sf::Vertex(sf::Vector2f(softBody.particles[index2].position.x, softBody.particles[index2].position.y), color));
clothSurface.append(sf::Vertex(sf::Vector2f(softBody.particles[index3].position.x, softBody.particles[index3].position.y), color));

clothSurface.append(sf::Vertex(sf::Vector2f(softBody.particles[index2].position.x, softBody.particles[index2].position.y), color));
clothSurface.append(sf::Vertex(sf::Vector2f(softBody.particles[index4].position.x, softBody.particles[index4].position.y), color));
clothSurface.append(sf::Vertex(sf::Vector2f(softBody.particles[index3].position.x, softBody.particles[index3].position.y), color));
}
}
mWindow.draw(clothSurface);
}

for (const SoftBody &softBody: mSoftBodies) {
for (const Spring &spring: softBody.springs) {
sf::VertexArray line(sf::Lines, 2);
Expand Down
4 changes: 2 additions & 2 deletions src/Cloth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ Cloth::Cloth(const glm::vec2 &position, unsigned int rows, unsigned int cols, fl
if (col < cols - 1) {
Particle &particle1 = particles[row * cols + col];
Particle &particle2 = particles[row * cols + col + 1];
springs.emplace_back(particle1, particle2, distance(particle1, particle2), 128.0f, 1.0f);
springs.emplace_back(particle1, particle2, distance(particle1, particle2), 256.0f, 2.0f);
}
if (row < rows - 1) {
Particle &particle1 = particles[row * cols + col];
Particle &particle2 = particles[(row + 1) * cols + col];
springs.emplace_back(particle1, particle2, distance(particle1, particle2), 128.0f, 1.0f);
springs.emplace_back(particle1, particle2, distance(particle1, particle2), 256.0f, 2.0f);
}
}
}
Expand Down

0 comments on commit 8644eb8

Please sign in to comment.