Game Development Reference
In-Depth Information
if ( mCollisionBodies.indexOf( mPuck ) > -1 &&
mCollisionBodies.indexOf( mGoal2 ) > -1 )
{
if ( mConfig.mPlayer1ScoreCallback != null )
{
mConfig.mPlayer1ScoreCallback();
break;
}
}
}
}
Box2D stores all collisions calculated in a particular step in
essentially an internal array. The information about when two
objects touch and the associated forces is stored in a b2Contact
object. Instead of giving us access to an array to iterate through, we
have to get at this collision data in an unusual way. Basically, calling
GetContactList on the world object returns the first collision it calcu-
lated (rather than an actual list). If there are none, it will return null.
Onceyouhavethiscollision,togetthenextone,youmustcallthe
GetNext method of the contact object. This continues through all
remaining collisions until eventually GetNext will return null. This is
why we have such a nonstandard for loop; once the collision vari-
able is null, the loop will end. For each contact object, we check to
see if the two bodies in the collision are the puck and either of the
goals. If the puck is touching Player 1
s goal, Player 2 scores and vice
versa. At this point, we run the callback function specified in the
configuration earlier and let the game logic take over. We also
assume that no further collisions need to be considered, so we break
from the loop to prevent unnecessary object creation. Now we need
to allow the controller class to update the positions of the paddles.
This is done through the following methods:
'
public function setPlayer1Position( _x:Number, _y:Number ):void
{
mPlayer1Joint.GetTarget().Set( _x / DISTANCE_RATIO, _y /
DISTANCE_RATIO );
}
public function setPlayer2Position( _x:Number, _y:Number ):void
{
mPlayer2Joint.GetTarget().Set( _x / DISTANCE_RATIO, _y /
DISTANCE_RATIO );
}
Both work exactly the same way, setting the target of each asso-
ciated mouse joint to the touch coordinates passed. Updating this
target will make the joint react by exerting a force on the paddle.
Search Nedrilad ::




Custom Search