Depth sorting

An isometric view needs us to handle the depth of items manually. For ground tiles, there is no depth issue as they always form the lowest layer over which all the overlay items and characters are drawn. But overlay tiles and characters need to be drawn at specific depths for it to look appropriate. By depth, I mean the order at which the images are drawn. An image drawn later will overlap the one drawn earlier, thereby making it seem in front of the latter. For a level which does not change or without any moving items, we need to find the depth only once for the initial render. But for a level with moving characters or vehicles, we need to find every frame in the game loop and render.

The current sample level does not change over time, so we can simply render the level by looping through the array. Any overlay item placed at a higher I or J value will be rendered later, and hence will be shown in front, where I and J are array indices. Thus, items placed at higher indices appear closer to the camera, that is, for the same I, a higher J is closer to the camera and vice versa.

Tip

You may go ahead and explore the painter's algorithm available at http://en.wikipedia.org/wiki/Painter's_algorithm.

When we have a moving item, we need to find the corresponding array position it occupies based on its current screen position. By using these new found array indices, we can compare with the overlay tile's indices and decide on the drawing sequence. The code to find array indices from the screen position is as follows:

//capture screen position
var screenPos:Point=new Point(hero.x,hero.y);
//convert to cartesian coordinates
var cartPos:Point=IsoHelper.isoToCart(screenPos);
//find tile indices from cartesian values
var tilePos:Point=IsoHelper.getTileIndices(screenPos,tileWidth);

Understanding isometric movement

Isometric movement is very straightforward to implement. All we need to do is move the item in top-down Cartesian coordinates and draw it on the screen after converting into isometric coordinates. For example, if our character is at a point, heroCart in the Cartesian system, then the following code moves him/her to the right:

heroCart.x+=heroSpeed;
//convert to isometric coordinates
heroIso=IsoHelper.cartToIso(heroCart);
heroImage.x=heroIso.x;
heroImage.y=heroIso.y;
rTex.draw(heroImage);

Detecting isometric collision

Collision detection for any tile-based game is done based on tiles. When designing, we will make sure that certain tiles are walkable while certain tiles are nonwalkable, which means that the characters can move over some tiles but not over others. So, when we calculate the movements of any character, we first make sure that the character won't end up on a nonwalkable tile. Thus, after each movement, we check if the resulting position falls in a nonwalkable tile by finding array indices as mentioned previously. If the result is true, we will ignore the movement, else we will proceed with the movement and update the on-screen character position.

heroCart.x+=heroSpeed;
//find new tile point
var tilePos:Point=IsoHelper.getTileIndices(heroCart,tileWidth);
//this checks if new tile position is occupied, else proceeds
if(checkWalkable(tilePos)){
  //convert to isometric coordinates
  heroIso=IsoHelper.cartToIso(heroCart);
  heroImage.x=heroIso.x;
  heroImage.y=heroIso.y;
  rTex.draw(heroImage);
}

You may be wondering that the hero character should need some special considerations to be drawn correctly as the right depth, but by the way we draw things, it gets handled automatically. We do not allow the hero to move onto a nonwalkable tile, that is, bushes, trees, and so on. So, any tile remains walkable or nonwalkable. The character gets drawn on top of a walkable tile, which does not contain any overlay items, and hence it will occupy the right depth.

In this method, a full tile has to be made either walkable or nonwalkable, but this may not be the case for all games. We may need to have tiles, which block entry from a specific direction or block exit in a particular direction as a fence along one border of a tile. In such cases, the tile is still walkable, but valid movement is also checked by tracking the direction in which the character is moving. For our game, the first method will be used along with the four-way freedom of movement.

In an isometric view, movement can be either in four directions or eight directions, which in turn is called a four-way movement or an eight-way movement respectively. A four-way movement is when we move along the X or Y axis alone on the Cartesian space. An eight-way movement happens when, in addition to four - way, we also move the item diagonally. Logic still remains the same.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.150