Game Development Reference
In-Depth Information
Finally, we have to round the final player position after we handled the colli-
sion so that the player sprite ends up neatly on an exact position. If we do not do
rounding, an oscillating effect can occur where the player is bouncing up and down
very fast (causing a severe headache). For rounding the position value, we use the
Math.Floor
method. The result of this method is not a
float
, so we have to cast the
result to a
float
since this is what the
Vector2
constructor expects:
position =
new
Vector2((
float
)Math.Floor(position.X), (
float
)Math.Floor(position.Y));
27.11 What You Have Learned
In this chapter, you have learned:
•
how to constrain a character within the environment;
•
how to simulate jumping and falling;
•
how to deal with collisions in games.