quinta-feira, 30 de setembro de 2010

CSG operations in 2D

...or "Integrating a Polygon Boolean Operations Library"

I've been developing a prototype for a platform game, using the Chipmunk Physics library to handle the physics. The control mechanics are to be very simple, similar to Super Mario (with almost no inertia, no floor sliding), aiming to allow for precise and responsive control.

After designing the initial prototype, I started creating levels, using square blocks placed next to each other. This resulted in a (that I found later) known problem, where the object for the player will "bump" into the lines where the blocks meet and slow down or jump on them. Ok, so I'll just use continuous collision lines on top of a group of blocks instead of individual collision squares next to each other, so this doesn't happen.

From here, I needed a solution to ease the level creation process. The options were:

- place the blocks images by hand, and then create the collision lines also by hand (easiest to code, but would lead to boring level editing)

- place the blocks, and code an algorithm to find where the blocks were, outlining it with collision lines (would take some thinking, and would basically be a Constructive Solid Geometry problem)

- place the blocks, integrate an existing CSG solution, and join the individual blocks into a single object (most flexible solution, and I was interested in the potential of integrating CSG functionalities into my engine)

So I went to look for CSG algorithms, libraries, etc, and from what I gathered it's a pretty deep field, with several working solutions but with somewhat complicated implementations.
I looked around for CSG specifically for 2D, and found 3 libraries. From those, Clipper by Angus Johnson presented the best feature list and had a free license, so I looked at it.

It's pretty straightforward and I had a test working in a short time, which was great! This allowed me to integrate it easily into my level editor, and the outcome was great. Editing levels is now a lot of fun!

Sem comentários: