Convert Figma logo to code with AI

liabru logomatter-js

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

16,610
1,958
16,610
261

Top Related Projects

2,639

JavaScript 2D physics library

2D JavaScript Physics Engine

8,069

Box2D is a 2D physics engine for games

Port of Box2D to JavaScript using Emscripten

A modular, extendable, and easy-to-use physics engine for javascript

36,911

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.

Quick Overview

Matter.js is a popular 2D physics engine for the web, written in JavaScript. It allows developers to create realistic simulations of rigid bodies, including collision detection and response, in browser-based applications and games.

Pros

  • Easy to use and integrate into web projects
  • Extensive documentation and examples
  • Supports various rendering options (Canvas, WebGL, SVG)
  • Active community and regular updates

Cons

  • Performance can be limited for complex simulations with many bodies
  • Not suitable for 3D physics simulations
  • May require additional libraries for advanced features
  • Learning curve for more complex physics concepts

Code Examples

  1. Creating a simple world with a ball and ground:
const engine = Matter.Engine.create();
const world = engine.world;

const ball = Matter.Bodies.circle(100, 50, 30);
const ground = Matter.Bodies.rectangle(400, 600, 800, 60, { isStatic: true });

Matter.Composite.add(world, [ball, ground]);

Matter.Engine.run(engine);
  1. Applying force to a body:
Matter.Body.applyForce(ball, ball.position, { x: 0.05, y: -0.05 });
  1. Creating a compound body:
const boxA = Matter.Bodies.rectangle(0, 0, 80, 80);
const boxB = Matter.Bodies.rectangle(100, 0, 80, 80);
const compound = Matter.Body.create({
    parts: [boxA, boxB]
});

Matter.Composite.add(world, compound);

Getting Started

  1. Install Matter.js via npm:
npm install matter-js
  1. Import Matter.js in your project:
import Matter from 'matter-js';
  1. Create a basic simulation:
const engine = Matter.Engine.create();
const render = Matter.Render.create({
    element: document.body,
    engine: engine
});

const box = Matter.Bodies.rectangle(200, 200, 80, 80);
const ground = Matter.Bodies.rectangle(400, 610, 810, 60, { isStatic: true });

Matter.Composite.add(engine.world, [box, ground]);

Matter.Engine.run(engine);
Matter.Render.run(render);

This setup creates a simple world with a falling box and a static ground. Adjust as needed for your specific project requirements.

Competitor Comparisons

2,639

JavaScript 2D physics library

Pros of p2.js

  • More focused on performance, particularly for mobile devices
  • Supports constraints and motors, allowing for more complex mechanical simulations
  • Has a smaller file size, which can be beneficial for web-based applications

Cons of p2.js

  • Less active development and community support compared to Matter.js
  • Fewer built-in features and rendering options out of the box
  • Steeper learning curve, especially for beginners in physics simulations

Code Comparison

p2.js:

var world = new p2.World();
var boxShape = new p2.Box({ width: 1, height: 1 });
var boxBody = new p2.Body({ mass: 1, position: [0, 5] });
boxBody.addShape(boxShape);
world.addBody(boxBody);

Matter.js:

var engine = Matter.Engine.create();
var box = Matter.Bodies.rectangle(200, 200, 80, 80);
Matter.World.add(engine.world, box);
Matter.Engine.run(engine);

Both libraries allow for creating physics simulations, but p2.js requires more explicit setup of the world and bodies, while Matter.js provides a more streamlined API for common tasks. Matter.js also includes built-in rendering, whereas p2.js focuses solely on physics calculations.

2D JavaScript Physics Engine

Pros of planck.js

  • More accurate physics simulation due to its use of the Box2D engine
  • Better performance for complex simulations with many bodies
  • Supports continuous collision detection for improved accuracy

Cons of planck.js

  • Steeper learning curve compared to Matter.js
  • Less extensive documentation and community resources
  • Smaller ecosystem of plugins and extensions

Code Comparison

matter.js:

var engine = Matter.Engine.create();
var world = engine.world;
var box = Matter.Bodies.rectangle(200, 200, 80, 80);
Matter.World.add(world, box);
Matter.Engine.run(engine);

planck.js:

var world = planck.World();
var box = world.createBody().createFixture(planck.Box(1, 1));
box.setPosition(planck.Vec2(2, 2));
world.step(1/60);

Both libraries provide physics simulations for JavaScript applications, but they differ in their approach and capabilities. Matter.js offers a more user-friendly API and extensive documentation, making it easier for beginners to get started. It also has a larger community and more plugins available.

On the other hand, planck.js provides more accurate physics simulations and better performance for complex scenarios, thanks to its use of the Box2D engine. It also supports continuous collision detection, which can be crucial for certain types of applications.

The code comparison shows that Matter.js has a more intuitive API, while planck.js requires a bit more setup but offers more fine-grained control over the simulation.

8,069

Box2D is a 2D physics engine for games

Pros of Box2D

  • More mature and widely used in professional game development
  • Generally offers better performance for complex physics simulations
  • Supports a wider range of joint types and constraints

Cons of Box2D

  • Steeper learning curve and more complex API
  • Less straightforward integration with web technologies
  • Requires more setup and configuration for basic scenarios

Code Comparison

Box2D (C++):

b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);
b2BodyDef groundBodyDef;
b2Body* groundBody = world.CreateBody(&groundBodyDef);

Matter.js (JavaScript):

const engine = Matter.Engine.create();
const world = engine.world;
const ground = Matter.Bodies.rectangle(400, 600, 800, 60, { isStatic: true });
Matter.World.add(world, ground);

Both libraries provide physics simulation capabilities, but Box2D offers more advanced features and performance at the cost of complexity. Matter.js, on the other hand, is more accessible and easier to integrate into web projects, making it a popular choice for simpler physics-based applications and games in JavaScript environments.

Port of Box2D to JavaScript using Emscripten

Pros of Box2D.js

  • More mature and widely used physics engine
  • Highly optimized performance for complex simulations
  • Extensive documentation and community support

Cons of Box2D.js

  • Steeper learning curve due to its complexity
  • Larger file size, which may impact load times
  • Less straightforward integration with modern JavaScript frameworks

Code Comparison

Matter.js example:

var engine = Matter.Engine.create();
var box = Matter.Bodies.rectangle(200, 200, 80, 80);
Matter.World.add(engine.world, box);
Matter.Engine.run(engine);

Box2D.js example:

var world = new b2World(new b2Vec2(0, -10));
var bodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(200, 200);
var body = world.CreateBody(bodyDef);

Both libraries offer robust physics simulations, but Matter.js provides a more intuitive API for simple scenarios, while Box2D.js excels in complex simulations with its powerful features and optimized performance. The choice between them depends on the specific requirements of your project, considering factors such as complexity, performance needs, and ease of integration.

A modular, extendable, and easy-to-use physics engine for javascript

Pros of PhysicsJS

  • More flexible and extensible architecture, allowing for easier customization of physics behaviors
  • Better support for complex constraints and composite bodies
  • More advanced collision detection system, including support for concave polygons

Cons of PhysicsJS

  • Less active development and community support compared to Matter.js
  • Steeper learning curve due to its more complex API
  • Fewer built-in examples and documentation resources

Code Comparison

Matter.js:

var engine = Matter.Engine.create();
var world = engine.world;
var box = Matter.Bodies.rectangle(200, 200, 80, 80);
Matter.World.add(world, box);
Matter.Engine.run(engine);

PhysicsJS:

var world = Physics();
var box = Physics.body('rectangle', {
  x: 200,
  y: 200,
  width: 80,
  height: 80
});
world.add(box);
world.add(Physics.behavior('constant-acceleration'));
Physics.util.ticker.on(function(time) {
  world.step(time);
});

Both libraries offer 2D physics simulation capabilities, but PhysicsJS provides more flexibility and advanced features at the cost of complexity. Matter.js, on the other hand, offers a simpler API and better community support, making it more suitable for beginners and smaller projects. The choice between the two depends on the specific requirements of your project and your familiarity with physics engines.

36,911

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.

Pros of Phaser

  • Comprehensive game development framework with built-in physics, audio, and input systems
  • Extensive documentation and large community support
  • Supports multiple rendering options (Canvas and WebGL)

Cons of Phaser

  • Steeper learning curve due to its extensive feature set
  • Larger file size, which may impact load times for web games
  • Less flexibility for custom physics implementations

Code Comparison

Matter.js (Physics simulation):

var engine = Matter.Engine.create();
var box = Matter.Bodies.rectangle(200, 200, 80, 80);
Matter.World.add(engine.world, box);
Matter.Engine.run(engine);

Phaser (Game setup with physics):

var config = { physics: { default: 'arcade' } };
var game = new Phaser.Game(config);
function create() {
    this.physics.add.sprite(200, 200, 'box');
}

Key Differences

  • Matter.js focuses solely on 2D physics simulation, while Phaser is a full-featured game framework
  • Phaser includes built-in game-specific features like sprite management and scene handling
  • Matter.js offers more granular control over physics simulations
  • Phaser provides a higher-level API for game development, abstracting many low-level details

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Matter.js

Matter.js is a JavaScript 2D rigid body physics engine for the web

brm.io/matter-js

Demos ・ Gallery ・ Features ・ Plugins ・ Install ・ Usage ・ Examples ・ Docs ・ Wiki ・ References ・ License

Demos


Gallery

See how others are using matter.js physics

Features

  • Rigid bodies
  • Compound bodies
  • Composite bodies
  • Concave and convex hulls
  • Physical properties (mass, area, density etc.)
  • Restitution (elastic and inelastic collisions)
  • Collisions (broad-phase, mid-phase and narrow-phase)
  • Stable stacking and resting
  • Conservation of momentum
  • Friction and resistance
  • Events
  • Constraints
  • Gravity
  • Sleeping and static bodies
  • Plugins
  • Rounded corners (chamfering)
  • Views (translate, zoom)
  • Collision queries (raycasting, region tests)
  • Time scaling (slow-mo, speed-up)
  • Canvas renderer (supports vectors and textures)
  • MatterTools for creating, testing and debugging worlds
  • World state serialisation (requires resurrect.js)
  • Cross-browser and Node.js support (Chrome, Firefox, Safari, IE8+)
  • Mobile-compatible (touch, responsive)
  • An original JavaScript physics implementation (not a port)

Install

You can install using package managers npm and Yarn using:

npm install matter-js

Alternatively you can download a stable release or try the latest experimental alpha build (master) and include the script in your web page:

<script src="matter.js" type="text/javascript"></script>

Performance with other tools (e.g. Webpack, Vue etc.)

Bundlers and frameworks may reduce real-time performance when using their default configs, especially in development modes.

When using Webpack, the default sourcemap config can have a large impact, for a solution see issue.

When using Vue.js, watchers can have a large impact, for a solution see issue.

Usage

Visit the Getting started wiki page for a minimal usage example which should work in both browsers and Node.js.
Also see the Running and Rendering wiki pages, which show how to use your own game and rendering loops.

Tutorials

See the list of tutorials.

Examples

See the examples directory which contains the source for all demos.
There are even more examples on codepen.

Plugins

The engine can be extended through plugins, see these resources:

Documentation

See the API Documentation and the wiki

Building and Contributing

To build you must first install node.js, then run

npm install

This will install the required build dependencies, then run

npm run dev

to spawn a development server. For information on contributing see CONTRIBUTING.md.

Changelog

To see what's new or changed in the latest version, see the changelog.

References

See the wiki page on References.

License

Matter.js is licensed under The MIT License (MIT)
Copyright (c) 2014 Liam Brummitt

This license is also supplied with the release and source code.
As stated in the license, absolutely no warranty is provided.

NPM DownloadsLast 30 Days