2D Game Development with OpenGL – Setting up Box2D
This post will explain how to install Box2D on a Linux/Unix system and then add it to your game, ready to be used. Box2D is a physics engine specifically designed to be used in 2D graphics software to bring real world physics. You can find a list of features Box2D contains here: http://www.box2d.org/features.html
Downloading and compiling for Linux
The first thing you will need to do is download the Box2D archive from the Google code website, found here: http://code.google.com/p/box2d/downloads/list. The latest version at the time of writing this post is v2.1.2. Once downloaded, extract the archive to any folder you want to use as the build directory.
The next step is to open a terminal and cd to the directory you have extracted Box2D to and then cd again into Box2D_v2.1.2 (or whatever version you have downloaded). And then finally, cd into the Box2D directory.
Once in this directory, use cmake to create the Makefile needed to build Box2D:
cmake .
You will need cmake to be installed before you do this, if you do not, you can install it on Debian distributions as follows:
sudo apt-get install cmake
If all is successful, you should receive no errors and your output should look something like this:
Next step is easy, just type make and Box2D will begin to compile. Output should look like this:
Once that has completed, go ahead and install the build Box2D libraries and header files with the following command:
sudo make install
Once this command is completed, type:
whereis Box2D
and the output should be the location the libraries have been installed to. In my case, they are at /usr/local/lib/Box2D.
You have now installed Box2D on your system and are ready to use it in your project.
Including it in our tank game
Including it in our game at this point only requires 2 changes, the first being the addition of the Box2D header file to Main.cpp and the addition of the library to the Makefile build line.
Add to the top of Main.cpp:
#include <Box2D/Box2D.h>
and then add:
-lBox2D
to the end of your makefile build line. Type make to build your game again and it should compile with Box2D included.
In the next step we will be drawing a basic tank to the screen



One Comment
Leave a CommentTrackbacks