gitmyhub

matter-js

JavaScript ★ 18k updated 1y ago

a 2D rigid body physics engine for the web ▲● ■

Matter.js is a JavaScript library that simulates 2D physics in a web browser, objects fall, collide, bounce, and connect with ropes, letting you build games and animations that feel physically real.

JavaScriptNode.jsCanvas APIsetup: easycomplexity 2/5

Matter.js is a JavaScript library for simulating 2D physics in web browsers and in Node.js. Physics simulation means the library calculates how objects move, collide, bounce, and fall under the influence of gravity — allowing developers to create games, interactive animations, and visual experiments where objects behave like they would in the real world.

The library supports rigid bodies (solid objects that don't deform), compound bodies (complex shapes built from simpler ones), concave and convex shapes, physical properties such as mass and density, friction, air resistance, and collision detection. It also handles constraints (like ropes or joints connecting objects), sleeping (pausing simulation for objects that have stopped moving to save processing power), raycasting (detecting what a ray of light would hit), and time scaling (slowing down or speeding up the simulation). There is a built-in canvas renderer that draws the simulation as 2D graphics, and the library works across major browsers as well as on mobile devices via touch input.

Matter.js can be installed via npm or included directly in a webpage via a script tag. The engine can be extended through a plugin system. The project includes a large set of demo scenes — things like Newton's cradle, a wrecking ball, cloth simulation, and various stacking challenges — both as code examples and as a live demo gallery.

Where it fits