There are some really exciting demos that demonstrate the kinds of things you can do with the chipmunk rigid body physics library. I hope these inspire you to check it out. Here is a chipmunk implementation of crayon physics that while not quite as aesthetically pleasing as crayon physics deluxe is still really, really cool. Here is one using chipmunk to model the behavior of fluids.
Here is a pretty dramatic one toppling a huge domino pyramid, and here are a handful of others you might be interested in. You can use chipmunk in Ruby, Python, C, C++ on Linux, Windows (so I hear), and Mac. Enjoy!
Monday, April 14, 2008
Thursday, April 10, 2008
Monday, April 7, 2008
Tuesday, April 1, 2008
My Favorite Chipmunk is Alvin Nomore.

So maybe you read my previous post about writing games in ruby. I had found a library for ruby called Gosu that makes game development fun and easy. Most of the games I am interested in writing involve physics in one way or another, be that having a kite string that sags realisticly or a balloon that floats and responds to the forces that it comes up against, physics is fundamental, and it can be really tricky to model cleanly and efficiently.
So it was high time I dug into a real physics library. Gosu integrates well with a physics library called Chipmunk. Chipmunk is a 2D rigid body physics library that is fast, numerically stable, and easy to use. I am completely addicted! I've already got a scrolling game with particle explosions, a spaceship, and editable maps a la the Captain Ruby Gosu demo.

Everybody loves a screenshot so there it is. The spaceship has just had an accident and crashed into a whole bunch of map tiles. The red particles are just debris from the explosion. Currently wherever you click you cause an explosion. It is powerful enough to throw blocks around the screen and generally cause chaos. This has nothing to do with how the game will eventually turn out but is more just proof of concept stuff and spikes. You can get the code for this here. If you want this exact version of it check out revision 8. It is changing pretty fast. It is definately time for me to solidify requirements and start working seriously on the game functionality I want.
Enough about the game though. I wanted to mention some of the stumbling points I had while learning about chipmunk to save anyone else the trouble. Here they are in no particular order:
- Don't make up the moment of inertia for objects. When you create a physical body in chipmunk you specify the mass and moment of inertia. Initially I just made up the moment for my objects, but that messes up their behavior and throws off the center of gravity for things. Calculating the moment is a bit of a pain and involves integration, but chipmunk supplies this handy dandy method moment_for_poly on the main chipmunk object. Use it!
- There are rules for specifying shapes in chipmunk. A shape must be convex and you have to specify the verticies in counterclockwise order. Don't forget otherwise your object won't interact with anything or will interact incorrectly.
- One of the complexities you get from using gosu and chipmunk together is that you have to maintain your physical objects in both the chipmunk space and on the screen with gosu. It pays to create an abstraction to hide this composition. If you check out the physical_object.rb module you can see how I handled it. There is probably a better way, but so far it is pretty useful.
- Sometimes you have groups of objects that you don't want to interact. For example in my explosions the interactions were causing the particles to shoot out in two directions instead of radially. The CP::Shape class has a group field that you can set. By default it is zero which means it will interact with everything, but if you set it to another integer it will only interact with things that don't share the same group. So adding all particles of the explosion to the same group makes the explosion work as expected.

Explosion with particle interaction turned off:

Programming with Chipmunk is a riot. If you'd like to give it a play, I highly recommend checking out the examples that ship with gosu. For something a bit more complex you can look at my code. Also be aware that gosu and chipmunk are available in C and C++.
Links:
Subscribe to:
Posts (Atom)