Game Development Reference
In-Depth Information
public class Main extends Sprite {
private var world:b2World;
private var worldScale:Number=30;
public function Main() {
world=new b2World(new b2Vec2(0,9.81),true);
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,30/worldScale);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
private function updateWorld(e:Event):void {
world.Step(1/30,10,10);
world.ClearForces();
}
}
}
Also, notice how I created the world and called the Step method. It's just to
save some lines of code.
Once you're done with the creation of a body definition, it's time to give it a shape.
Creating a circular shape
A shape is a 2D geometrical object, such as a circle or a polygon, which in this case
must be convex (every internal angle must be less than 180 degrees). Remember,
Box2D can only handle convex shapes.
At the moment, we are starting with the ball, so we'll create the circle:
var circleShape:b2CircleShape;
circleShape=new b2CircleShape(25/worldScale);
b2CircleShape is used to create a circular shape, and its constructor wants the
radius as an argument. With the previous lines, we are creating a circle whose
radius is 25 pixels, thanks to our friend—the worldScale variable. From now on,
every time you want to work with pixels, you'll have to divide them by worldScale .
You can also define a function called, let's say, pixelsToMeters , and call it every
time you need to convert pixels to meters.
When we have a body definition and a shape, we can glue them together using
a fixture.
 
Search WWH ::




Custom Search