diff --git a/include/Application.hpp b/include/Application.hpp index f048b89..e444bd0 100644 --- a/include/Application.hpp +++ b/include/Application.hpp @@ -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 mSoftBodies; + glm::vec2 mGravitationalAcceleration{0.0f, 9.8f}; bool mIsSoftBodySelected{false}; bool mIsParticleSelected{false}; size_t mSelectedSoftBodyIndex{0}; diff --git a/src/Application.cpp b/src/Application.cpp index ea16fca..1c68c6c 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -4,6 +4,7 @@ #include "Spring.hpp" #include +#include #include #include @@ -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; @@ -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); diff --git a/src/Cloth.cpp b/src/Cloth.cpp index 2e11109..cd73a03 100644 --- a/src/Cloth.cpp +++ b/src/Cloth.cpp @@ -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); } } }