An open API service indexing awesome lists of open source software.

https://github.com/wendtpiotr/electric-field-visualizer

Interactive Physics Simulation of Electric Fields Using Unity and RK4 Integration
https://github.com/wendtpiotr/electric-field-visualizer

coulomb-law electric-field-lines physics-simulation rk4-algorithm unity3d

Last synced: 19 days ago
JSON representation

Interactive Physics Simulation of Electric Fields Using Unity and RK4 Integration

Awesome Lists containing this project

README

          

# ⚡ Electric Field Visualizer — Unity Simulation

### 🧠 *Interactive Physics Simulation of Electric Fields Using Unity and RK4 Integration*

image

https://github.com/user-attachments/assets/79c53b16-531c-4ef9-93da-2b88c3438eaf

---

## 🌍 Overview

**Electric Field Visualizer** is a **real-time 3D simulation** of electrostatic fields generated by *many* moving point charges.
It brings together **Coulomb’s Law**, **Gauss’s Law**, and **numerical integration (RK4)** to create a realistic, dynamic visualization of electric fields evolving in time and space.

Each charge contributes to the overall field, and the superposition principle ensures accurate interactions between dozens of charges simultaneously — **in real time**.

---

## 🧪 Core Concepts

### 🧲 Coulomb’s Law

Every point charge generates an electric field **E** described by:

[
\vec{E} = \frac{1}{4 \pi \varepsilon_0} \cdot \frac{q}{r^2} \hat{r}
]

Where:

* ( q ) — charge magnitude (C)
* ( \hat{r} ) — unit vector from charge to observation point
* ( \varepsilon_0 ) — permittivity of free space

The simulation adds up all contributions from each charge to compute the resultant vector field.

---

### 🔁 Field Line Tracing (Runge–Kutta 4)

To visualize electric field lines, we integrate the electric field vector field using **Runge–Kutta 4 (RK4)** — a 4th-order accurate method.

[
\vec{r}_{n+1} = \vec{r}_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)
]

This ensures:

* Smooth, continuous trajectories for field lines
* Numerical stability even near strong field gradients
* Correct directionality and convergence around opposite charges

---

### 🌀 Moving Point Charges

Unlike static field plots, this system supports **dynamically moving charges** using RK4 integration for motion as well:

* Charges can **orbit**, **rotate**, or **follow defined motion patterns** around a central point.
* Each charge’s motion continuously updates the surrounding field — creating a *time-dependent* electrostatic visualization.
* The system efficiently handles **many simultaneous charges** (tens or even hundreds, depending on setup), maintaining stable framerates through optimized computation and procedural line management.

---

### 🌊 Oscillating Charge Magnitudes

Charge strengths can smoothly **oscillate** using **Perlin noise**, simulating fluctuations in field intensity — ideal for representing pulsating or dynamic systems.

---

## 💡 Scientific Highlights

| Feature | Description |
| ----------------------------------- | ---------------------------------------------------------------- |
| ⚙️ **Numerical Integration** | RK4 method used for both motion and field tracing. |
| ⚡ **Coulomb Field Calculation** | Real-time vector summation from multiple sources. |
| 🧮 **Gauss’s Law Visualization** | Field line density proportional to net charge magnitude. |
| 🌀 **Dynamic Multi-Charge Support** | Move, rotate, or oscillate dozens of charges simultaneously. |
| 🎨 **Gradient Field Lines** | Continuous color blending between positive and negative sources. |

---

## 🧰 Technologies Used

* 🧩 **Unity Engine (C#)** — Real-time 3D rendering & simulation framework
* 🧵 **LineRenderer** — Procedural field line drawing
* 🔄 **RK4 Integration** — Stable and accurate motion and field evolution
* 🌈 **Perlin Noise** — Smooth charge oscillation modeling
* 🧮 **Coulomb + Gauss Foundations** — Field superposition and flux concepts
* 🧠 **Optimized Object Pooling** — Efficient handling of many field lines and charge markers

---

## 🚀 Getting Started

1. Clone the repository:

```bash
git clone https://github.com/pietras333/ElectricFieldVisualizer.git
```
2. Open in **Unity 2021.3+**.
3. Add the `ElectricFieldVisualizer` prefab to your scene.
4. Configure:

* 🔋 Number of charges
* 🌀 Motion (rotation/oscillation)
* ⚙️ Visual parameters (field lines per charge, color gradients)
5. Press **Play** — and watch the field come alive!

---

## 🔭 What You’ll See

* 🟥 **Positive charges** radiate outward field lines.
* 🟦 **Negative charges** attract lines inward.
* 🌀 Moving charges dynamically distort and reshape the field.
* 💫 Multiple charges interact through **superposition**, forming complex field structures and interference zones.

---

## 📘 Future Work

* ⚡ GPU-accelerated field computation (Compute Shaders)
* 🌈 Adaptive field line density for charge clusters
* 🔋 Integration with **Maxwell’s Equations** to simulate electromagnetic waves
* 🎥 Real-time export for educational or research animations

---

## 🧬 License

MIT License — Free to use, modify, and expand for physics education, visualization research, or scientific art.

---