Realistic shadows for roguelike
Good time, Habr community.
Many years ago, stumbled upon post (1) 3r3191. . Then I was puzzled by the opportunity to create interesting elements for gameplay in roguelike
(2) 3r3191. . Suppose the enemy can be behind the wall, we do not see him until we encounter him in the line of sight. But more I like the situation when, traveling through the corridors of the dungeon, we reveal the features of the location of objects gradually based on the area of visibility.
Later in the posts:
(3) 3r3191. ,
(4) 3r3191. and
(5) 3r3191. The issues of shadow overlay in 2D games were considered. As noted both by the authors themselves and in the comments, the calculation of shadows is a rather voluminous and not an easy task, both for a calculator and for design.
Somehow I had a few free days, and I decided to return to the issue of more promising shadows. It is clear that the video card copes with the shadows successfully and quickly, but in this case, I wanted to process the shadows for a 2D game, and it seemed to me odd to transfer the calculations to the video card. And the processor power in recent years has generally grown, the actual post about what happened in the end.
The program was written in Pascal, simply because I know it quite well, and Lazarus is an open IDE with a wide range of components.
The initial idea was to draw straight lines from the observer through each of the corners of the tile, and then darken the resulting figure.
However, such a shadow looks somewhat unnatural when the angle of view changes. Shadows are becoming wider, now.
The shadow of a round object looks much better. In order to build such a shadow, you need to hold two tangents from the observation point to the circle, and to the edges of the screen. The diameter of the circle will correspond to the size of the tile.
In my program I used the following function:
//Find the coordinates of the points of intersection of the line with the circle 3r33232 function tangent_to_circle (x? y? x? y? r: Single; var x? y? x? y4: Single): Boolean; 3r33130.
var l, dx, dy, i, ii, ij: Single;
dx: = x1 - x2;
dy: = y1 - y2;
l: = Sqrt (dx * dx + dy * dy);
if l 3r376. begin
tangent_to_circle: = false;
i: = r /l;
ii: = i * i;
ij: = i * Sqrt (1 - ii);
x3: = x2 + dx * ii - dy * ij;
y3: = y2 + dx * ij + dy * ii;
x4: = x2 + dx * ii + dy * ij;
y4: = y2 - dx * ij + dy * ii;
tangent_to_circle: = true;
Where (x? y1) is the observation point, (x? y2) is the center of the circle, ® is its radius, and (x? y3) and (x? y4) are the points of intersection of the lines and the circle. The function returns truth only when the observer is outside the circle.
Since the processor is not very friendly with trigonometry, I tried to use it to a minimum. Actually it was based on a simple rule (a rough model), experts will tell you why.
3r3102. (Bad) SIN, COS > '/', SQRT> 'DIV', 'MOD'> 'SHR', 'SHL'> '*'> ': =', '+', '-', 'AND ',' XOR ' (Good) [/b]
Implement the graphic part of the primitives on the canvas, something else is fun, there are many libraries and engines that facilitate the work. When developing on Delphi, I had to use the Agg2D library, on Lazarus its port
exists. (6) 3r3191. , on it and decided to translate the idea. Actually, the gain from the library is that the alpha channel is added to the RGB colors, and the primitives are smoothed out, and due to direct access to the pixels and various tricks, the processing is much faster than the canvas.
When drawing the shadow of the tile, it was originally intended to fill the sector with a shadow, but then the image inside the tile was poorly distinguishable (the sector in Figure 3 is filled with green). Having experimented with various options, I stopped at selecting a sector from the shadow area.
To draw a sector, we need an angle in radians; nevertheless, it did not go without trigonometry. (arctan2 is the library function of the math module)
//Get the angle in radians 3r33130.
function alpha_angle (x? y? x? y2: Single): Single;
alpha_angle: = arctan2 (y1 - y? x1 - x2);
Actually, everything is ready to assemble the image. We take a tile map and apply shadows on a separate layer, one by one. For trees, the shadows are darker, for other objects the shadows are more transparent.
The finished image is applied on top of the main tile layer. A little background design and pick up tilesets more fun. Actually, it took me two days to search for suitable tilesets, those that are publicly available or of very poor quality or cost money. As a result, the trees were painted by himself, and he borrowed other elements from the user Joe Williamson
(7) 3r3191. (great style).
This would all have ended, but there was some sediment about the performance. When the number of objects about half a thousand begins drawdown. Different methods of optimization were considered, and splitting into cores and limiting the drawing area to a certain radius, changing the shape of the shadow (to less costly than arcs), even thought to transfer the calculation to video.
As a result, I came to the conclusion that the best option would be to reduce the image discretization of the employee by the shadow mask. Since the number of calculations is significantly reduced, and the unexpected effect of pixelization of the shadow contours appears, which gives a certain old school charm.
I liked the effect so much that I had to make scaling a dynamic process through a given multiplicity parameter.
It only remained to create opaque walls and present the result to the community.
Waiting for new games that use this effect or its development.
3r3190. Demo version where you can feel the handles (exe for windows).
3r3196. 3r3197. References: 3r3193. 3r-33199. 1) habr.com/post/16927/
It may be interesting
I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!
Took me time to understand all of the comments, but I seriously enjoyed the write-up. It proved being really helpful to me and Im positive to all of the commenters right here! Its constantly nice when you can not only be informed, but also entertained! I am certain you had enjoyable writing this write-up.