How Does Box2dWeb Work?

Box2dWeb is a physics modeling engine. It allows a developer to create a world with physics properties and add objects to that world that react to the properties of that world and to each other. However, the world is not displayed (not formally anyway, as you’ll see in a bit). The job of the Canvas developer when using Box2dWeb is to define a physics model and then apply it to the Canvas.

Before we get started, here are some definitions in Box2dWeb related to the examples we are going to cover. (The World modeled with Box2D is more complicated than just this, but these are the things we will cover in the examples.)

Shape

A geometrical object that exists 2D space. These are circles, squares, rectangles, and so on.

Fixture

A logical object that binds a shape to body and give it properties like density, friction, and restitution.

Rigid Body

A physical manifestation of an object in the 2D world, with a fixture and a shape applied.

World

A 2D physics world that contains bodies that react to one another

In the examples that follow, we will show how these things relate to one another in Box2D.

Get HTML5 Canvas, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.