A lightweight crowd simulation project built in Unity, implementing a Magnetic Force Model (based on Coulomb's Law). The project is optimized for Mobile VR, focusing on maintaining high frame rates on mobile hardware while simulating moderate crowd sizes.
- Magnetic Force implementation: Agents act as "charged" particles, naturally avoiding each other and obstacles through magnetic-like repulsion.
- Coulomb's Law based physics: Uses inverse-square law forces to manage agent spacing and collision avoidance.
- Mobile VR Optimized: Specifically tuned for devices like Google VR, ensuring smooth performance even with 200+ agents.
- Intelligent Obstacle Avoidance: Agents dynamically steer around static and dynamic obstacles using calculated repulsive potential fields.
- Path-based Navigation: Agents follow a sequence of randomly assigned destinations to create realistic movement patterns.
- Dynamic Animation Controller: Character animations (walk speed, leg movements) are synced with the agent's physical velocity.
This simulation is designed to balance physical realism with mobile hardware constraints:
- Benchmark: Achieves smooth performance with up to 200 agents on mobile hardware (e.g., Poco F1) without noticeable delay.
-
Complexity: The force calculations run at
$O(N^2)$ for agent-agent interactions and$O(N \times M)$ for obstacles. - Mobile Efficiency: Performance is achieved through efficient C# vector math and localized force calculations.
The simulation treats agents as particles with "magnetic charges" (
- Desired Force (Attraction): An attractive force pulling the agent toward its current destination at a controlled speed.
-
Magnetic Repulsion (Agents): Based on Coulomb's Law (
$F = k \frac{Q^2}{r^2}$ ), this force prevents agents from colliding. The variableskandQin the code define the strength and range of the repulsion. - Obstacle Repulsion: A specialized force that calculates the closest point on any obstacle and applies a magnetic push away from it, ensuring agents stay within navigable paths.
Agent.cs: Contains the core math for the inverse-square law force calculations and velocity integration.Manager.cs: Handles spawning, destination assignment, and global simulation parameters.Obstacle.cs: Provides proximity and collision data for the agent's force calculations.
| Overview | Closer Look |
|---|---|
![]() |
![]() |
- Unity 2018.x or later (Recommended for compatibility with Google VR assets)
- Android/iOS SDK (For mobile deployment)
- Google VR SDK (Included in project)
- Clone the repository:
git clone https://github.com/ntoulasm/crowd-simulation-unity.git
- Open the
Unityfolder project in Unity Hub. - Open the main scene in
Assets/Scenes. - Press Play to see the simulation in action.
For more detailed information, check the docs folder which contains:
CrowdSimulationForMobileVR.docx: Full technical report explaining the magnetic field approach.CrowdSimulationForMobileVR.pptx: Presentation slides.

