Game Development Reference
In-Depth Information
exactly one unit; this is called a unit vector . With a length of one unit, all you need to
do is to multiply this unit vector by maxSpeed to cap the body's velocity to
maxSpeed .
The last line simply causes the body's rotation to be reset; in other words, the body
never rotates. This is to prevent the ball's sprite from rotating. Because the ball's image
has a highlight and a shadow, you can only create the illusion of a light source shining
on the ball if the highlight and shadow of the ball stay in place.
Because the body's world center is set as the position, the position of the body remains
the same, and only its rotation is updated. Doing so doesn't stop the physical effect of a
spinning ball colliding with or sliding along hard surfaces because that effect is calcu-
lated from the body's angular velocity, which remains unaffected. You can safely reset
the ball's rotation, because from the perspective of the physics engine, the ball's shape
being a circle means that the ball's surface features are the same from any direction. A
circle is completely symmetrical.
Now have a look at the applyForceTowardsFinger method, which makes the
ball accelerate toward the finger, as in Listing 13-10 .
Listing 13-10 . Accelerating the Ball Toward the Touch Location
-(void) applyForceTowardsFinger
{
b2Vec2 bodyPos = physicsBody- > GetWorldCenter();
b2Vec2 fingerPos = [Helper toMeters:fingerLocation];
b2Vec2 bodyToFingerDirection = fingerPos - bodyPos;
bodyToFingerDirection.Normalize();
b2Vec2 force = 2.0f * bodyToFingerDirection;
physicsBody- > ApplyForce(force, physicsBody- > GetWorldCenter());
}
You have the two positions of the body and the finger, and then you subtract the finger
position from the body position. For example, if the body's position were at the screen
center (160, 240), and the finger is touching near the upper right-hand corner of the
screen at (300, 450), then subtracting the body position from the finger position, being
the subtraction of the individual x and y coordinates, results in (300 - 160, 450 - 240) =
 
 
Search Nedrilad ::




Custom Search