The technique of total calculation in the lighting algorithm for a tile 2D game

Hello! My last game is an isometric walker, one of the features of which is the “exploration” of the territory: initially the map is black and the player opens this “fog of war” during the game. Moreover, the visibility of the tiles depends not only on the distance to the character, but also on the environment: the cells behind the opaque walls are not visible, even if you approach it point-blank, but, for example, a bush degrades the visibility of the cells behind it by 50%.

image

In order not to burden the processor with frame-by-frame ray tracing (to determine which cell is “visible” at the moment), I used a rather interesting method of “total calculation” - the main parameters for virtually all possible situations are considered before playing the large matrix, and remains during the game just contact her, choosing the right values.

What do I mean by preliminary calculations: The

hero is put in the cell at the origin (0; 0). For each cell, within the visibility of the screen (with a margin, within two screens), various game parameters are considered, including a list of cells that the segment intersects from the origin to its center:

image

To be more precise, such lists are considered for each of the 4 corners of each cell:

image

All this data is recorded in the “visibility matrix” M.

How does it work? Each object in the game has an “opacity” parameter - a value from 0 to 1, a percentage of the obstruction of the review. In the game, to find out how visible the tile (xt; yt) is relative to the position of the hero (x0; y0), I need to:

1. Turn to the matrix element Mt = M [xt-x0; yt-y0]
2.To calculate how much each of the corners of this tile is visible (and the corners that are blocked by the tile itself (xt; yt) are not taken into account, so there are 2 or 3 of them): for this, I read from Mt the vector of tiles that meet on the way to the corner and multiply “ degree of visibility ”of all the objects that are in them (and yes, as soon as an object with zero visibility is found, you can’t continue walking through the list).
3. The degree of visibility of the tile Vt = the arithmetic mean of the visibility of each of its angles.

So I do for each of the tiles in a certain area around the hero when something changes (the position of the hero or objects).

Corner calculations are used instead of one center of the tile to smooth out artifacts when some opaque wall is slightly blocked by the end of the cell, but because of this the cell becomes completely invisible.

image

The method, as you know, is not entirely accurate (compared to an honest "analog" ray tracing), but it gives good values. Discreteness is noticeable the less, the visually less cells in the game.

How much the performance will be improved by a preliminary calculation of all parameters in the “visibility matrix” (as compared to defining parameters on the go) will depend on the language, but in fact all geometric calculations are replaced by the operation of accessing the matrix / vector element. For me on AIR, the increase in speed varies from 7 to 11 times (depending on the degree of loading of the map area with objects)

The same matrix can be used to determine the illumination of the cell, when some objects can emit light, and some can block it. The algorithm here is exactly the same.

image
(coins have a small glow)

Demonstration of changing the appearance of tiles in the dynamics:



Thanks for attention!

Also popular now: