Game Development Reference
In-Depth Information
Creating an oriented box shape
To create an oriented box shape, we will use a b2PolygonShape method similar to
SetAsBox , yet more advanced and called the SetAsOrientedBox method.
The arguments are the width and height of the box, the center of the box, also
defined as a b2Vec2 object, and the rotation in radians.
1.
Applying this concept, the idol function continues in the following way:
var bW:Number=5/worldScale;
var bH:Number=20/worldScale;
var boxPos:b2Vec2=new b2Vec2(0,10/worldScale);
var boxAngle:Number=- Math.PI/4;
The first two lines are quite understandable as we are defining the size of the
box, which is same as the box we just created.
Looking at the third line, where I define the position, you could have some
doubts. The center of the body of the first idol box was (320,242) in pixels,
so why am I placing the second idol box at (0,10) ? Shouldn't it be placed
near to the first idol box?
That's the magic of compound objects you are about to learn. Now we aren't
defining the position as absolute, but as relative to the position of the body of
the first idol box.
So the meaning of the line is: the box will be placed a little below the center
of the body.
The last line just specifies the angle in radians, 45 degrees counterclockwise.
2.
With these four variables, you can call the SetAsOrientedBox method in the
following way:
polygonShape.SetAsOrientedBox(bW,bH,boxPos,boxAngle);
3. Then, as usual, we need to update the fixture shape:
fixtureDef.shape=polygonShape;
4.
And now, the magic. Rather than creating a new b2Body object, we attach the
fixture to the existing theIdol body:
theIdol.CreateFixture(fixtureDef);
5.
If we apply the same concept for the other box, we need to change the
boxAngle variable:
boxAngle=Math.PI/4;
 
Search WWH ::




Custom Search