Game Development Reference
In-Depth Information
As you can see, you don't directly access the Tile objects. The reason is that sometimes, the x or
y index can be negative because the character is near the edge of the screen. Here you see the
advantage of using the getTileType method you added to the TileField class. You don't care if
you're really dealing with a tile: as long as you know its type and bounding box, you can do your job.
In the nested for instruction, you also see a new keyword: continue . This keyword can be used in
for or while instructions to stop executing the current iteration of the loop and continue to the next
one. In this case, if the tile is of type background , the rest of the instructions are no longer executed,
and you continue to increment x and start a new iteration to check the next tile. The result is that
only tiles that aren't of type background are considered. The continue keyword is related to break ,
which stops the loop entirely. Unlike break , continue only stops the current iteration.
This code doesn't always work correctly, though. Particularly when the character is standing on
a tile, rounding errors when calculating the bounding box can lead to the algorithm thinking the
character isn't standing on the ground. The character's velocity is then increased, and the character
may fall through the tile as a result. To compensate for any rounding errors, you increase the height
of the bounding box by 1:
var boundingBox = this.boundingBox;
boundingBox.height += 1;
if (!tileBounds.intersects(boundingBox))
continue;
// handle the collision
Dealing with the Collision
Now that you can detect collisions between the character and the tiles in the game world, you have
to determine what to do when a collision happens. There are a couple of possibilities. You could let
the game crash (not good if you want to sell your game to many people), you could warn users that
they shouldn't collide with objects in the game (results in a lot of pop-up messages), or you could
automatically correct the position of the character if it collides with an object.
In order to correct the character's position, you need to know how bad the collision was. For
example, if the character walked into a wall on the right, you have to know how far you have to
move the character to the left to undo the collision. This is also called the intersection depth . Let's
extend the Rectangle class with a method called calculateIntersectionDepth that calculates
the intersection depth in both x and y directions for two Rectangle objects. In this example, these
rectangles are the bounding box of the character and the bounding box of the tile it's colliding with.
The intersection depth can be calculated by first determining the minimum allowed distance
between the centers of the rectangles such that there is no collision between the two rectangles:
var minDistance = this.size.addTo(rect.size).divideBy(2);
Then you calculate the real distance between the two rectangle centers:
var distance = this.center.subtractFrom(rect.center);
 
Search WWH ::




Custom Search