Category Archives: Box2d

Box2d Demo v1.2

Finally got around to updating the Box2d demo to version 1.2, while I wait for my Froyo update from Google (hurry up!). There is so much functionality in Box2d, I’m barely scratching the surface of what can be simulated. So what’s changed in the new version?

  • Uses the menu to change objects to add to the scene
  • Allows the user to edit the scene by toggling the edit mode and drawing static lines that the dynamic objects can hit.
  • Has a link to Bay Nine Studios from the menu.

I think this is going to be the last update for the Box2d demo in a while. It’s a fun api to play around with but I really need to get back to other stuff. With the Froyo (Android 2.2) update coming, I’m going to be spending more time learning the new features of OpenGL ES 2.0. In particular, I’d like to learn more about vertex and fragment shaders. This is going to be an exciting time in Android game development, as the platform is becoming a viable competitor to the iPhone OS.

Box2d Demo Updated

Just uploaded a new version of the Box2d demo. Not much change to Nexus One users but now should support Android 1.5 and up. Did some major refactoring of the code so that in the next version the user should be able to draw static objects on the screen and then simulate dynamic objects on those. Code can be found on Google Code.

Now if only I could get the icon on the Android Market to look right.

Box2d Demo on Android Market

Last Thursday I tried putting an app on the Android Market, the Box2d Demo app. The next day to my surprise, I found that 11 people had downloaded it. I really just uploaded it as a test and to easily distribute the app to other Android owners I know. In the meantime I’m frantically work on an update for the app to not be so embarrassed by the uselessness of it. Search for Box2d on the Android Store and you’ll see it, under Bay Nine Studios. Here is also a link to the app on App Brain.

Box2d on Android



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.