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 ...

Get Box2D for Flash Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.