Top Related Projects
a 2D rigid body physics engine for the web ▲● ■
2D HTML5 rendering and layout engine for game development
JavaScript 2D physics library
Port of Box2D to JavaScript using Emscripten
A modular, extendable, and easy-to-use physics engine for javascript
Quick Overview
Planck.js is a JavaScript rewrite of the Box2D physics engine, designed for use in web browsers and Node.js environments. It provides a 2D physics simulation for games and interactive applications, offering a lightweight and efficient solution for developers working on web-based projects.
Pros
- Lightweight and optimized for web environments
- Compatible with both browser and Node.js platforms
- Provides a familiar API for developers experienced with Box2D
- Supports various shapes and physics simulations, including collisions and joints
Cons
- Limited documentation compared to the original Box2D
- May have performance limitations compared to native implementations
- Lacks some advanced features found in more comprehensive physics engines
- Smaller community and ecosystem compared to more established physics libraries
Code Examples
Creating a world and adding a body:
const planck = require('planck-js');
const world = planck.World();
const bodyDef = {
type: 'dynamic',
position: planck.Vec2(0, 0),
angle: 0,
};
const body = world.createBody(bodyDef);
const box = planck.Box(1, 1);
body.createFixture(box, {
density: 1,
friction: 0.3,
});
Simulating the world:
const timeStep = 1 / 60;
const velocityIterations = 6;
const positionIterations = 2;
function step() {
world.step(timeStep, velocityIterations, positionIterations);
// Update game objects based on physics simulation
}
setInterval(step, 1000 / 60);
Handling collisions:
world.on('begin-contact', (contact) => {
const fixtureA = contact.getFixtureA();
const fixtureB = contact.getFixtureB();
// Handle collision between fixtureA and fixtureB
});
Getting Started
To get started with Planck.js, follow these steps:
-
Install Planck.js using npm:
npm install planck-js
-
Import Planck.js in your project:
const planck = require('planck-js');
-
Create a world and add bodies:
const world = planck.World(); const body = world.createBody({ type: 'dynamic', position: planck.Vec2(0, 0), }); body.createFixture(planck.Box(1, 1));
-
Implement a game loop to step the physics simulation:
function gameLoop() { world.step(1 / 60); requestAnimationFrame(gameLoop); } gameLoop();
Competitor Comparisons
a 2D rigid body physics engine for the web ▲● ■
Pros of Matter.js
- More extensive documentation and examples
- Larger community and ecosystem
- Built-in rendering capabilities
Cons of Matter.js
- Slower performance for complex simulations
- Less accurate physics simulation
- Larger file size
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);
Summary
Matter.js offers a more user-friendly experience with better documentation and built-in rendering, making it suitable for quick prototypes and simpler projects. Planck.js, on the other hand, provides better performance and more accurate physics simulation, making it ideal for complex simulations and games requiring precise physics. The code comparison shows that Matter.js has a slightly more intuitive API, while Planck.js offers more granular control over the physics simulation.
2D HTML5 rendering and layout engine for game development
Pros of Stage.js
- Focuses on 2D game development with a higher-level API
- Provides built-in support for animations, sprites, and scene management
- Easier to use for beginners and those primarily interested in 2D game creation
Cons of Stage.js
- Less flexible for complex physics simulations compared to Planck.js
- Limited to 2D applications, while Planck.js can be extended for 3D use
- Smaller community and fewer resources available
Code Comparison
Stage.js example:
var stage = Stage.create();
var box = stage.createSprite().pin({
handle: 0.5,
width: 100,
height: 100,
rotation: Math.PI / 4
});
Planck.js example:
var world = planck.World();
var body = world.createBody({
type: 'dynamic',
position: planck.Vec2(0, 0),
angle: Math.PI / 4
});
body.createFixture(planck.Box(0.5, 0.5));
Stage.js is more focused on creating visual elements for games, while Planck.js provides lower-level physics simulation capabilities. Stage.js offers a simpler API for game development, but Planck.js offers more flexibility for complex physics-based applications.
JavaScript 2D physics library
Pros of p2.js
- More mature and established project with a larger community
- Supports a wider range of physics simulations, including springs and constraints
- Better documentation and examples available
Cons of p2.js
- Larger file size and potentially higher performance overhead
- Less actively maintained, with fewer recent updates
- May be more complex to use for simple 2D physics scenarios
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);
planck.js:
var world = planck.World();
var boxBody = world.createBody({
type: 'dynamic',
position: planck.Vec2(0, 5)
});
boxBody.createFixture(planck.Box(0.5, 0.5));
Both libraries offer similar functionality for basic 2D physics simulations, but planck.js tends to have a more concise syntax and smaller footprint. p2.js provides more advanced features and broader physics capabilities, making it suitable for more complex simulations. The choice between the two depends on the specific requirements of your project and the level of physics complexity needed.
Port of Box2D to JavaScript using Emscripten
Pros of Box2D.js
- Direct port of the popular C++ Box2D physics engine, ensuring high compatibility and familiarity for developers
- Extensive documentation and community support due to its long-standing presence in the game development ecosystem
- Proven track record in numerous production-level games and applications
Cons of Box2D.js
- Larger file size and potentially higher memory footprint compared to Planck.js
- May have performance overhead due to being compiled from C++ to JavaScript via Emscripten
- Less idiomatic JavaScript API, as it closely follows the C++ structure
Code Comparison
Box2D.js:
var world = new b2World(new b2Vec2(0, -10));
var bodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
var body = world.CreateBody(bodyDef);
Planck.js:
var world = planck.World({gravity: planck.Vec2(0, -10)});
var body = world.createBody({
type: 'dynamic'
});
Both libraries provide similar functionality, but Planck.js offers a more JavaScript-friendly API with a smaller footprint. Box2D.js, being a direct port, maintains closer alignment with the original C++ implementation, which may be preferable for developers familiar with Box2D.
A modular, extendable, and easy-to-use physics engine for javascript
Pros of PhysicsJS
- More extensive documentation and examples
- Supports a wider range of physics simulations, including soft-body physics
- Modular architecture allows for easy customization and extension
Cons of PhysicsJS
- Less actively maintained (last update was several years ago)
- Performance may be slower for large-scale simulations
- Steeper learning curve due to more complex API
Code Comparison
PhysicsJS:
var world = Physics();
world.add(Physics.body('circle', {
x: 50,
y: 30,
radius: 20
}));
world.render();
Planck.js:
var world = planck.World();
var body = world.createBody();
var circle = planck.Circle(20);
body.createFixture(circle);
body.setPosition(planck.Vec2(50, 30));
Both libraries allow for creating physics worlds and adding bodies, but PhysicsJS uses a more declarative approach, while Planck.js offers a more programmatic API. PhysicsJS provides a built-in renderer, whereas Planck.js requires separate rendering implementation.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Planck.js
Planck.js is JavaScript/TypeScript rewrite of Box2D physics engine for cross-platform HTML5 game development.
Motivations
- Taking advantage of Box2D's efforts and achievements
- Developing readable and editable code in JavaScript/TypeScript
- Providing idiomatic JavaScript/TypeScript API
- Optimizing the library for web and mobile platforms
Documentation
Examples
Discord
Made with Planck.js
Report Issues
To speed up resolving issues, please provide testbed code to reproduce the issue.
Top Related Projects
a 2D rigid body physics engine for the web ▲● ■
2D HTML5 rendering and layout engine for game development
JavaScript 2D physics library
Port of Box2D to JavaScript using Emscripten
A modular, extendable, and easy-to-use physics engine for javascript
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot