#### Overview of this book

Physics games are getting more and more popular, and Box2D is the best choice if you are looking for a free, stable and robust library to handle physics. With Box2D you can create every kind of 2D physics game, only coding is not the fun part, but the game itself. "Box2D for Flash Games" will guide you through the process of making a Flash physics game starting from the bare bones and taking you by hand through complex features such as forces, joints and motors. As you are learning, your game will have more and more features, like the physics games you are used to playing. The book analyzes two of the most played physics games, and breaks them down to allow readers to build them from scratch in a step-by-step approach. By the end of the book, you will learn how to create basic primitive bodies as well as complex, compound bodies. Motors will give life to cars, catapults and siege machines firing bullets, while a complete collision management will make your game look even more realistic. If you want to make full Flash games with physics, then Box2D for Flash Games will guide you through the entire process of making a Flash physics game.
Box2D for Flash Games
Credits
www.PacktPub.com
Preface
Free Chapter
Hello Box2D World
Interacting with Bodies
Applying Forces to Bodies
Skinning the Game
Bullets and Sensors
Index

## Defining the Box2D World

Like all worlds, the Box2D World has a gravity , so the first thing you need to do is define world gravity.

1. In your `Main` function, add the following line:

`var gravity:b2Vec2=new b2Vec2(0,9.81);`

This introduces our first Box2D data type: `b2Vec2`.

`b2Vec2` is a 2D column vector that is a data type, which will store x and y components of a vector. As you can see, the constructor has two arguments, both numbers, representing the x and y components. This way we are defining the `gravity` variable as a vector with `x=0` (which means no horizontal gravity) and `y=-9.81` (which approximates Earth gravity).

Physics says the speed of an object falling freely near the Earth's surface increases by about 9.81 meters per second squared, which might be thought of as "meters per second, per second". So assuming there isn't any air resistance, we are about to simulate a real-world environment. Explaining the whole theory of a falling body is beyond the scope of this book, but you can get more information by searching for "equations for a falling body" on Google or Wikipedia.

2. You can set your game on the move with the following line:

`var gravity:b2Vec2=new b2Vec2(0,1.63);`

You can also simulate a no gravity environment with the arguments set at `(0,0)`:

`var gravity:b2Vec2=new b2Vec2(0,0);`

### Note

Working with no gravity is also useful if you want to work in a top-down environment.

3. We also need to tell if bodies inside the world are allowed to sleep when they come to rest, that is when they aren't affected by forces. A sleeping body does not require simulation, it just rests in its position as its presence does not affect anything in the world, allowing Box2D to ignore it, and thus speeding up the processing time and letting us achieve a better performance. So I always recommend to put bodies to sleep when possible.

### Note

Sleeping bodies won't sleep forever and they'll wake up as soon as a collision occurs or a force is applied directly on them.

4. Add the following line, which is just a simple Boolean variable definition:

`var sleep:Boolean=true;`
5. And finally, we are ready to create our first world:

`var world:b2World = new b2World(gravity,sleep);`
6. Now we have a container to manage all the bodies and perform our dynamic simulation.

7. Time to make a small recap. At the moment, your code should look like the following:

```package {
import flash.display.Sprite;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite {
public function Main() {
var gravity:b2Vec2=new b2Vec2(0,9.81);
var sleep:Boolean=true;
var world:b2World = new b2World(gravity,sleep);
}
}
}```

Now you learned how to create and configure a Box2D World. Let's see how can you simulate physics in it.