Particle system dynamics Jiří Chmelík, Marek Trtík PA199 ►Motivation ► ►Motion of a single particle: Equations of motion ►Use of an ODE solver ► ►Motion of many particles ► ►Forces ►Gravity, drag, spring, local interaction ► ►Collision: particle vs. plane ►Detection, response, simple friction Outline Motivation 3 https://experiments.withgoogle.com/fluid-particles https://en.wikipedia.org/wiki/Particle_system Particle definition 4 Particle equations of motion 5 Solving equations of motion 6 Solving equations of motion 7 void getState(Particle const& p, std::vector& y0) { y0.push_back(p.position.x); y0.push_back(p.position.y); y0.push_back(p.position.z); y0.push_back(p.velocity.x); y0.push_back(p.velocity.y); y0.push_back(p.velocity.z); } Building initial state for ODE 8 Building derivatives for ODE 9 void doSimulationStep(Particle& p, float& t, float const dt) { UpdateForce(p,t,dt); // Applies external forces and impulses. std::vector y0, y; std::vector Fyt; getState(p, y0); getDerivative(p, Fyt); ODE(y0, Fyt, t, dt, y); // Computes y and updates t (t += dt). setState(p, y.begin()); } Simulation step for single particle 10 void setState(Particle& p, std::vector::const_iterator& it) { p.position.x = *it; ++it; p.position.y = *it; ++it; p.position.z = *it; ++it; p.velocity.x = *it; ++it; p.velocity.y = *it; ++it; p.velocity.z = *it; ++it; } Saving ODE results 11 Data flow in simulation step 12 p.position p.velocity p.position p.velocity p.force / p.mass p.velocity Particle system 13 void getState(ParticleSystem const& ps, std::vector& y0) { for (Particle const& p : ps) getState(p,y0); } void getDerivative(ParticleSystem const& ps, std::vector& Fyt) { for (Particle const& p : ps) getDerivatives(p, Fyt); } void setState(ParticleSystem& ps, std::vector::const_iterator& it) { for (Particle& p : ps) setState(p, it); } ODE helper functions 14 void doSimulationStep(ParticleSystem& ps, float& t, float const dt) { UpdateForce(ps,t,dt); // Applies external forces and impulses. std::vector y0, y; std::vector Fyt; getState(ps, y0); getDerivative(ps, Fyt); ODE(y0, Fyt, t, dt, y); // Computes y and updates t (t += dt). setState(ps, y.begin()); } Simulation step for whole system 15 Data flow in simulation step 16 ps[0] ps[1] ps[2] … void UpdateForce(ParticleSystem& ps, float const t, float const dt) { clearForce(ps); applyForce(ps,t,dt); // Add all forces and impulses to all particles. } void clearForce(ParticleSystem& ps) { for (Particle& p : ps) p.force = Vector3(0,0,0); } ►Next we discuss what forces we can add to particles inside the function applyForce(). Forces 17 Gravity 18 Viscous Drag 19 Spring 20 Local interaction 21 https://experiments.withgoogle.com/fluid-particles ►We often want particles to collide with the ground or a wall. These boundaries can be approximated by planes. ► ► ► ► ► ► ► ►The process consists of two parts: ►Detection of a collision. ►Response to the collision. ► ► Collision: particle vs. plane 22 https://github.com/LakshithaMadushan/Unity-Particle-System Collision detection 23 Collision response 24 Simple friction 25 ►We defined particle and particle system. ► ►We learned Newton’s equations of motion for a particle, i.e., a system of 1st order ODEs. ► ►We learned how to use ODE solver for the simulation. ► ►We learned several kinds of forces which we can apply to particles. ► ►We know how to compute and respond to collision of a particle with a plane, including application of a friction force. Summary 26 ►[1] Andrew Witkin; Physically Based Modeling: Principles and Practice Particle System Dynamics; Robotics Institute, Carnegie Mellon University, 1997. References 27