Game Development Reference
In-Depth Information
mPuck = displayObjectToCircle( mConfig.mPuck,
mPhysicsEngine );
mPuck.SetLinearDamping( .8 );
mPuckOrigin = mPuck.GetPosition().Copy();
mGoal1 = displayObjectToBox( mConfig.mPlayer1Goal,
mPhysicsEngine );
mGoal2 = displayObjectToBox( mConfig.mPlayer2Goal,
mPhysicsEngine );
for each (var boundary:DisplayObject in mConfig.
mBoundaryList)
{
displayObjectToBox( boundary, mPhysicsEngine );
}
mCollisionBodies = new Vector.
<
b2Body
>
(2, true);
}
When setting up a b2World object, you can define certain prop-
erties of it, such as gravity and whether objects are allowed to
sleep (that is, be removed from the calculations of the simulation if
they
re not moving). In our case, since the gravity of our game is
downward through the z -axis of the board, we don
'
twantthephy-
sics engine to apply gravity. Passing an empty b2Vec2 gives us a
gravity of 0. The next few lines create the body objects for the pad-
dles and copy their original position for the origin objects. You
'
ll
notice that this process uses a method called displayObjectToCircle .
This is a static function we
'
ll look at shortly. It takes a display
object and the corresponding b2World object and performs all the
processes of
'
the visual representation into a mathe-
matical circle. For the walls, we
converting
ll use a very similar function, dis-
playObjectToBox , which works for rectangular objects.
After defining the bodies for the paddles, we need joints to attach
themtotheworld,sotheyhaveareactiontoinput.Thisisdoneby
creating a new joint definition of the type b2MouseJointDef. Joints
have to have two bodies to work, so the first body we supply is the
world
'
and the second is the paddle to which it will attach.
The target of a joint is the position to which it wants to move. When
reading input later on, we
'
s
body
'
ll change this target vector to reflect the
position of the player
s finger. This change in target will cause the
joint to react, exerting a force on the paddle body. The last thing we
dobeforecreatingthejointintheworldistospecifya maxForce
property, which, according to the Box2D manual, is
'
used to prevent
violent reactions when multiple dynamic bodies interact.
Think
of it as a throttle on the force you can exert. I wish I could say
thevalueof300timesthemassofthepaddlewasavalueIcalcu-
lated carefully. Unfortunately, like so much of working with physics
Search Nedrilad ::




Custom Search