PyWeek - Chipmunk 2d physics lib

In the same spirit as Hectigo and tsmaster, I have now with this post released a small library called pymunk just for pyweek! :)

Chipmunk is a 2d physics engine written in C, and can be found here: Chipmunk.

pymunk is a low level binding made with ctypes, so you can now use Chipmunk in your Python code!

The link: pymunk-0.1.zip

And a screenshot from the included demo:

And a cut-n-paste from the readme:

pymunk - pyweek5 edition
========================
2007, Victor Blomqvist - vb@viblo.se


ABOUT
pymunk is a wrapper around the 2d physics library Chipmunk
(http://wiki.slembcke.net/main/published/Chipmunk).

At the moment, its an autogenerated ctypes binding + a modified version
of the vector2d cookbook-snippet from pygame.org at
(http://www.pygame.org/wiki/2DVectorClass)


HOW TO USE
There is no specific pymunk documentation yet except for this readme.
However, The Chipmunk docs should be more or less enough together with ctypes,
as pymunk is just a very low level ctypes binding at the moment.
Chipmunk documentation can be found here:
http://files.slembcke.net/chipmunk/chipmunk-docs.html


EXAMPLE
See demo1.py for an example of how easy it is to use! :)


DEPENDENCIES/REQUIREMENTS
* A windows-dll of Chipmunk (or a *nix-library if you are on *nix). See COMPILE
for compile-instructions. The dll/lib should be placed somewhere where pymunk
can find it. A safe bet should be in the same folder, at least on Windows ;)
pymunk uses the source from Chipmunk SVN revision 125.
* ctypes (included in python 2.5)


COMPILE
If you are on a platform other than windows, you will have to build Chipmunk
on your own. It should be no problem, the source is included in the
Chipmunk_src folder.

> gcc -O3 -std=gnu99 -ffast-math -c *.c
> gcc -shared -o libChipmunkPyEd.so *.o
or run build.sh (untested!) to compile.
The output should be placed somewhere pymunk can find it.


THE FUTURE
I have plans to do a more high-level/pythonic wrapper in the future, and even
started a little. But the pyweek library release stop is today(! or tomorrow?),
so the low-level stuff had to do for now.

viblo on 2007/08/01 20:35 of Bouncy


Comments: (log in to comment)

Ooooooh.... :)
richard, can we have until the 3rd to release our libs?
that is technically 1 month before the competition start on the 3rd of September.
Whoops, it looks like the start is the 2nd, oh well, can we have until tomorrow then?
Yes, you can have a day :)
oh, thanks, I'll actually be done this time :D
To build chipmunk on OS X:
gcc -O3 -std=gnu99 -fno-common -ffast-math -c *.c
gcc -dynamiclib -o libChipmunkPyEd.dylib *.o
Works well!
Haha!
Whee, this is a really fun library. :)

Thanks viblo!
I have zipped a new version that can be downloaded from here: pymunk-0.1.1.zip
The changes are:
- Included the dylib-file richard compiled (and made a little adjustment to the "load dll" part of _chipmunk).
- Added two functions to pymunk.util, reduce_poly() and convex_hull(). reduce_poly removes a couple of points from a list of points it thinks are to close each other, and convex_hull creates a convex hull from a list of points.
- Added demo3.py that lets you draw polys that are reduced and have their hulls shown.
No more changes before (or more like after..) pyweek!

It's a nice library, and could prove to be very useful in the future, but the demo1.py included in version 0.1.1 seems to have one error on line 79, which causes the program to crash at least on my Windows XP machine with Python 2.5 when trying to create boxes or polygons. The point vector there is passed on to util.py as a tuple, when it actually seems like it should be a vec2d object. Changing the line from

        points2.append((x,y))

to

        points2.append(vec2d(x,y))

seems to fix the problem. Anyway, it's pretty easy to fix and doesn't probably prevent anyone from using the library.

Oh, thanks for pointing it out. While looking at it, I also found another bug or 3, line 179, 295 and 297 should also wrap the tuples in a vec2d object. Sorry about it, I put the bugs there because of the little change I made to the is_clockwise() function in util, it now expects a vec2d object instead of just a tuple. Maybe it was a bad move to change it, but .x is imho more clear than [0] and is what the other util-functions use.

It shows why you shouldn't rush lib releases, and Im glad most of the important code (that is, _chipmunk.py) was autogenerated and not written by me :)

Anyway, as you said this shouldn't cause any big problems for anyone using the lib.

Bugfix releases are welcomed before the challenge starts :)
This looks AWESOME! I'm going to love using this. 2d shooters have never been so much fun. The demo was heaps of fun on its own, although there were no joints. Are chipmunk's joints supported in Pymunk?
I haven't tested them myself, but they should work. You should be able to do anything you can do with chipmunk from pymunk. The low level bindings are just autogenerated, and I think it got most of it (except for a few inlined vector functions, but that should be no problems since pymunk uses its own vec2d object instead).
On Ubuntu 7.04
Run once:
cd /path/to/pymunk/Chipmunk_src
gcc -O3 -std=gnu99 -ffast-math -c *.c
gcc -shared -o libChipmunkPyEd.so *.o
cp libChipmunkPyEd.so ..
To run demo:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. python demo1.py
note that requires python 2.5 , for functools module.
@viblo: I'm planning on using this for PyWeek, so once it starts I'll probably be working on a higher level interface for it. If my code doesn't turn out terribly, you might be able to use it as the base to your wrapper.
neko: Sounds great! :)
We might come up with something too, but you never know. The game is more important, and last minute cutouts might be needed. I still don't have a clear picture of how a higher level interface should look, some inspiration, experice and code from other people using it on how to do it best would be very nice.

claxo: Ah, didnt think about that.. Maybe it would be better if I removed it (on the other hand, ctypes is also new in 2.5 even if its possible to download for 2.4 and 2.3)?

Note: There is a mem leak in the demo. It doesnt do free the shapes and bodies, it just removes them from the space. No big deal in the demo (except that it was intended to show how you do stuff), you will have to add a lot of boxes and balls before it become a problem ;-) (This is why I like to program in python and not C/C++!)

@viblo/Note: Changing the removal lines to this should fix the problem for the polygons, right?

cp.cpSpaceRemoveShape(space, poly)

cp.cpSpaceRemoveBody(space, body)
cp.cpShapeFree(poly) # added
cp.cpBodyFree(body)
 # added
polys.remove(poly)
(Somehow the code's whitespace got mangled when I posted it, but it's still readable so I won't repost.)
neko: yep.
In case anyone happens to read this page in search for pymunk: see pymunk.googlecode.com for the latest version, the one linked here is very outdated.