Game Development Reference
In-Depth Information
mConfig.mPuck.x = mPuck.GetPosition().x * DISTANCE_RATIO;
mConfig.mPuck.y = mPuck.GetPosition().y * DISTANCE_RATIO;
}
Surprisingly simple, you say? This is where the beauty of Box2D
really shines. Once the simulation has been configured correctly,
you simply tell the world to advance or Step ,passingitthedelta
time (in this case, a fixed 1/30th of a second), and the number of
collision iterations to perform. The values I
m using for both types of
collisions, 10 and 10, are fairly standard in the Box2D community for
most uses. Basically, the more iterations it performs, the more accu-
rate the simulation and the lower the risk of missed collisions. How-
ever, every iteration adds computation cycles that eat processing
power. You generally want to keep this as efficient as possible, espe-
cially on a mobile device. The ClearForces method is then called to
'
according to the Box2D
manual. In earlier versions, this was done automatically by the step-
ping process, but it is now done manually so that you can perform
multiple steps before resetting forces. After the two commands of
the physics simulation are called and all of the bodies have been
subsequently updated, it
clear any forces you applied to bodies,
s time to adjust the positions of our display
objects. The next six lines of code simply update the x and y coordi-
nates of the two paddles and the puck.
With only the update function in place, our game would run
just fine since all of the heavy lifting is done by Box2D. However,
as is, we have no way of knowing when a player lands the puck in
their opponent
'
sgoal.Ifyourecallfromthe Game class, we called
a method named checkForCollisions that does just that.
'
public function checkForCollisions():void
{
for ( var collision:b2Contact = mPhysicsEngine.
GetContactList(); collision; collision = collision.
GetNext() )
{
mCollisionBodies[0] = collision.GetFixtureA().
GetBody();
mCollisionBodies[1] = collision.GetFixtureB().
GetBody();
if ( mCollisionBodies.indexOf( mPuck )
>
-1 &&
mCollisionBodies.indexOf( mGoal1 )
>
-1 )
{
if ( mConfig.mPlayer2ScoreCallback != null )
{
mConfig.mPlayer2ScoreCallback();
break;
}
Search Nedrilad ::




Custom Search