A modular JavaScript physics engine that simulates a basketball with realistic physics including gravity, air resistance, bouncing, and surface friction.
- Realistic basketball simulation with gravity and air resistance
- Tarmac surface with appropriate friction
- Visual trail effects for fast movement
- Charge-and-release gameplay mechanic
- Responsive design that adapts to window size
The codebase is organized into separate modules following modern JavaScript practices:
src/constants.js
- Shared constants, configuration, and statesrc/ball.js
- Ball physics calculations and state updatessrc/renderer.js
- Canvas drawing functions for visual elementssrc/input.js
- User interaction handlers (mouse events)src/engine.js
- Main physics engine and animation loopsrc/main.js
- Entry point for the application
- Gravity: Constant downward acceleration
- Air Resistance: Calculated based on air density, velocity, drag coefficient, and cross-sectional area
- Surface Friction: Different friction values for different surfaces
- Collision Detection: Boundary collisions with walls and floor
- Rotational Physics: Ball rotation affected by collisions and rolling
- Open
index.html
in a web browser - Click and drag on the basketball to charge it
- Release to launch the ball with the direction and power indicated by the arrow
- The ball will interact with boundaries and surfaces realistically
The modular structure allows for easy extension:
- Add new surfaces by extending the
surfaces
object inconstants.js
- Implement new physics behaviors in
ball.js
- Create additional visual effects in
renderer.js
index.html
- The main HTML filestyles.css
- CSS styling for the canvas and page layoutball.js
- JavaScript implementation of the custom physics engine
You can modify the physics parameters in the ball.js
file:
gravity
- Controls how fast the ball accelerates downwardfriction
- Controls how quickly horizontal movement slows downrestitution
- Controls how bouncy the ball is (0-1 range)