Home > Actionscript 3, Box2D > Setting Up The Box2D B2World Object

Setting Up The Box2D B2World Object

When coding Box2D the first thing that needs to be setup is the Box2D world, known as the b2World.

Inside the b2World object, we set the attributes for:

  • Size – How large we want our b2World to be.
  • Gravity – How strong we want our gravity to be.
  • Sleeping Object – Used to speed our app.

Here is an example of how to set up a b2World:

public function setupWorld ():void
{
// Setup our universe size
var universeSize:b2AABB = new b2AABB();
universeSize.lowerBound.Set (-3000 / RATIO ,-3000 / RATIO);
universeSize.upperBound.Set (3000 / RATIO ,3000 / RATIO);

// Setup our gravity
var gravity:b2Vec2 = new b2Vec2(0 , 9.8);

// Setup sleeping
var ignoreSleeping:Boolean = true;

// Create our world
myWorld = new b2World(universeSize,gravity,ignoreSleeping)
}

Firstly we set our attributes for the size of our world, the universe size is set using a class called the b2AABB. It is basically a rectangle and the parameters UpperBound and LowerBound must be Set. In this example we our applying physics between -3000 px and 3000 px.

After that we must set up our gravity. The gravity is of a b2Vec2 class. The parameters required are X and Y. In this example, as all box2D lengths and in meters,the gravity has been set to 0 meters per second right and 9.8 meters per second down.

Next we have to create a variable to tell box2D whether to ignore sleeping objects. This is mainly just for performance. When an object is not moving, it is classed as sleeping. When an object is sleeping box2D can ignore it for performance increase. Usually, you’ll want this on.

Lastly we must actually create our world. This is of a b2World class and has the parameters:

  • world:b2AABB
  • gravity:b2Vec2
  • doSleep:Boolean

That sums up our tutorial on setting up the Box2D B2World object, in our next tutorial in this series we will look at how to create static objects in box2D

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Twitter
Categories: Actionscript 3, Box2D Tags:
  1. No comments yet.
  1. No trackbacks yet.