Adding the AndEngine Box2D Physics Extension to an AndEngine project

I’ve talked about AndEngine before as a great framework for building 2D games on the Android platform. Today, I will be explaining how to include the AndEngine Box2D extension within an AndEngine project so that you can start implementing realistic physics within your game.

The AndEnginePhysicsBox2DExtension, provides an implementation of the popular C++ Box2D physics framework for AndEngine. Getting this doohicky integrated with your current AndEngine project can be a little tricky, however, and that’s what I’m going to explain today.

First, you need to have an Eclipse project with AndEngine included in any way you prefer. Note that you’ll have to have AndEngine as a library project in Eclipse before you begin, as the Box2D Extension will need it as well.

The next step is to clone the Github repository for the AndEnginePhysicsBox2DExtension.

Git Clone the Box2D Extension
Git Clone the Box2D Extension

Now, import the extension as a Library project to your Eclipse workspace. (Right click on workspace -> Import -> Android -> Android Project from Existing Code).

Import the extension as existing Android code to your workspace
Import the extension as existing Android code to your workspace

Once you’ve imported the extension, it will immediately build a set of JARs and various other binary files. Now, there are two ways to include the extension in to an existing AndEngine project:

  1. Include it as a library project (recommended) :
    1. Make sure your AndEnginePhysicsBox2DExtension workspace project is pegged as a library. You should be be able to check this by right clicking on the extension project, going to properties, and checking whether ‘Is Library’ is ticked under the Android section.
      Make sure that your extension project is a library
      Make sure that your extension project is a library

      Now, right click on your existing AndEngine project and go to Properties and go to the Android Section. In the libraries box at the bottom, you should be able to add the AndEnginePhysicsBox2DExtension as a library.

      Add the extension as a library to your AndEngine project
      Add the extension as a library to your AndEngine project
  2. Add AndEnginePhysicsBox2DExtension binaries to your libs folder:
    1. As I mentioned earlier, your AndEnginePhysicsBox2DExtension workspace project will build a set of extension binaries in the /bin folder as soon as you import it. Although it is not recommended by the developer of AndEngine, you can copy over these files to your AndEngine project’s /libs folder.
      Copy Physics Extension JAR files to your /libs folder
      Copy Physics Extension JAR files to your /libs folder


Now that you’ve included the AndEnginePhysicsBox2DExtension within your AndEngine project, you can proceed to write awesome physics code in to your game, like including the gravity of the first Death Star within the game’s environment!

Use the gravity of the first Death Star in your game! This is actually an Android thing, not an AndEngine thing.
Use the gravity of the first Death Star in your game! This is actually an Android thing, not an AndEngine thing.

Some Physics functions in AndEngine GLES1.0 + Box2D

AndEngine is one of the best game engines available to build 2D games on the Android platform. It has a load of great features, one of which its integration with the Box2D physics engine. This is done via a great extension.

There are many sources out there to help developers with integrating physics features in to AndEngine, including examples from the author of the engine himself, however, I thought of writing my own helpful guide with some of the lesser-explained functions of AndEngine’s physics engine.

You’ll be able to use this awesome blog post by Maya Posch to set up AndEngine and get it configured with the AndEngine Physics Box2D Extension. It’s a bit complicated, so if you’re lazy like me you could find the compiled sources from the previously-linked Examples repository (though this is NOT recommended).

Now, to get started on some of the functions that you’ll need. The Box2D environment, when oversimplified, contains the following main elements:

  • Physics World: This is where all the exciting business goes on, and where all the elements related to physics are stored and manipulated. Welcome to the world of physics!
  • Bodies: Bodies are similar to sprites in any game; they’re what move about, crash in to each other etc.
  • Vectors: These are what you’ll be applying on your bodies (you do know your basic physics, right?) to make them move around, rotate, stop, and every other conceivable thing.

Basics aside, we’ll look at a few functionalities that are rarely explained in most AndEngine physics examples:

  1. Contact Listeners:

Contact listeners are Box2D’s equivalent to collision detection in game engines. A ContactListener is defined for the Physics World in general, and will handle all contacts that happens in that world. That means, when a contact listener fires, you’ll only get the Fixtures (and underlying Bodies) of that particular contact to manipulate. Handling contact listeners in this way can get pretty dirty pretty fast, it turns in to a mess of checking fixtures for their underlying attributes and rechecking to see if they’re the actual body within the physics world that you want to check for collisions. Well, you did want to have smooth physics in your game, right? Now pay the price. 😉

Following is a code snippet from a car racing game I worked on:

mPhysicsWorld.setContactListener(new ContactListener() {

			public void preSolve(Contact contact, Manifold oldManifold) {


			public void postSolve(Contact contact, ContactImpulse impulse) {


			public void endContact(Contact contact) {


			public void beginContact(Contact contact) {

				if ( contact.getFixtureA().getBody().getUserData() != null &&
						contact.getFixtureB().getBody().getUserData() != null  ) {
                                        //I stored a 'Car' object in the userdata of each physics body as I created it
					final Car carA = (Car)contact.getFixtureA().getBody().getUserData();
					final Car carB = (Car)contact.getFixtureB().getBody().getUserData();
					if ( carA.getId() == 1 || carB.getId() == 1 ) {
						carState = CAR_STATE_CRASHED;

By returning the bodies associated with each fixture in teh contact, I have managed to get the ‘Car’ type objects stored within the userdata elements within each body. By checking a specific value within these car objects, I have decided what to do next. Any contact handling that you’d do with AndEngine + Box2D would have to be a lot of if-else clauses where you check whether the bodies crashing together within the physics world are the bodies you want to check for collisions with. The presolve() an postsolve() methods are concerned with preparing the physics world for the contact, where you can set various attributes like whether contacts are enabled at that point.

2. Setting your body’s velocity to 0:

This might seem like a weird requirement, but believe me, after your sprites start bouncing around the screen like crazy ping pong balls, you’ll thank me. This is something it took me a while to figure out, so I thought I’d share:

Vector2 linearZero = Vector2Pool.obtain(0,0);
Vector2Pool.recycle(linearZero);//don't forget to recycle your vectors! It helps save the environment.

Ok, so you have your playerBody right? What you do is, you initialize a vector with 0 values, and you apply that as the linear velocity to your body. That will stop the body from moving on all plains.

3. Insta-move your sprite to any place on the screen:

Sometimes, even when you have a physics engine at your disposal, you just want to magically materialize your sprite somewhere on the screen. The problem is, if you use the normal AndEngine sprite.setPosition() method, the physics body you’ve attached to the sprite is not going to move with it. You need to use Box2D itself to insta-move your sprite (once you move the body, the sprite will always move with it, but not vice-versa):


Vector2 transformVector = Vector2Pool.obtain(newXPosition, newYPosition);
playerBody.setTransform(transformVector, playerBody.getAngle());

Vectors again. Simply build a new vector containing the X and Y coordinates you want to move your sprite to, and use the setTransform() method on the body attached to the sprite.


That’s it from me for now. More soon!