I’ve been hard at work at my project which I’m yet to call by name on here (and will continue to be that way until I have a playable demo.) First, a status update. I’ve ditched the idea of doing the entire engine myself and have now migrated to HGE (Haaf’s Game Engine) which fairly recently had a port to *nix and OS X by Ryan C. Gordon. I’ve worked a bit on the engine itself after the source release and my changes were pulled in upstream (mostly it adding a CMake build system, but I also did some things like get the tutorial/examples working, silenced warnings, etc.) Basically, I was pretty impressed with the engine and its features. Using OpenGL for 2D rendering was something I had kind of wanted to do, but had decided to just do software SDL rendering. That, in addition to being a pretty nice all-in-one 2D engine, had me sold on it. I was able to pretty quickly rip out the SDL work and put HGE in its place. What I currently have is a player that has basic animations. When I say basic, I mean, you press left, and one animation is played. You press up, and another is played. I also have the level layout rendering, and can change the (currently single) level file and have the changes take effect on the next run.
At this point I thought it would be prudent to the project to take the plunge into 2D physics engines. It was either that, or implement some basic physics myself (which wouldn’t be too big of a deal in this case, at least to begin with.) So, I did some research. The first one that I knew of off the top of my head was Box2D. The other one I found for this project was Chipmunk Physics. Honestly, this isn’t going to be a comparison between the two, because quite frankly, I didn’t even give Box2D a proper try. Not to mention that after I did a few tests with Chipmunk, it seemed like a good fit. It has a very easy to use C API, a very permissive license (MIT), it seems quite fast, and like I said, it seems like it’ll be a good fit.
The first thing I did was do a quick test with a single box that was affected by gravity. It was simple enough to implement. Next, I put the logic for the box into a class that would then take care of the Chipmunk memory management in the constructor and destructor. So far, so good. Next I added these boxes to a vector dynamically when the mouse is clicked. I then observed how the boxes reacted as they fell and collided with each other. This all seemed to work well, which made me want to try to add some things like being able to clear out the boxes without having to restart the program, giving all the boxes a random velocity, and simulating a explosion at the mouse pointer on a click. All-in-all, this all went together fairly quickly (few hours at most.)
I thought this simple demo might be of interest to others as well. So I’m going to post the code here. It requires HGE (I’ve only tested against hge-unix) and Chipmunk. Other than that, it require C++ and uses std::vector for dynamic storage. All the features I described above are implemented, and it comes in at just under 300 lines of code. The buttons for various actions are as follows:
- Space: clears all boxes from the screen
- Left click: adds boxes at the current mouse possition
- Right click: gives all boxes a random velocity
- Middle click: simulate an explosion at the mouse’s location
- Mousewheel: Increases/decreases size of boxes spawned (v0.2+)
- Backspace: Removes some of the most recent boxes (v0.2+)
- Delete: Removes some of the first boxes spawned (v0.2+)
- c: Changes from box->circle or circle->box (v0.3+)
0.1 Initial release
0.2 Added proper box rotation, ability to remove boxes (from front or back of vector), ability to change box size, and varying box mass based on size.
0.3 Ability to add circles as well (requires the image be in the same directory as the executable.)
0.3.1 final This is most likely the final release. It basically just corrects the mass of the objects.
0.3.2 Very minor update to CMake FindHGE.cmake to define HGE_HELPERS_LIBRARY as well and HGE_LIBRARIES (instead of HGE_LIBRARY) includes both.