Author Topic: Shadowcast Java algorithm  (Read 21407 times)

Omnivore

  • Rogueliker
  • ***
  • Posts: 154
  • Karma: +0/-0
    • View Profile
Re: Shadowcast Java algorithm
« Reply #15 on: March 27, 2015, 12:05:04 PM »
Wow didn't expect such heated debate about FOV algorithms.

Anyway I am just using something like this now.

http://i.imgur.com/3HuRUn0.png

It works and is symmetrical, although I'm not sure how fast it is. At least straight lines are easy to figure out. I'll try some of these suggestions and see how it works.

Debating is easier than creating :) 

If you want some examples of line generation algorithms, have a look at http://www.openimaj.org/apidocs/src-html/org/openimaj/image/pixel/util/LineIterators.html.  The author does the supercover algorithm as well as bresenham's.  If you want to try the partial supercover variant I use, simply eliminate the else statement in the inner loops (line 264 to 267 and 284 to 287). 

My Java skills are rusty so I'll just do this in pseudo code: you might want to try something simple like this:
Code: [Select]
fieldOfView.Clear()
for y = viewpoint.y - radius; y < viewpoint.y+radius; y++:
    for x = viewpoint.x - radius; x < viewpoint.x+radius; x++:
       for point in supercover(viewpoint.x, viewpoint.y, x, y):
          if point.x == x and point.y == y:
              fieldOfView.SetVisible(x,y)
          if DoesMapBlock(point.x, point.y):
              break;

That is the simple, obvious, field of view calculation that all others try to imitate (for the most part, there are rare exceptions).
 
Before you try improving upon that, recall the words of Donald Knuth: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." [Computing Surveys 6:4 (December 1974), pp. 261–301,]

I'm as guilty there as anyone else, profile first, then improve if need be.  Simpler is better and easier to maintain and easier to expand upon to encompass new goals.

Hope this helps,
Omnivore