Mastering AndEngine Game Development
上QQ阅读APP看书,第一时间看更新

Integrating physics with AndEngine

There exists a physics engine extension for AndEngine based on Box2D. As its name implies, Box2D is a 2D physics engine, which means that its simulation occurs solely in a two-dimensional world (x and y axes). This makes it a good match for something like AndEngine, which doesn't normally care about the third dimension anyway.

Box2D's simulation takes place inside a physics world, as it's called. This mirrors AndEngine's scene object in the sense that for every object in a scene, we also have a matching physics object inside the physics world.

Unfortunately, we cannot directly use this Box2D extension with the code we added in the previous chapter to render 3D models. The reason we cannot use Box2D directly with the code from the previous chapter is that the extension's fixtures (connections between visual and physics objects) require that the physics object (a body) is linked with a Java object which is derived from AndEngine's IEntity interface. Fortunately, the changes we need to make this work are relatively minor, as we'll soon see.

It's also possible to use other physics engines with AndEngine. This can have advantages, yet you have to consider the limitations of a 2D engine and its use of sprites. We will discuss this later in the chapter.