Test for a range not being covered by a list of ranges.

Started by
5 comments, last by Gavin Williams 10 years, 8 months ago

Hi,

I'm writing some code to do a sweeping visibility check in a rogue-like (running around in a spiral while maintaining a collection of the occluding angles and adding a tile if it is not completely occluded at that point in the scan.

If I have a list of floating point ranges called the occludedRanges (represented by List<Vector2>), how can I test if a range called the occludingArc (represented by Vector2) is visible in the set of occludedRanges ? After which I would either discard that tile or union the occludingArc to the occludedRanges.

Advertisement

When you union occludingArc to the occludingRanges, merge any ranges that can be merged. That way you'll always have your occludingRanges described as a union of disjoint ranges, and checking whether a new range is contained or not is trivial (it must be contained in one of the ranges, or otherwise it will be visible).

Ok, I see the advantage of that now that you've said it (about the containment of the range being tested, within one of the existing ranges, and how that makes it simple to test). But what do i do across the polar axis (the +'ve x-axis in cartesian). Currently I produce 3 results in my method ..


List<Vector2>ComputeOccludingArcsOfSegment(float x1, float y1, float x2, float y2)

1. a single range [alpha,beta] there are 5 cases for this

2. 2 ranges, [alpha,2Pi] & [0,beta] there are 3 cases for this (where the segment is bisected by the polar axis)

3. null there are two cases for this, point and segment intersecting the relative origin.

I don't see how I can measure a range that goes from negative into positive, and have it be meaningful to a range being tested. I could keep the arc starting angle and it's sweeping angle, is that one way to do it ? Or maybe as long as I know the lower angle, and understand that it's floating point value may actually be higher than the upper angle of the range, then is that ok ? Am I just then testing lower against lower and upper against upper (for containment)

Edit : deleted the edit.

Edit 2 : Ok, so here is an example of what I don't understand ..

Let's say I have an existing occluding range 315 to 45 degrees.

How do i test if the range 0 - 20 is contained ? ... 20 < 45, but what about 0 to 315. I don't see a simple comparison.

I have to think about this one a bit more, but using angles is almost always a source of headaches. You probably need functions to check if an angle is in between two other angles and things like that (perhaps that's the only thing you need?). You can then implement those checks carefully.

I have a well-known aversion to using angles, because what you are dealing with here is a circle, and unit-length 2D vectors is usually the better representation of points in a circle. But I am not sure things would be all that much simpler than using angles in this case.

I'll try to write some code for you tonight.

[EDIT: Changed "projective line" to "circle", because you need to distinguish between a vector and its opposite, so you don't really have a projective line in the usual sense (although topologically circle and projective line are the same).]

This is probably why i got into vectors. Maybe using vectors would be easier. I haven't actually given that any thought yet.

Edit : So thinking about vectors .. what about if I project the range end-points onto the unit circle. And those vectors represent the corners of a bounding box (bounding the arc range). I would have a collection of bounding boxes. The range being tested would itself have a bounding box. Then do containment checks on the bounding boxes.

I have just thought of that, it looks ok, but it's now 2:30am here in Canberra and I'm getting tired. Thanks in advance for looking at this for me. I'll be back on tomorrow afternoon.

Edit 2 : just thought, for angles sweeping across the axes, the points where the arc intersect the axes would need to be contained by the bounding box for the range. So for the range 45 to 225, not only would the end points be included, but also the points 0,1 and -1,0.

Ok,

Bounding box idea is no good, it probably works for smaller angles but I can think of cases where larger angles produce errors.

I went through my collision code, because I remembered that I have previously written a method to check for the closest point on an arc to a point. I extracted the following code...


        /// <summary>
        /// Test if a point p is within an arc swept by two vectors P1 & P2 from the perspective of the given focus. [ cost = 63 ticks ]
        /// </summary>
        /// <param name="arcP1"></param>
        /// <param name="arcP2"></param>
        /// <param name="focus"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static bool PointWithinArc(Vector2 arcP1, Vector2 arcP2, Vector2 focus, Vector2 p)
        {
            float angleP1 = AngleToVector(arcP1 - focus);
            float angleP2 = AngleToVector(arcP2 - focus);
            float angleP = AngleToVector(p - focus);

            // guarantee that angleP1 is lower than angleP2
            if (angleP1 > angleP2)
                angleP1 -= 2 * (float)Math.PI;

            // if the angle to the point p is within the arc range then the closest point is on the curve of the arc
            bool arcSoln = false;
            if (angleP1 >= 0)
            {
                if (angleP >= angleP1 && angleP <= angleP2)
                    arcSoln = true;
            }
            else // (angleP1 < 0)
            {
                if (angleP < angleP2)
                    arcSoln = true;
                if (angleP - 2 * (float)Math.PI > angleP1 && angleP - 2 * (float)Math.PI < angleP2)
                    arcSoln = true;
            }
            return arcSoln;    
        }

It works for all the trivial cases i have checked so far, but i need to give it some proper thought. At 63 ticks its an OK cost, but will really add up. given a 50x33 tilemap, assuming 30% occluder coverage, a check of the total map would result in a cost of about 34,600 ticks (x2-4), not so good. However, range of visibility (radius 12 for example) would reduce this to about 28,000 ticks (x2-4). Still very expensive. Too expensive. I have read about algorithms that check by octants, I suppose once an octant is fully occluded they don't check any more tiles in that octant. That could be a way to cut it down even further.

I realize too, that I am thinking of a general solution, but the fact that I am working with tiles means I should probably take advantage of that fact to optimize the solution. On the other hand, I already compute wall surfaces of the entire scene. So maybe I can do something there to remove unnecessary checks. I'll get this working first and check it's overall cost

I just want to mention an immensely useful article by Eric Lippert http://blogs.msdn.com/b/ericlippert/archive/tags/shadowcasting/ which helped me get a grasp of the traditional way to compute visibility.

This topic is closed to new replies.

Advertisement