# Setting Shadow Tiles Behind Objects

## Recommended Posts

GameLogicMax    122
Hi there, just joined :) Hope someone can help me out. I've a tile based game, basic stuff with just trees and grass. The player can walk on the grass but can't walk on a tree :) It's basic stuff where when you click right, it moves the unit to the tile to the right (just sets him on it, no animation), all done using a simple 2 dimensional array. ie: int Map[5][5] = {0,0,0,0,1} {0,2,0,0,1} {0,1,1,0,1} {0,0,0,0,1} {0,0,1,0,1} Where 0=grass, 1=tree and 2=playerPosition. In the actual game the grid is say 100x100. Now the player has a "view range", a square grid around his position of 4 grid slots, in all directions. so if he can see 4 grid slots in front and 4 behind, plus the slot he is standing on thats 9 slots across, both vertically and horizontally is a 9x9 view range in a square shape (keeping it basic). Any enemies outside this range won't show up. Now the problem. If there is a tree within this view range, I want to 'shadow' behind it any slots that would normally be visible (because they are within the view range) but aren't now because they are behind the tree. Now I was thinking of an algorithm that 'shoots' a line from the center of the player slot around him in 10 degree increments (1 degree would be more accurate but I don't want 360 calculations each move), an if the line reaches the outside edge of the view range square then any slots it has passed through are visible. If it sets to a degree (say 130) and the line hits a tree slot, then any slot it passed through before the tree is visible, and because the line couldn't reach behind the tree, these slots are marked outside the view range. Hope you know what I mean. Would anyone here know a good way of doing this? Or a better way? Assuming this method, I presume I'd need the equation for a line and move along this line in 1pixel increments checking if I'm in the slot ? Thanks for your help, I'm totally new to game design. Cheers

##### Share on other sites
Alan Kemp    772
What you need is a line of sight (often abbreviated to LOS) algorithm.

There are lots of resources for that here.

Alan

##### Share on other sites
GroZZleR    820
Draw everything like it was a 10x10 view, but only have 9x9 visible. This will also solve 2 tile tall sprite's heads not showing up.

##### Share on other sites
GameLogicMax    122
Thanks for the info and links.

I seem to be too stupid to grasp the details of putting it into code which flustrates me immensely though.

When this tutorial for example
http://roguelikedevelopment.org/php/article/showArticle.php?path=development/LOS/articles/&article=Line%20of%20Sight%20-%20Tobias%20Downer%20[toby@iglou.com].txt
says
"I won't go into how to
walk along a line here. Look up existing line drawing routines or
work it out yourself."

Is it referring to a line as in
y = mx + c
or as in a vector as in
P(t) = origin + t * direction

The vector way looks easier but I have no experience.

Can anyone give me this hint ?

Thanks

##### Share on other sites
JTippetts    12970
Most likely they are talking about using a method such as Bresenham's line drawing algorithm, to step along the line. Methods such as this offer good approximations of a line in a space made up of discrete units such as a tile-based map. It has frequently been used in general to rasterize lines of pixels on-screen, and since rasterizing a line of pixels is strongly similar to tracing a line of tiles, it works as well here. Google for Bresenham line to get more information on how it works; it's really very simple. You step along the line from the viewpoint to a certain point in the world, and at each step you check the tile to see if it blocks or not.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account