Simulations

Discover how to create physics simulations and particle systems using P5 Svelte. These examples demonstrate various simulation techniques and mathematical concepts.

Particle System

Interactive Particle System

Click and drag to create particles affected by gravity:

FPS: 60
View Code
const particleSystemSketch: Sketch = {
  setup: (p5) => {
    p5._particles = [];
    p5._gravity = 0.1;
  },
  draw: (p5) => {
    // Particle system logic...
  }
};

Double Pendulum

Double Pendulum Simulation

A demonstration of coupled harmonic motion:

FPS: 60
View Code
const pendulumSketch: Sketch = {
  setup: (p5) => {
    p5._state = {
      r1: 125, r2: 125,
      m1: 10, m2: 10,
      a1: p5.PI/2, a2: p5.PI/2,
      // ... more initialization
    };
  },
  draw: (p5) => {
    // Pendulum physics...
  }
};

Simulation Concepts

Physics Engine

  • Force calculations
  • Velocity and acceleration
  • Collision detection

Performance

  • Optimization techniques
  • Spatial partitioning
  • Object pooling

Time Management

  • Delta time
  • Frame independent motion
  • Animation timing

Additional Resources

Next Steps

Ready to explore more advanced topics? Check out: