Skip to content

Commit

Permalink
Allow particles to be grabbed
Browse files Browse the repository at this point in the history
  • Loading branch information
xorz57 committed Jan 27, 2024
1 parent d71de6d commit e3d7e6c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ class Application {
sf::ContextSettings mSettings{24u, 8u, 8u, 3u, 3u};
glm::vec2 mGravitationalAcceleration{0.0f, 9.8f};
std::vector<SoftBody> mSoftBodies;
int mGrabbedParticleIndex = -1;
int mGrabbedSoftBodyIndex = -1;
};
38 changes: 37 additions & 1 deletion src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,47 @@ void Application::Run() {
case sf::Event::Closed:
window.close();
break;

case sf::Event::Resized:
view.setSize(static_cast<float>(event.size.width), static_cast<float>(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<int>(softBodyIndex);
mGrabbedParticleIndex = static_cast<int>(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;

Expand Down Expand Up @@ -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);
}
}
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), 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);
}
}
}
Expand Down

0 comments on commit e3d7e6c

Please sign in to comment.