Game such as Starcraft has a mechanism that anything behind obstacles (trees, cliff etc) is covered in Fog of War even if a unit’s sight circle touches it. I have been thinking how to do this efficiently for quite a while. A naive approach would be for each tile inside the unit’s sight circle, check if there is an obstacle between the unit and the tile. But I could imagine this won’t scale well when you have a lot of units on the map. Can anyone suggest a more efficient way of doing this?
Use ray casting to see if anything is between the unit and a tile.
Well, you can draw based on the y-axis position of the object.
If you have your images draw in a queue, you can basically give the illusion of depth by changing the order in which the objects are drawn based on how far up they are on the screen. (This is assuming you’re using sprites and not using objects in a 3-D environment.)
[quote]Use ray casting to see if anything is between the unit and a tile.
[/quote]
Thanks for the reply, this approach looks promising This means I just have to cast enough rays (from the unit position) to cover all the tiles inside the unit’s sight circle. Much better than my original naive approach.
Yes, but don’t over do it! A newbie would cast rays for every degree in a circle, but that would take a hit on your performance, even if its just a little bit. Cast just enough rays so that every tile is covered. So, I think for all the cardinal coordinates. N E S W NE NW etc… As long as your tiles are all the same size, you should be casting rays through every side and corner of every tile!