Advertisement

Line of Sight in Tiles Base Map / Fog of War

Started by November 23, 2011 01:35 PM
2 comments, last by DJLink 12 years, 11 months ago
hi there. I'm making a tile based game. The map is getting discovered as the player moves.
So far so good. I've managed to get the player discovering a circle around him.
Thing is, I would like that it only discovers till it hits a wall.
How can I make this? Draw a line to each tile from the player position and see if it hits a wall?

This is what I have so far for the circular sight.


void discover_area(const Vector2i& center, int radius)
{

int left = center.X - radius < 0 ? -center.X : -radius;
int right = center.X + radius >= _n_tiles.X ? _n_tiles.X - center.X : radius+1;
int top = center.Y - radius < 0 ? -center.Y : -radius;
int bottom = center.Y + radius >= _n_tiles.Y ? _n_tiles.Y - center.Y : radius+1;

int range2 = radius*radius;
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
if (x*x + y*y <= range2)
{
_tiles[center.X+x][center.Y+y]->discover();
}
}
}
}
[twitter]DJ_Link[/twitter]
Blog
I used a variant of the LOS algorithm found here: Ultima IV LOS

More specifically, I rewrote the xu4 algorithm in JavaScript; demo here.
Advertisement
Draw a line to each tile from the player position and see if it hits a wall?[/quote]

While its probably not the most efficient way, I would go with that and only worry about redoing it if I ran into performance problems.

I used a variant of the LOS algorithm found here: Ultima IV LOS

More specifically, I rewrote the xu4 algorithm in JavaScript; demo here.


I've been trying to implement this but it's still not quite there, maybe because I've misinterpreted these two images.
If I understand right I should start by traversing with diagonals, if it reached the middle Cardinal, I can show? Or after reaching the middle cardinals I have to traverse it cardinally until the center?

Any help figuring this one out would be great.


los1.png
los2.png
[twitter]DJ_Link[/twitter]
Blog

This topic is closed to new replies.

Advertisement