# Ground for 2D AndEngine game with physics

I am using AndEngine to create a 2D Game for Android platform using Java and AndEngine.

I have learned how to use Tiled editor to create tiled maps. The only problem I am having is that I don't know how to place an object such as a character on the ground, and have it count as the ground.

How do I go about doing this or what is the best way to do this?

• Do you mean your game is a side-scroller and you want some tiles to be "empty" so the character falls down, and some other tiles to be "solid" so the character can walk on them? – Sergey Nov 3 '11 at 5:35
• Yes this is exactly what i want – Troy Walker Nov 8 '11 at 3:35

AndEngine examples have a TMX Map Example which, among other things, demonstrates how to place a character on the map and how to determine in which map cell the character is currently in.

From there it should be simple(-ish) - you need to designate some map tiles as "empty" and some as "solid" - TMX format allows you to specify properties of tiles so I guess that's what I would use. Then you write a function which checks if your character is in "mid-air", an if so, the character should begin falling. In pseudocode:

if (map.isEmptySpace(character.getX(), character.getY()) {
character.setVSpeed(character.getVSpeed() + GRAVITY);
}


I assume you are using Box2D Physics extension in AndEngine.

1. Design your level as you wish, paint tiles in the Map layer.
2. Create an object layer and set a "wall" or "ground" property to it. Use rectangles to draw ground.
3. In your code, iterate through the these rectangles and create physics invisible objects that will serve as ground. Something like this:

for (TMXObjectGroup tog : tmxMap.getTMXObjectGroups()) {
if (tog.getTMXObjectGroupProperties().containsTMXProperty(
"type", "wall")) {
for (TMXObject to : tog.getTMXObjects()) {
final Rectangle wall = createWall(to);
scene.attachChild(wall);
}
}
}


I use exactly this and the following method to create the walls (not just ground).

private Rectangle createWall(TMXObject to) {
final Rectangle wall = createPhysicsRectangle(to);
wall.setVisible(false);
PhysicsFactory.createBoxBody(physics, wall, BodyType.StaticBody, Constants.WALL_FIXTURE);
return wall;
}


In my case Constants.WALL_FIXTURE is a non-elastic, high-friction, no mass fixture.