How hanson-sun/particle-physics-engine works
Project Type & Purpose
• This project is a lightweight 2D particle physics engine written in vanilla JavaScript. It targets web browsers as its primary platform and provides physics simulation using particles, constraints, and static walls. The engine supports various interactive demos (e.g., cloth simulation, Newton’s Cradle, soft body, etc.) and emphasizes a modular design for handling behaviors, physics solving, rendering (using HTML canvas), and input processing.
• Key functionalities include particle-based fluid simulations using SPH, efficient spatial interactions through spatial hashing, and a semi-implicit Euler (predictor-corrector) integration method for stability.
File Structure
• The file tree highlights several modules:
• The src folder further splits into key architectural modules such as:
• behaviors – containing code that defines particle-specific actions (self and near behaviors).
• constraints – enforcing restrictions on particle movement (e.g., distance, rigid group constraints).
• core – the simulation’s backbone including the Solver (handles the physics calculations), Particle, World, SpatialHashGrid, and HashGridItem.
• renderers – responsible for visualizing the simulation via default 2D canvas-based rendering.
• utils – provides utility code such as InputHandler and Vector2D operations.
• walls – encapsulates classes defining static boundaries against which particles collide.
• There are also folders for old_src and modules, indicating previous iterations or modular bundling which may provide additional context on adaptation or legacy design.
• The README confirms the structure by listing the key modules (pphys.behaviors, pphys.constraints, pphys.core, pphys.renderers, pphys.utils, pphys.walls) and explains their roles.
Elements
• Main Components:
• World & Solver: Show the “world” as the simulation environment where particles, constraints, and walls coexist. The Solver is responsible for doing the physics calculations, applying behaviors, constraints, and updating particle positions.
• Particles: Represent the fundamental units that are manipulated by behaviors.
• Constraints: Display the set of rules (e.g., ForceDistanceConstraint, PositionPivotConstraint) that restrict particle movement.
• Behaviors: Include both self and near behaviors (Gravity, Drag, Pressure, etc.) that affect individual or nearby particles.
• Walls: Denote the static boundaries used for collisions.
• Renderers: Represent the visualization layer managing canvas interactions (e.g., ParticleRenderer, ConstraintRenderer, and WallRenderer).
• Utilities: Show support libraries like Vector2D and InputHandler which are used by multiple components.
• Demos and Documentation:
• Demos directory as the interactive front-end for user testing and demonstration of the simulation.
• External & Legacy Components:
• Legacy “old_src” files and modules for historical context or alternate implementations if relevant.
Relationships & Interactions
• From the World to the Solver and vice versa; the world contains particles, constraints, and walls, while the solver continuously updates these elements.
• Input flows from the InputHandler to the simulation world for events.
• Iterative and modular relationships (for example, the solver repeatedly invoking updates on all physics entities each frame).
Architectural Patterns & Design Principles
• Modular structure which cleanly separates simulation (core physics and solver), behaviors, constraints, rendering, and utilities. This is reflective of a loosely-coupled, easily extensible system design.
• Use of an iterative local solver (predictor-corrector) within the engine’s update cycle.
• Recognize how the engine separates physics calculation from visualization thereby enabling demos to leverage the core library by simply linking the “dist” files in an HTML document.