A modular 2D physics engine with interactive demos showcasing real-world physics concepts and game development techniques.
Physics-engine/
├── README.md # This overview
├── core/ # 🔧 Shared Physics Engine
│ ├── vector.js # Enhanced 2D vector mathematics
│ └── collision.js # Collision detection utilities
└── demos/ # 🎮 Interactive Demonstrations
└── bouncing-balls/ # Multi-ball physics sandbox
Location: demos/bouncing-balls/
An advanced physics sandbox featuring:
- Realistic Physics: Gravity, drag, friction, and bouncing
- Multi-Ball Support: Up to 10 balls with ball-to-ball collisions
- Interactive Controls: Mouse drag-and-throw with inertia
- Visual Effects: 3D rendering, trails, and dynamic shadows
- Real-time Physics: Velocity Verlet integration at 60 FPS
Features:
- Proper collision response with energy dissipation
- Ball settling (balls come to rest naturally)
- Multiple input methods (keyboard, mouse, scroll)
- Advanced force modeling (gravity, drag, floor friction)
- Pause functionality with visual indicator
Enhanced Vec class with comprehensive 2D operations:
- Basic math: add, subtract, multiply, divide
- Advanced operations: normalize, dot product, cross product
- Utilities: rotation, distance, interpolation, reflection
limit()andsetMagnitude()for convenience- Both immutable and mutating versions for performance
Optimized collision detection utilities:
- Circle-rectangle collision
- Circle-circle collision (for ball interactions)
- Point-in-shape tests
- Collision normal and penetration calculation
- Edge case handling for invalid inputs
- Bouncing Balls: Open
demos/bouncing-balls/index.htmlin any modern browser
The demo works immediately with no build step required.
- Mouse Click: Create new ball at cursor position
- Mouse Drag: Drag and throw balls with inertia
- Mouse Wheel: Adjust size for next ball
- Space: Create random ball
- R: Drop ball from top
- +/-: Adjust ball size
- P: Pause/Resume
- C: Clear all balls
- Kinematics: Position, velocity, acceleration relationships
- Force Dynamics: Multiple force types and Verlet integration
- Collision Response: Elastic collisions with proper impulse
- Energy Systems: Conservation, damping, and settling
- Modular Architecture: Shared core with demo-specific implementations
- Canvas Graphics: Advanced 2D rendering techniques
- Game Loops: Fixed timestep physics with smooth animation
- Object Pooling: Memory-efficient vector operations
- Vector Algebra: 2D vector operations and transformations
- Numerical Integration: Velocity Verlet for stable physics
- Collision Geometry: Circle intersection algorithms
- Trigonometry: Angle calculations and rotations
- Code Reuse: Physics components shared between demos
- Maintainability: Bug fixes in core benefit all demos
- Extensibility: Easy to add new demos using existing foundation
- Performance: Optimized physics calculations and rendering
- Create new directory in
demos/ - Import shared components from
core/ - Implement game-specific logic
- Update this README
- Optimized Physics: Efficient vector operations and collision detection
- Spatial Partitioning: Grid-based optimization for many balls
- Object Pooling: Reduced garbage collection overhead
- Smart Rendering: Minimal overdraw and strategic updates
- 60 FPS Target: Consistent performance across devices
Each demo is self-contained with its own:
- HTML entry point
- CSS styling
- JavaScript logic
- Documentation
Shared components in core/ are imported as needed, promoting:
- Code reuse
- Consistent behavior
- Easy maintenance
- Modular development
🎮 Ready to explore physics in action?
Start with the Bouncing Balls Demo for realistic physics simulation!
Built with modern JavaScript, HTML5 Canvas, and a passion for physics simulation.
Open source under the MIT License. See the root LICENSE file in the repository for full details.