Hsssss. No, I mean, hi.
I just got Box2D, managed to compile it (without any problems!) on MinGW under Windows, and since I'm a Python fan, I thought... it'd be cool if we could have Box2D working under Python. Like any programmer (I guess), I'm lazy, and I thought, what the hell, I could just do it the easiest possible way, that is, using SWIG to auto-generate everything. Guess what - it works! I just ran Hello.py (ported Box2D HelloWorld.cpp), and it.. well, it just seems to work, which is obviously just awesome.
Okay, so here's what I did. I'm expecting you to have MinGW, SWIG and Python (I used 2.4) installed.
- Compiled Box2D. (I can't remember the exact command line, now, but this should be close enough.
Box2D\Source> g++ -c -O3 -w Common\*.cpp Collision\*.cpp Dynamics\*.cpp Dynamics\Contacts\*.cpp Dynamics\Joints\*.cpp
Box2D\Source> ar rcvs ..\Library\libBox2D.a *.o
- Wrote the trivial SWIG wrapper file, Box2D.i, in our wrapper-building directory, Box2D\Swig.
- EDIT: There's one function declared in the headers that isn't in the .cpp files. SWIG will try to futilely wrap this, making the compilation step fail, unless you comment it out. The function is b2BroadPhase::ValidatePairs (line 88 in Source\Collision\b2BroadPhase.h). Just comment that out, ie.
And you'll be done.
- So far so easy, right? Let's make SWIG crunch that file then. (I'll just go ahead and expect you to have SWIG in your %PATH%.)
Box2D\Swig> swig.exe -python -c++ -O -includeall -ignoremissing Box2D.i
This should complete with a few warnings, but those didn't seem to matter. Maybe they will with something less trivial than Hello World.
- Compile the wrapper DLL. This command line will vary with your installation paths and Python version.
You might also probably need a GCC (.a) version of the Python library. See http://www.dabeaz.com/cgi-bin/wiki.pl?S ... UsingMingw for instructions (that Perl part). Though try without, first!
Box2D\Swig> g++ -shared -o _Box2D.dll -O3 -I \Programs\python24\include -L \Programs\Python24\Libs box2d_wrap.cxx ..\Library\libbox2d.a -lpython24
Since that's running in optimization mode three, it'll take some time to finish. Grab a cup of cof.. oh, it's ready.
- Try it! Here's Hello.py. (Place it in the Swig directory, so it can find your new Box2D module.)
from Box2D import *
doSleep = True
world=b2World(worldAABB, gravity, doSleep)
groundBoxDef.density = 0.0
boxDef.density = 1.0
boxDef.friction = 0.3
body = world.CreateBody(bodyDef)
timeStep = 1 / 60.0
iterations = 10
for i in xrange(60):
position = body.GetOriginPosition()
rotation = body.GetRotation()
print "%4.2f %4.2f %4.2f"%(position.x, position.y, rotation)
Hope this process works for you people, too!
Here are the compiled wrappers for Python 2.4 and 2.5 / MingW / Windows, as well as Hello.py if you just want to tinker (at a whopping 449 KiB, the ZIP is too big to attach...): http://servut.us/akx/Box2DPy.zip
Enjoy and ask if you have problems. I'm no expert, but I might be able to help nevertheless.