Game Development Reference
In-Depth Information
left. The user isn't always touching the screen, and while she isn't, the player's body is
free to go wherever it wants to go. But once the user touches the screen, you don't want
her having to tap for longer than usual just to cancel out a possible leftward-oriented velo-
city. At the same time, repeated or continued taps should be allowed to increase the hori-
zontal speed toward the right.
Caution It's worth noting that the velocity is a CGPoint data type. CGPoint
is a C struct, not an object reference (pointer). The same is true for CGSize
and CGRect data types. This is why it is required to use CGPointMake rather
than just assigning physicsBody.velocity.x = 0.0; . That code
would generate a compile error: “Expression is not assignable.” That's because
a struct field like velocity.x isn't a property in the Objective-C sense—it
has no property setter method.
The applyImpulse: method uses the previously defined nudge variables as the im-
pulse vector. Internally, applyImpulse: updates the body's velocity by multiply-
ing the impulse with the inverse of the body's mass:
[physicsBody
applyImpulse:CGPointMake(_playerNudgeRightVelocity,
_playerNudgeUpVelocity)];
You can think of an impulse to be like the impact of a billiard queue on the cue ball. You
can do it gently, or you can do it with force. Essentially, an impulse is force applied at a
specific point in time. The effect of an impulse depends solely on the body's mass—if you
increase the body's mass, the same impulse will accelerate the body less than it did before.
If you want to apply an impulse that ignores the body's mass, you simply alter the phys-
icsBody.velocity property directly.
A related concept to applying an impulse is applying a force. A force is an impulse ap-
plied continuously. Taking the previous billiard example, if hitting the cue ball is akin to
applying a force, the cue ball would continue to accelerate over time. A force should be
met with an opposing force such as friction or an on/off switch (for example, a rocket ac-
celerates until it's out of fuel) to keep the physics simulation reasonably balanced. You
don't often see things continue to accelerate past supersonic speeds in the real world. Un-
less you work in the aerospace industry, of course.
Search WWH ::




Custom Search