Game Development Reference
In-Depth Information
As you can see, we multiply the opposite and adjacent sides with a value of 0.5 so
that the ball is drawn halfway up the rotated barrel.
The second part of the Update method also is an if -instruction:
if (Painter.GameWorld.IsOutsideWorld(position))
Reset();
This part of the method deals with the event that occurs when the ball goes outside
of the game world. In order to calculate if this is true, we added a method called
IsOutsideWorld to the GameWorld class. The goal of this method is to check if a given
position is outside of the game world. We define the boundaries of the game world
by a few simple rules. Remember that the top left of the screen is the origin. An
object is outside the game world if its x -position is smaller than zero, or larger than
the width of the screen. An object is also outside the game world if its y -position
is larger than the height of the screen. Note that we do not say that an object is
outside of the world if its y -position is smaller than zero. Why not? We chose to
do this so that it is possible for a player to shoot a ball in the air and let the ball be
momentarily above the screen before falling down again. Often, you see a similar
effect in platform games, where a character can jump up to disappear partly outside
of the screen, as opposed to falling through the bottom of the screen which generally
means instant death of the character.
If you look at the header of this method, you see that it has a return value of type
bool :
public bool IsOutsideWorld(Vector2 position)
Furthermore, this method has one input parameter of type Vector2 which indicates
the position that needs to be checked.
If we want to check if a position is outside of the screen, we need to know the
width and height of the screen. Unfortunately, this information is only available in
the Painter class, since that class inherits from the Game class and it has a graph-
ics device which can provide that kind of information. Passing along the width and
height of the screen constantly to all methods that need it is also not really con-
venient. Since there will most probably only be a single screen width and height,
we could decide to store this information in a static Vector2 variable in the Painter
class:
static Vector2 screen;
When the graphics device is created, we assign a value to this vector. For ex-
ample, this could be done once in the LoadContent method. For retrieving the
screen dimensions, we can use the Viewport property of the graphics device, as fol-
lows:
screen = new Vector2(GraphicsDevice.Viewport.Width,
GraphicsDevice.Viewport.Height);
Search WWH ::




Custom Search