Game Development Reference
In-Depth Information
That's it! You managed to see the bodies you have placed into Box2D World.
At the moment the ball does not seem to fall down according to gravity, but don't
worry about it now, we'll fix it later.
Now, let's create something that can be used as a ground, such as a large rectangle to
be placed at the bottom-side of the stage. Everything will be simpler from now on, as
new bodies will be automatically displayed as soon as they are added to the world.
Creating a box shape
Let's perform the following steps:
1. First, body and fixture definitions can be reassigned to define our new
body. This way, we don't need to declare another bodyDef variable, but
we just need to reuse the one we used for the creation of the sphere by
changing its position:
bodyDef.position.Set(320/worldScale,470/worldScale);
Now the body definition is located in the horizontal center, and close to the
bottom of the screen.
2.
To create a polygon shape, we will use the b2PolygonShape class:
var polygonShape:b2PolygonShape=new b2PolygonShape();
This way we create a polygon shape in the same way we created the circle
shape earlier.
3.
Polygon shapes must follow some restrictions, but at the moment because we
only need an axis-aligned box, the SetAsBox method is all we need.
polygonShape.SetAsBox(320/worldScale,10/worldScale);
The method requires two arguments: the half-width and the half-height of
the box. In the end, our new polygon shape will have its center at pixels (320,
470), and it will have a width of 640 pixels and a height of 20 pixels—just
what we need to create a floor.
4.
Now we change the shape attribute of the fixture definition, attaching the
new polygon shape:
fixtureDef.shape=polygonShape;
5. Finally, we can create the world body and embed the fixture in it, just like we
did with the sphere.
var theFloor:b2Body=world.CreateBody(bodyDef);
theFloor.CreateFixture(fixtureDef);
 
Search WWH ::




Custom Search