Category Archives: Box2D

Thinking outside the Box2D

When one thinks of the capabilities of Box2D one instantly conjures up images of flying birds, angry flying birds. So what really lies underneath the hood of this little engine that could. Well there’s only one way to find out. So I grabbed myself a copy of the latest code, version 2.12 as of this writing. The first thing you need to do after downloading is configure it with cmake to generate the visual studio solution and associated projects. Which is not to difficult just a matter of unzipping the source somewhere, pointing cmake to that directory, set a build output directory, hit the configure and if no errors hitting generate. A solution called Box2D should be created, open it and build both a debug and release build and you’re good to go.

To assist in getting up to speed with Box2D, there is the usual hello world program. This does nothing more than track the movement of a Box2D object or body (as it is defined) and output it to standard output. Better examples are contained within the folder named Testbed. Inside there is a program that when run allows you to choose a set of examples configured to display and render graphically using OpenGL, freeglut and glui. More importantly all the code is available soone can see how it is all done. Its cleverly designed in a such way to be easily extensible. Just create another header file defining your world, body and fixtures while also including the usual Step, Create and Keyboard functionality and add a new entry into the testentries code file and your done.

As with all frameworks there are certain workflows a developer must abide by to create and use the API correctly. In Box2D, you need to define a world in the form of a b2World object. Think of this as your standard factory pattern that allows you to create other things. Within this world, the other things that can be created are bodies, fixtures and shapes. You can create the world object using new, however due to how the memory is contained in pools you can not new bodies, fixtures and shapes. So dont do it. One of the first things I found out about Box2D is it is renderer agnostic, it basically lives as an entity that you tell to work in a world and how you wish to display that world is up to you. What is strange though for an API designed in C++, why are there so many examples found on the web all using mainly Flash. My knowledge of Flash is dismal to non-existent so you can imagine for a beginner you have this API that can create worlds and there are no examples of how to display your beautiful world. In the end I decided to traverse the testbed example and break it up into the minimum functionality that would produce an output.

Here in lies one of the first hiccups I had using Box2D, evidently it was not triggered by Box2D itself rather it was freeglut. I created a new empty console project and added the necessary files from the testbed, namely Test, Render and Main. I linked the freeglut_static.lib file and all was good except when it came time to run, it complained that freeglut.lib was missing. I thought this was strange as I had linked the static lib. Searching through some forums, I sourced a reputable prebuilt lib file and used it, it fixed the issue provided I also placed the associated freeglut.dll in the executable directory. The program fired up with no popups and then instantly terminated, flashing an error message in the command window before shutting down. After tracing through the code I discover the message was trying to say:

freeglut function called without first calling ‘glutinit’

int main(int argc, char** argv)
testCount = 0;
while (g_testEntries[testCount].createFcn != NULL)

testIndex = b2Clamp(testIndex, 0, testCount-1);
testSelection = testIndex;

entry = g_testEntries + testIndex;
test = entry->createFcn();

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(width, height);
char title[32];
sprintf(title, "Box2D Version %d.%d.%d", b2_version.major, b2_version.minor, b2_version.revision);
mainWindow = glutCreateWindow(title);


As seen in the highlighted code snippet above, I had actually called glutInit so what could possibly be causing the issue. After debugging I could see that line that was causing the issue was the call to GLUI_Master.set_glutReshapeFunc(Resize);. Now GLUI_Master is a global instance that should be available with the initialisation of glut, so if I was calling glutInit… why then wasn’t this being intialised as well. Several hours past I finally found it to be due to the mixing of the freeglut_static.lib and freglut.lib. The fix was as simple as adding the preprocessor definition FREEGLUT_STATIC. Once in place all the code I transferred from the testbed program into my solution worked. So with a working testbed and seeing exactly how everything hooked up, what else could I do. I decided that perhaps putting together an angry birds clone would fuel my persistence to learn Box2D in more depth. At this stage I only feel like mimicking the first level with the aim to make it as close as possible to the original in functionality.


League of Lull

Literally weeks away from beginning second year at AIE and I for one am very eager. Mainly because I have taken my school holidays a little bit too relaxed, so I am in need of some structure. What has happened since my last post? Well a few things:

I got engaged to my long, long time girlfriend. A parting gift so to speak as she was leaving to go on holiday with a friend to America (San Francisco, LA, Las Vegas, Atlanta and NYC), Hawaii and Fiji. I decided to surprise her just days before both Christmas and her departure date.

The ring was a Verragio and up until about 6 months ago I never even knew such a designer even existed. It was only by chance perusing parts of pinterest that I discovered this hand made exclusive designer. So exclusive that here in Australia there are no retailers, distributors and manufacturers anywhere, nothing. Fortunately the internet age has made things a little easier and I was able to source an online retailer. It even had a useful website allowing me to select an appropriate style, diamond all online. The actual jeweller was situated in NYC, which was somewhat convenient considering the time I purchased the ring was very close to her departure date. So plan B was to have her pick up the ring from them while she was in NYC, fortunately the ring arrived several days before so plan B never eventuated. After the order, I waited 4 weeks only to receive a phone call that customs would not release a delivery of mine until I paided some taxes, some unforseen additional taxes mind you, to the sum of ~$1100. My immediate thought was, is this a hoax? Then a small inkling made me realise that it could possibly be due to the ring I had ordered, it was. I called the toll free number and this took me to FedEx where I later discovered the total tax could be broken down into 3 constituents which included GST (10% on total price), Duty (5% of total price) and admin (around $50). Now that doesn’t seem like a lot until you consider the ring itself set me back ~$9200AUD and I am no longer working which meant I had no income other than savings, so as you can imagine it hurt the pocket a little more than I had expected. In the end I had to pay for it, no point going this far and not getting what I ordered. Besides, most people only perform this act once and considering I had waited just over 10 years to ask, I felt the means out weighed the costs.

Christmas week towards new years saw my cousin and her friend fly in from interstate to visit me. Having moved from Adelaide we did leave behind a lot of family and friends so it was refreshing to see some family especially during Christmas time. Additionally the added benefit for someone visiting me in Sydney after living here for 2 years I was able to show them all the good spots and a taste of what I found interesting about Sydney. Which is not to say the touristy adventures were out, they still happened, only I listed the goods ones or the ones I never had tried before.

However, after they left I feel into a rut and was not over productive, my sleep was a mess and I was not doing much programming or blogging. The only positive was I increased my gym attendance for 3-4 times a week to everyday and I continued this for about 3 weeks straight. The idea was to tire myself out so I could sleep better, however, it instead created more energy and I was finding it had the opposite effect. My mind started thinking about all sorts of game ideas.

Today I can say my sleep is still a mess though it is gaining some normality. Over the next few days I will aim to get back to programming and what better than to contribute on a topic I have always been interested in, physics in games. So with that I have chosen to experiment with Box2D. Hopefully I can post some simple code samples on here with the aim that I will build it up to a game demo prototype of some sort.