Game Development Reference
In-Depth Information
Now, we still need to build its head.
At this time, you should be able to build it by yourself. All in all it's just another
oriented box to be part of the compound object. That's why I will show you another
way to do it, using the most powerful way to create a polygon shape.
Creating any kind of convex polygons
Box2D allows you to create any kind of polygon shape, as long as the polygon is
convex , which means it must have all interior angles less than 180 degrees, so all the
vertices point away from the center, and you provide its vertices in clockwise order.
1.
First, let's create a vector to store all vertices:
var vertices:Vector.<b2Vec2>=new Vector.<b2Vec2>();
2.
Then we will push all vertices in clockwise order, as b2Vec2 objects,
specifying the relative position of the vertices to the center of the idol body.
vertices.push(new b2Vec2(-15/worldScale,-25/worldScale));
vertices.push(new b2Vec2(0,-40/worldScale));
vertices.push(new b2Vec2(15/worldScale,-25/worldScale));
vertices.push(new b2Vec2(0,-10/worldScale));
3.
The previous lines of code are the four vertices of idol's head. Now let's turn
this vector into a polygon shape:
polygonShape.SetAsVector(vertices,4);
The SetAsVector method turns any vector of clockwise vertices into a
polygon shape. The second argument just represents the number of vertices
to consider.
4.
Finally, as usual, you need to update the fixture shape and add it to the
theIdol body:
fixtureDef.shape=polygonShape;
theIdol.CreateFixture(fixtureDef);
5.
The following is how the idol function looks now:
private function idol(pX:Number,pY:Number):void{
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(5/worldScale,20/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=1;
 
Search WWH ::




Custom Search