Key motivations for the development of this project are:
- Taking advantage of Box2D's efforts and achievements
- Optimizing the library for web and mobile platforms
Made with Planck.js
Testbed examples and demos.
- Astray 2 (source) by Rye Terrell
- Acolyte Fight
- Nitro Clash
- Air Hockey (source) by Steve Meredith
- Coined (source)
- Hoverator by Jonathon Vogel
- 1000 Unique Postcards by Andreas Gysin
- Flag in the Wind by Nick Matantsev
- Neuroevolution Bots by Mishig Davaadorj
- Walking EA by Mats Krüger Svensson
- Chaotic Water Wheel by John Hearn
- Dynamic Convex Hull by Bo Zheng
- Q-Learning Ragdoll by Mikko Kämäräinen
Articles and Tutorials
- Phaser 3 with Planck.js by Emanuele Feronato
To try Planck.js, simply add
planck-with-testbed.js script to your HTML code and call
planck.testbed(callback) with your code in callback. For example:
Latest builds are available on releases page.
Planck.js is available on jsDelivr.
npm install planck-js --save
bower install planck-js --save
API and Architecture
Planck.js public API closely follows Box2D API (see Resources), with the following differences:
b2prefix is dropped from class names, for example
b2Worldis now available as
- Method names are converted from UpperCamelCase to lowerCamelCase.
- Shapes are considered immutable and are not cloned when used to create fixtures.
- Listener classes are replaced with simple functions.
World#off(eventName, listenerFn)are added to add and remove event listeners. Currently supported events are:
Planck.js does not include rendering and graphics by default, however you can use one of these options:
- For testing and development use testbed as explained in Try it and Development sections
- Develop a renderer or integrate with an existing graphics library
- Use an existing renderer such as RealPeha/planck-renderer
Resources and References
- Box2D Manual and FAQ are highly recommended to get started.
- iforce2d website includes a collection of helpful tutorials and resources to learn Box2D.
Following resources are recommended if you are interested in learning about Box2D/Planck.js's internal details.
- Continuous Collision by Erin Catto (slides)
- Solving Rigid Body Contacts by Richard Tonge (slides)
- dyn4j Blog Posts by William Bittle
This tutorial is under development, please feel free to edit, comment or ask for new sections.
Before writing code there are a number of key concepts to learn:
- Shape - A shape contains geometrical information and is used in collision detections.
- Fixture - A fixture consists of a shape and physical properties such as density.
- Body - Each body is composed of a number of fixtures which are fixed together, that is a body is a set of shapes with physical properties. Bodies have position, angle, linear velocity, angular velocity, etc. which can be changed by applying linear and angular forces or impulses. Bodies represent rigid objects in the world, such as ground, a box or a car.
- Joint - Joints are constraints on bodies position or velocity.
- Contact - When two bodies touch each other (are colliding) a contact between them is created.
- World - A world is composed of a number of bodies and joints interacting with each other. Every time world’s step() function is called, world solver will detect any contact between bodies and then change bodies position and velocity according to velocity, forces, contacts, and joints constraints.
A physics simulation in Planck starts by creating a World and adding Bodies and Joints. So let’s create our world:
var world = planck;
This will create a world with default options. You can pass a definition object as first argument to World constructor, for example:
var world = planck;
Bodies are directly created and added to world:
var ground = world;
This will create a body with default options, which means a ‘static’ body at position 0, 0 with no velocity. You can pass a body definition object to change it:
var ground = world;
After creating a body, you can create a fixture using a shape:
Rendering and Integration
Planck.js does not use any renderer by default. To use or integrate it with a rendering library all you need to do
world.step(timeStep) in each frame, and then iterate over world entities to draw or update them.
You may also want to listen to world events to remove objects which are removed from the world. For example:
For development, you can run testbed locally with a live build and try examples in
Clone or download this repository
Install npm dependencies:
Run testbed and open it in your web browser (see command-line output for URL to open):
npm run testbed
Box2D is a popular C++ 2D rigid-body physics engine created by Erin Catto. Box2D is used in several popular games, such as Angry Birds, Limbo and Crayon Physics, as well as game development tools and libraries such as Apple's SpriteKit.
TypeScript definitions for planck.js are developed by Oliver Zell.
Planck.js is available under the zlib license.