Box2D C++ tutorials - Introduction
Last edited: March 14 2012Introduction
Box2D is the name given to the world's best 2D physics engine, which may seem a little unimaginative when so much software is named after some kind of animal, but a name is just a name and this rose would smell as sweet no matter what it was called. It's light, robust, efficient and highly portable. It has been battle-proven in many applications on many platforms. And it's open-source and free. Check out the Box2D website at http://www.box2d.org
A physics engine simulates the physics of objects to give them believable real-life movement. Although it can be used for other applications, the project was born primarily as a library for use in games, and games make up the majority of software using Box2D. The engine is written and maintained by one Erin Catto who it seems, not satisfied by simply making kick-ass physics as a day-job, set up Box2D to make kick-ass physics as a hobby as well.
Box2D is written in C++, but has been ported to many different languages by the user community. Here are just a few, you can also find more at http://www.box2d.org/links.html.
- (Flash) http://www.box2dflash.org/
- (Flash) http://www.sideroller.com/wck/
- (Java) http://www.jbox2d.org/
- (Python) http://code.google.com/p/pybox2d/
- (Javascript) http://box2d-js.sourceforge.net/
- (C#) http://code.google.com/p/box2dx/
- cocos2d iPhone
- Citrus engine
- Corona SDK
- Game salad
- Pixelwave (iOS)
- todo: help me out here...
Box2D C++ tutorials
The user manual explains almost everything you need to know about using the library, at least for C++. However I've been using Box2D for a while and reading the discussion forums I see the same questions coming up quite regularly. Since these are often things I have come across myself too, I decided I would write up some suggestions about how these issues could be solved. Then I figured if I was gonna do that, I might as well make a set of tutorials on using Box2D right from the beginning.
Looking around on the net, I found some people have already done a pretty good job of this:
- Allan Bishop (Flash)
- Emanuele Feronato (Flash)
- Flashy Todd (Flash)
- Ray Wenderlich (objc/c++/iPhone)
- Seth Ladd (Javascript)
- ... you? (let me know!)
Basic usage
- Testbed setup (linux, windows, mac)
- Testbed structure
- Making a 'test' for the testbed
- Bodies
- Fixtures
- World settings
- Cancelling gravity
- Forces and impulses
- Moving at constant speed
- Keeping a body rotated at given angle
- Jumping
- Using debug draw
- Drawing your own objects
- User data
- Collision callbacks
- Collision filtering
- Sensors
- Raycasting
- World querying
- Removing bodies
- The 'touching the ground' question
- Joints
- Some gotchas
- Vehicle suspension
- Sticky projectiles
- Projected trajectory
- Explosions
- Breakable bodies
- Top-down car physics
- Terrain
- One-way walls and platforms
- Conveyor belts
- Escalators?
- Elevators
- Arrow in flight
- Advanced character movement
I will be using version 2.1.2 of the Box2D source code which seems to be the latest official release at this time. For most of the examples I will try to add the code for the tutorial as a 'test' in the 'testbed' which comes with the Box2D source code. This removes the need for setting up a project, window, etc and allows us to get straight into using the actual library itself.
Update:Version 2.1.2 is looking a little old now, but the main difference in the current (v2.3.0) version is the addition of chain shapes. Most of the material in these tutorials is still applicable, and I will try to add a little 'update' note like this in places where something should be mentioned.
Requirements
To follow these tutorials you will need to have an intermediate-level knowledge of C++. A beginner-level knowledge of OpenGL would be handy too, but not necessary. As for required software, you can download everything you'll need for free - no excuses now huh?
Feedback
If you spot any mistakes, have suggestions for improvement, or any other feedback write a comment on one of the topic pages or contact me at the gmail address: 'iforce2d'
Follow me on twitter to be notified when topics are added.
