
I used Pocket Physics on the NDS and thought it was pretty neat. That got me looking at Box2d, an open source 2d physics engine for games. During the initial tech demos of this Android game I’m working on (yet to be named or previewed), I thought it might be interesting to use Box2d as the physics engine. Or at the very least just to do some neat effects or collision detection, there is something visually appealing seeing accurate physics simulated. Box2d is originally in C++ but has numerous ports, a quick Google search of Android and Box2d, I came across this posting at 4 Feet Software http://www.4feets.com/2009/03/2d-physics-on-android-using-box2d/”
Great post to get started with Jbox2d (Java port of Box2d) on Android. Unfortunately, the code was not able to run as it’s posted on the page and needed a few tweaks. People in the comments pointed some of the tweaks out.
This tutorial basically ports the tutorial code from the original Box2d tutorial to Android. The one problem is the code (in both this one and the original tutorial) puts nothing to the screen. So this required me to figure out something quick and easy to draw primitives to the screen. Canvas seemed to do the trick, and I got it working with Canvas. But only circles would work right, as the draw box routines did not rotate the boxes (rotated cirlces always look the same). Note, I now know that I can do matrix transforms using Canvas but found that out well after I was into my elbows in OpenGL. OpenGL it is.
The source can be found on Google Code here…
http://code.google.com/p/bayninestudios/source/browse/#svn/trunk/AndroidBox2dDemo
A bit of warning, It only has been run on the Nexus One. It has a large amount of code which is dependent on the Nexus One resolution.
What originally started out as putting circles and boxes to the screen and running them through Box2d, has turned into
- Using the touch screen to add objects
- Checking the accelerometer to determine gravity
- Adding a third type (long boxes)
- Texture mapping the objects
- Using the bottom of the screen to switch which objects to drop
- Showing which object is selected to drop
And there is more that I’d like to do still (as of the writing of this entry)…
- Add more static objects to collide with
- Texture map the long box
- Link objects together in Box2d
- Clean up how static objects are created in the world and drawn (currently disconnected and hard coded)
- Make it run on all Android handsets
- Actually indicating that the bottom of the screen is part of the interface
- The SensorListener is deprecated, need to use SensorEventListener instead
Hopefully someone finds the code useful. If anything it is a neat tech demo and it’s helped me learn a bit in Box2d and Android.