Game Development Reference
In-Depth Information
restitution, which is how much bounce occurs when things hit the
walls. A value of 0 would mean that no bounce occurred. A value
of 1 would mean that a bounce of exactly the same magnitude of
its incoming force would occur. We
ll see how this applies for the
paddle and puck objects in a moment. The final step is to create a
body in the world using the definition object, and then attach its
fixture.
'
private static function displayObjectToCircle( _displayObject:
DisplayObject, _world:b2World ):b2Body
{
// Create body def
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set( _displayObject.x / DISTANCE_RATIO,
_displayObject.y / DISTANCE_RATIO );
// Create shape and fixture
var circleShape:b2CircleShape = new b2CircleShape(
(_displayObject.height / 2) / DISTANCE_RATIO );
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = circleShape;
fixtureDef.density = .5;
fixtureDef.friction = .5;
fixtureDef.restitution = 1;
// Create body
var body:b2Body = _world.CreateBody( bodyDef );
body.CreateFixture( fixtureDef );
return body;
}
For circle objects like the puck and paddles, we do almost the same
process, except that we use a b2CircleShape instead of a polygon. We
also apply a restitution of 1, so there will be a nice bounce when the
paddles impact the puck. Now that we
'
ve got all of the setup in order,
we
'
ll look at what happens with the engine updated every frame.
public function update( _dt:Number ):void
{
mPhysicsEngine.Step( 1/30, 10, 10 );
mPhysicsEngine.ClearForces();
mConfig.mPlayer1.x = mPlayer1.GetPosition().x *
DISTANCE_RATIO;
mConfig.mPlayer1.y = mPlayer1.GetPosition().y *
DISTANCE_RATIO;
mConfig.mPlayer2.x = mPlayer2.GetPosition().x *
DISTANCE_RATIO;
mConfig.mPlayer2.y = mPlayer2.GetPosition().y *