Game Development Reference
In-Depth Information
the player sprite is currently in.
PointF TL = new PointF(m_Player.PositionX +
0.25f, m_Player.PositionY + 0.25f); // Top left
corner
PointF BL = new PointF(m_Player.PositionX +
0.25f, m_Player.PositionY + 0.75f); // Bottom
left corner
PointF TR = new PointF(m_Player.PositionX +
0.75f, m_Player.PositionY + 0.25f); // Top
right corner
PointF BR = new PointF(m_Player.PositionX +
0.75f, m_Player.PositionY + 0.75f); // Bottom
right corner
The first line calls the base class's UpdateScene() method, so it can perform its
stuff. The next four lines may look a bit odd though. Why do we need to find out
which grid square each corner of the player sprite is in? It has to do with how our
player's movement will work. Specifically, this is used by our collision detection code.
You may also notice that the first four lines of code are skewing all four corners in-
ward by 25 percent. You can think of these four corners as our bounding box for col-
lision detection. Shrinking the bounding box like this makes it easier for the player to
enter narrow spaces that are only one block wide. Note that TL is short for top-left,
TR is top-right, BL is bottom-left, and BR is bottom-right. The following is the first part
of our collision detection code:
// Check if the user is pressing left.
if
(m_UserInput.KeyboardState_Current.IsPressed(Key.A)
||
(m_UserInput.KeyboardState_Current.IsPressed(Key.LeftArrow)))
{
if ((!m_TileList[m_Map[(int) TL.Y, (int)
(TL.X - PLAYER_MOVE_SPEED)]].IsSolid) &&
(!m_TileList[m_Map[(int) BL.Y, (int) (BL.X -
PLAYER_MOVE_SPEED)]].IsSolid)){
Search WWH ::




Custom Search