Making 23040000 linecasts per second might be too taxing for the CPU

Started by
10 comments, last by serumas 10 years, 3 months ago

Wouldn't it be easier to only draw two linecasts per wall tile, based on the relative position of the character to the wall, thus halving the number of linecasts? It would take some ifs or a switch to determine the character's area, but everything's better than two extra floating point linecasts, isn't it?


000012222     If the character is standing in area ...
000012222     0 bottom-left and top-right corners are used
000012222     1 top-left and top-right corners are used
000012222     2 top-left and bottom-right corners are used
7777X3333     3 top-right and bottom-right corners are used
666654444     4 bottom-left and top-right corners are used
666654444     5 bottom-left and bottom-right corners are used
666654444     6 top-left and bottom-right corners are used
666654444     7 top-left and bottom-left corners are used

So, to recap:

Every frame, a character loops through several steps of checking his surrounding tiles. Every step his "checking circle" expands by 1 tiles in all directions, starting with his 8 adjacent tiles. Every step, the character checks all tiles in his checking circle.

Is this tile a wall or other vision obstruction?

No:

Go on to the next tile

Yes:

Make a Bresenham from the character to the tile to see if it's in LOS (check if there's a wall or shadow tile in the way)

Not in LOS:

Go on to the next tile

In LOS:

Draw two floating point Unity linecasts from the center of the character tile to the right corners, and see where they intersect with the level boundaries. Round these two intersection points to the nearest tiles, and then draw shadowline Bresenhams towards them from the original wall tile.

Go on to the next tile

When the character is done checking his circle (actually, it's more of a square) for vision obstructions, he then flood-fills all unspecified tiles (i.e. all tiles that are not a wall, and haven't been marked as shadow line), until he has reached his vision border (maximum viewing distance).

All player's characters will then combine their vision and based on that, some tiles will be grayed out (those are shadow tiles), and others will be full bright (those are visible).

I think there may be a more efficient solution, without the use of any linecasts:

Is this tile a wall or other vision obstruction?

No:

Was the previously checked tile a wall?

Yes:

The previously checked tile was a wall edge. Create a Bresenham shadow line from the character to that tile and continue it all the way to the level boundary.

Go on to the next tile.

No:

This is just an ordinary empty tile; go on to the next one

Yes:

Make a Bresenham from the character to the tile to see if it's in LOS (check if there's a wall or shadow tile in the way)

Not in LOS:

Go on to the next tile

In LOS:

Is this the beginning of a wall? (no if the previously checked tile was a wall)

No:

Go on to the next tile (don't draw a Bresenham shadow line because there will be taken care of that on the beginning and ending edges of the wall)

Yes:

Create a Bresenham shadow line from the character to that tile and continue it all the way to the level boundary.

When the character is done checking his circle (actually, it's more of a square) for vision obstructions, he then flood-fills all unspecified tiles (i.e. all tiles that are not a wall, and haven't been marked as shadow line), until he has reached his vision border (maximum viewing distance).

I haven't tested any of it yet, but I think that if the second algorithm is accurate enough (and I sure hope so), it is the one I will go with.

Do you have any suggestions for one or both of the algorithms?

Pintrix

Advertisement

I would think about game world more like round.. smile.png

start point sight from 0.. 2pi

circle by circle eliminate segments from sight, clever skip tiles that potentialy are alredy covered, clever manage circle checks if most circle is alredy covered and so on....clever data usage 160x160 tiles = 800 ints easy to cache

How complex is your world? How much concave polygons of ~10 lines can be in your world?

This topic is closed to new replies.

Advertisement