What is Box2D? It’s a free and open source 2D physics engine. It is developed with C++, and has fortunately been ported to many other languages, including Actionscript and Java. I was eager to try it out.
At the same time, I was eager to get more familiar with the new C++11 standard I’ve been hearing about (a.k.a. C++0x). So I made a small program to get a taste of Box2D and C++11 features. You can view and download the program here.
To use it, drag out a rectangle with your mouse. When you release the mouse, the rectangle becomes a object in the world. It is affected by gravity, and interacts realistically with other objects. If you happen to own a touch screen, you can press space to hide the mouse cursor. You’re welcome.
Mixing Box2D with SFML graphics was a quite tricky. To keep track of the objects, I created an std::vector of this struct:
typedef struct {
shared_ptr graphic;
shared_ptr body;
} PhysicsObject;
There was a problem though: The Box2D world’s coordinates are 1 unit = 1 meter, and the Y-axis increases upwards. SFML coordinates, however, are more conventional computer graphics coordinates: 1 unit = 1 pixel, and the Y-axis increased downwards. To add to these troubles, the SFML view (sf::view) has to be able to scale (zoom) and move around the world.
…