Game Development Reference
In-Depth Information
fixtureDef.friction=0.5;
var physicsBird:b2Body=world.CreateBody(bodyDef);
physicsBird.CreateFixture(fixtureDef);
physicsBird.SetLinearVelocity(birdVelocity);
removeChild(theBird);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,birdMove);
stage.removeEventListener(MouseEvent.MOUSE_UP,birdRelease);
}
Finally, we are adding some forces, so it's time to explain some lines:
var distanceX:Number=theBird.x-slingX;
var distanceY:Number=theBird.y-slingY;
distanceX and distanceY are two numbers representing the distance between the
bird and the center of the sling. The greater the distance, the stronger is the force that
will be applied.
var velocityX:Number=distanceX*-1/5;
var velocityY:Number=distanceY*-1/5;
We said the force is related to the distance, so here is how horizontal and vertical
velocities are determined. First, we have to multiply them by -1 because force has
to be the opposite of the distance, or the bird will be thrown in the wrong direction.
Then we divide the results by 5 just to make it a bit weaker, or we will be firing a
supersonic bird.
Changing this value will affect gameplay, so play a bit with it to see what happens.
var birdVelocity:b2Vec2=new b2Vec2(velocityX,velocityY);
Finally, velocity variables are used to build the b2Vec2 object that will assign the
force to the bird.
bodyDef.type=b2Body.b2_dynamicBody;
Obviously, before we can apply a force to a body, we must set is as dynamic.
physicsBird.SetLinearVelocity(birdVelocity);
And now the bird can take off and destroy the pig's nest. I used SetLinearVelocity
because I want to give different kinds of birds the same velocity if they are released
at the same point, no matter their size or mass.
Obviously, I could have used ApplyForce or ApplyImpulse by multiplying with
birdVelocity as I had showed at the beginning of the chapter, but why bother? I
have my SetLinearVelocity method that fits like a glove, and that's it!
 
Search WWH ::




Custom Search