(For more resources related on Spring, see here.)
A fixture is used to bind the shape on a body, and to define its material setting density, friction, and restitution.
The first step is to create the fixture:
var fixtureDef:b2FixtureDef = new b2FixtureDef(); fixtureDef.shape=circleShape;
Once we have created the fixture with the constructor, we assign the previously created shape using the shape property.
Finally we are ready to add the ball to the world:
var theBall_b2Body=world.CreateBody(bodyDef); theBall.CreateFixture(fixtureDef);
b2Body is the body itself: the physical, concrete body that has been created using the bodyDef attribute.
To recap, use the following steps when you want to place a body in the world:
i. Create a body definition, which will hold body information such as its position.
ii. Create a shape, which is how the body will look.
iii. Create a fixture to attach the shape to the body definition.
iv. Create the body itself in the world using the fixture.
Once you know the importance of each step, adding bodies to your Box2D World will be easy and fun.
Back to our project. The following is how the class should look now:
package {
import flash.display.Sprite;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Bo x2D.Common.Math.*;
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);
var circleShape:b2CircleShape;
circleShape=new b2CircleShape(25/worldScale);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape=circleShape;
var theBall_b2Body=world.CreateBody(bodyDef);
theBall.CreateFixture(fixtureDef);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
private function updateWorld(e:Event):void {
world.Step(1/30,10,10);
world.ClearForces;
}
}
}
Time to save the project and test it. Ready to see your first Box2D body in action? Run the movie!
Ok, it did not display anything. Before you throw this article, let me tell you that Box2D only simulates the physic world, but it does not display anything.
This means your body is alive and kicking in your Box2D World; it's just that you can't see it.
Let's perform the following steps:
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.
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.
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 fl oor.
Now we change the shape attribute of the fixture definition, attaching the new polygon shape:
fixtureDef.shape=polygonShape;
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);
The following is how your Main function should look now:
public function Main() {
world=new b2World(new b2Vec2(0,9.81),true);
var bodyDef_b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,30/worldScale);
var circleShape:b2CircleShape;
circleShape=new b2CircleShape(25/worldScale);
var fixtureDef_b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=circleShape;
var theBall_b2Body=world.CreateBody(bodyDef);
theBall.CreateFixture(fixtureDef);
bodyDef.position.Set(320/worldScale,470/worldScale);
var polygonShape_b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,10/worldScale);
fixtureDef.shape=polygonShape;
var theFloor_b2Body=world.CreateBody(bodyDef);
theFloor.CreateFixture(fixtureDef);
var debugDraw_b2DebugDraw=new b2DebugDraw();
var debugSprite_Sprite=new Sprite();
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(worldScale);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
debugDraw.SetFillAlpha(0.5);
world.SetDebugDraw(debugDraw);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
Test the movie and you'll see the floor: