An interactive magnetic field simulator where you can drag magnets around and watch particles react in real-time. Also includes a 3D dipole field visualizer.
- Drag & Drop Magnets: Place North, South, Dipole, or Quadrupole magnets and drag them around the canvas
- Live Particle Physics: Hundreds of floating particles respond to magnetic fields in real-time
- Curved Field Lines: Flowing field lines trace from north to south poles
- Field Arrow Grid: Directional arrows show field strength and direction everywhere
- Per-Magnet Controls: Adjust individual magnet strength and dipole rotation
- Global Controls: Particle count, field strength, speed, friction sliders
- Display Toggles: Field lines, particle trails, glow effects, grid overlay, polarity coloring
- Keyboard Shortcuts: 1-4 to add magnets, Del to remove, G/F/T for display toggles
- 3D Dipole Viewer: Plotly-based 3D quiver and isosurface visualization
- Python 3.7+
- numpy (only for
--3dmode) - plotly (only for
--3dmode)
pip install -r requirements.txtpython main.pyOpens a full interactive simulator in your browser — no dependencies needed beyond Python.
python main.py --3dRequires numpy and plotly. Generates a 3D quiver + isosurface visualization.
| Action | How |
|---|---|
| Add a magnet | Click sidebar buttons or press 1-4 |
| Move a magnet | Click and drag it |
| Place a dipole | Double-click the canvas |
| Rotate a dipole | Right-click on it |
| Remove a magnet | Select it + press Del, or click × in sidebar |
| Adjust strength | Select a magnet, use the strength slider |
| Toggle field lines | Press F or use sidebar toggle |
| Toggle trails | Press T or use sidebar toggle |
| Toggle grid | Press G or use sidebar toggle |
| Clear all | Press C or click Clear All button |
MagnetProject/
├── main.py # Launcher script
├── magnetic_field.html # Interactive simulator (standalone HTML/JS)
├── requirements.txt # Python dependencies
├── README.md # This file
└── .github/
└── copilot-instructions.md
The interactive simulator uses:
- HTML5 Canvas for high-performance 2D rendering at 60fps
- Coulomb-like force model for magnetic pole interactions
- Particle physics with velocity, friction, and Brownian motion
- Field line tracing from north poles to south poles
The 3D viewer uses the dipole equation:
B = (1/r³) × [3(m·r̂)r̂ - m]
This project is open source.