Game Development Reference
In-Depth Information
public void Update(GameTime gameTime)
{
if (shooting)
{
velocity.X
=0.99f;
velocity.Y += 6;
position += velocity
( float )gameTime.ElapsedGameTime.TotalSeconds;
}
else
{
Color = Painter.GameWorld.Cannon.Color;
position = Painter.GameWorld.Cannon.BallPosition
Center;
}
if (Painter.GameWorld.IsOutsideWorld(position))
Reset();
}
As you can see in the header of the Update method, it has one parameter: the
game time. Having the current time is very useful in many games. In this case we
are going to use the passed time to calculate what the new position of the ball should
be.
Looking at the body of the method, we can see that the first part consists of an
if -instruction. The condition of the if is that the shooting member variable should
have the value true . So, if the ball is currently shooting, then the body of the if -
instruction will be executed. This body again consists of three instructions. The
first two instructions update the velocity, the last one updates the position. The first
instruction updates the x direction of the velocity. We multiply the velocity with
a value of 0 . 99, the effect of which is that the velocity slowly decreases. This is
done to simulate air friction. The second instruction slightly increases the y velocity
in each update. This is done to simulate the effect that gravity will have on the
ball. Together, the velocity changes in the x and y direction result in plausible ball
behavior. Finally, the current position of the ball is updated by adding the velocity
to it.
Of course, in the real world, the gravity is not 6 and the same goes for the air
friction. But then again, our real world does not consist of pixels either. Physics in
game worlds does not always accurately represent physics in the real world. When
you want to incorporate some form of physics in your game (be it very simple or
extremely complex), the most important part is not that the physics is realistic, but
that the game is playable . These parameters have a huge influence on the gameplay.
Try, for example, to give them different values and see what happens. You will
note soon that the range of values that produce a game that is playable is not very
big.
The next line is very important:
position += velocity
( float )gameTime.ElapsedGameTime.TotalSeconds;
 
Search WWH ::




Custom Search