diff --git a/include/Application.hpp b/include/Application.hpp index 7da1055..49ff88d 100644 --- a/include/Application.hpp +++ b/include/Application.hpp @@ -21,4 +21,6 @@ class Application { sf::ContextSettings mSettings{24u, 8u, 8u, 3u, 3u}; glm::vec2 mGravitationalAcceleration{0.0f, 9.8f}; std::vector mSoftBodies; + int mGrabbedParticleIndex = -1; + int mGrabbedSoftBodyIndex = -1; }; diff --git a/src/Application.cpp b/src/Application.cpp index 9c18407..70c258c 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -63,15 +63,47 @@ void Application::Run() { case sf::Event::Closed: window.close(); break; + case sf::Event::Resized: view.setSize(static_cast(event.size.width), static_cast(event.size.height)); window.setView(view); break; + + case sf::Event::MouseButtonPressed: + if (event.mouseButton.button == sf::Mouse::Left) { + for (size_t softBodyIndex = 0; softBodyIndex < mSoftBodies.size(); ++softBodyIndex) { + const SoftBody &softBody = mSoftBodies[softBodyIndex]; + for (size_t particleIndex = 0; particleIndex < softBody.particles.size(); ++particleIndex) { + const Particle &particle = softBody.particles[particleIndex]; + const sf::Vector2f worldMousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window)); + const float distance = glm::length(glm::vec2(worldMousePosition.x - particle.position.x, worldMousePosition.y - particle.position.y)); + if (distance <= particle.radius) { + mGrabbedSoftBodyIndex = static_cast(softBodyIndex); + mGrabbedParticleIndex = static_cast(particleIndex); + break; + } + } + } + } + break; + + case sf::Event::MouseButtonReleased: + if (event.mouseButton.button == sf::Mouse::Left) { + mGrabbedSoftBodyIndex = -1; + mGrabbedParticleIndex = -1; + } + break; + default: break; } } + if (mGrabbedSoftBodyIndex != -1 && mGrabbedParticleIndex != -1) { + const sf::Vector2f worldMousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window)); + mSoftBodies[mGrabbedSoftBodyIndex].particles[mGrabbedParticleIndex].position = glm::vec2(worldMousePosition.x, worldMousePosition.y); + } + const sf::Time deltaTime = deltaClock.restart(); const sf::Time scaledDeltaTime = timeScale * deltaTime; @@ -101,7 +133,11 @@ void Application::Run() { shape.setRadius(particle.radius); shape.setOrigin(particle.radius, particle.radius); shape.setPosition(particle.position.x, particle.position.y); - shape.setFillColor(sf::Color(230, 232, 230)); + if (mGrabbedParticleIndex != -1 && &particle == &mSoftBodies[mGrabbedSoftBodyIndex].particles[mGrabbedParticleIndex]) { + shape.setFillColor(sf::Color(255, 0, 0)); + } else { + shape.setFillColor(sf::Color(230, 232, 230)); + } window.draw(shape); } } diff --git a/src/Cloth.cpp b/src/Cloth.cpp index 971beec..e68ecf7 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), 100.0f, 2.0f); + springs.emplace_back(particle1, particle2, distance(particle1, particle2), 32.0f, 1.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), 100.0f, 2.0f); + springs.emplace_back(particle1, particle2, distance(particle1, particle2), 32.0f, 1.0f); } } }