Game Development Reference
In-Depth Information
in games, it was a value that after some testing
Often,
real-world accuracy is not what is desired in a physics simulation, so
elements like this will require some futzing with values to get the
desired feel. After creating the joints for the paddles, we simply con-
vert the other display objects passed into the config. We
felt right.
ll now look
at these two conversion functions and how they work. They
'
'
re very
similar overall, so we
ll start with the displayObjectToBox and then
look at the differences when we need a circle instead.
'
private static function displayObjectToBox( _displayObject:
DisplayObject, _world:b2World ):b2Body
{
// Create body def
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_staticBody;
var rect:Rectangle = _displayObject.getRect(_displayObject.
parent);
bodyDef.position.Set( (rect.left + rect.width/2) /
DISTANCE_RATIO, (rect.top + rect.height/2) / DISTANCE_
RATIO );
// Create shape and fixture
var boxShape:b2PolygonShape = new b2PolygonShape();
boxShape.SetAsBox( ( rect.width /2)/DISTANCE_RATIO,
( rect.height /2)/DISTANCE_RATIO );
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = boxShape;
fixtureDef.density = .5;
fixtureDef.friction = 1;
fixtureDef.restitution = 0.1;
// Create body
var body:b2Body = _world.CreateBody( bodyDef );
body.CreateFixture( fixtureDef );
return body;
}
For all of the boxes in this engine, we just need them to provide
fixed boundaries, so we can conveniently define all of the box body
types as static. We use the getRect method of the DisplayObject
we
ve used in previous chapters to get the dimensions of the box.
We then use this rectangular object to set the position and shape of
the body. The shape object we use, which is a generic b2Polygon-
Shape, has a convenient SetAsBox method that is used to quickly
define a rectangular shape. You may notice that we pass the SetAs-
Box method half the width and height of the rectangle. This is
because the box will be centered on its position and the method
wants to know how far out from the center to travel. Next, we create
a fixture object that defines the physical properties of the walls. As
these objects are static, the only real value we
'
'
re concerned with is
Search Nedrilad ::




Custom Search