void FluidSim::InitialiseParticles(void) { KernelPow9 = glm::pow(SmoothingKernelSize, 9.0f); KernelPow6 = glm::pow(SmoothingKernelSize, 6.0f); KernelPow2 = glm::pow(SmoothingKernelSize, 2.0f); float pi = glm::pi<float>(); DefaultKernelConstant = 315.0f / (64.0f * pi * KernelPow9); NormalKernelConstant = -945.0f / (32.0f * pi * KernelPow9); SurfaceKernelConstant = NormalKernelConstant; PressureKernelConstant = -45.0f / (pi * KernelPow6); ViscosityKernelConstant = 45.0f / (pi * KernelPow6); particles.reserve(ParticleCount); for(int x = 0; x < ParticlesPerDimension; x++) for(int y = 0; y < ParticlesPerDimension; y++) for(int z = 0; z < ParticlesPerDimension; z++) { Particle* particle = new Particle(ParticleMass); particle->SetPosition(GetRandomValue(-HorizontalBounds, HorizontalBounds), GetRandomValue(0.0f, HorizontalBounds), GetRandomValue(-HorizontalBounds, HorizontalBounds)); //particle->SetPosition(0.0f, 0.0f, 0.0f); particle->SetVelocity(0.0f, 0.0f, 0.0f); particles.push_back(particle); } container = Capsule(); container.radius = HorizontalBounds; container.length = HorizontalBounds; container.centre = glm::vec3(); innerContainer = Capsule(); innerRadius = HorizontalBounds / 2.0f; innerLength = HorizontalBounds / 2.0f; innerContainer.radius = innerRadius; innerContainer.length = innerLength; innerContainer.centre = glm::vec3(); container.CalculateCapsulePosition(glm::vec3()); innerContainer.CalculateCapsulePosition(glm::vec3()); crashCapsule = brokenCapsule = showNeighbours = showSurfaceParticles = hideSurfaceParticles = false; particleHighlightID = 0; wallEnabled = true; }
int main() { srand(time(NULL)); sf::RenderWindow window(sf::VideoMode(800, 600), "Collisions"); CollidersContainer Container; Particle A; A.SetMass(10); A.SetRadius(5); A.SetPosition(10, 10); A.SetActingForce(0, 0); sf::Clock clock; clock.restart(); //Creating Particles //N2 - 156 | O2 - 42 | Ar - 2 | All - 200 //Nitrogen Particles //Mass - 4.652 × 10^-26 kg //Radius - 112 * 10^-12 m A.SetMass(4.652 * pow(10, -26)); A.SetRadius(5.6); for (int i = 0; i < 156; i++) { //Setting random values to Velocity and Position double RandomVelocityX = rand() % 1000 + 1000; double RandomVelocityY = rand() % 1000 + 1000; if (rand() % 2 == 0){ RandomVelocityX *= -1; } if (rand() % 2 == 0){ RandomVelocityY *= -1; } A.SetVelocity(RandomVelocityX, RandomVelocityY); A.SetPosition((rand()) % 800 + 1, (rand()) % 600 + 1); //Creating Particle from template A Container.CreateParticle(A); } //Oxygen Particles //Mass - 5.314 × 10^-26 kg //Radius - 96 * 10^-12 m A.SetMass(5.314 * pow(10, -26)); A.SetRadius(4.8); for (int i = 0; i < 42; i++) { //Setting random values to Velocity and Position double RandomVelocityX = rand() % 1000 + 1000; double RandomVelocityY = rand() % 1000 + 1000; if (rand() % 2 == 0){ RandomVelocityX *= -1; } if (rand() % 2 == 0){ RandomVelocityY *= -1; } A.SetVelocity(RandomVelocityX, RandomVelocityY); A.SetPosition((rand()) % 800 + 1, (rand()) % 600 + 1); //Creating Particle from template A Container.CreateParticle(A); } //Argon Particles //Mass - 6.642 × 10^-26 //Radius - 71 * 10^-12 m A.SetMass(6.642 * pow(10, -26)); A.SetRadius(3.55); for (int i = 0; i < 2; i++) { //Setting random values to Velocity and Position double RandomVelocityX = rand() % 1000 + 1000; double RandomVelocityY = rand() % 1000 + 1000; if (rand() % 2 == 0){ RandomVelocityX *= -1; } if (rand() % 2 == 0){ RandomVelocityY *= -1; } A.SetVelocity(RandomVelocityX, RandomVelocityY); A.SetPosition((rand()) % 800 + 1, (rand()) % 600 + 1); //Creating Particle from template A Container.CreateParticle(A); } while (window.isOpen()) { sf::Time T = clock.getElapsedTime(); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(); Container.MoveParticles(0.0015); Container.SimulateCollisions(); Container.DeflectParticlesOutOfBorder(); for (unsigned int i = 0; i < Container.ParticlesQuantity(); i++) { sf::CircleShape shape(Container.GetParticleRadius(i)); shape.setPosition(Container.GetParticlePositionX(i), Container.GetParticlePositionY(i)); switch ((int)Container.GetParticleRadius(i)) { case 5: shape.setFillColor(sf::Color::Blue); break; case 4: shape.setFillColor(sf::Color::White); break; default: shape.setFillColor(sf::Color::Magenta); break; } window.draw(shape); } while (T.asSeconds() < 0.045){ T = clock.getElapsedTime(); } window.display(); clock.restart(); } return 0; }