Ray casting 2d Visibility

Hi!

I am trying to implement ray casting 2d visibility in java. Following this guide: https://www.redblobgames.com/articles/visibility/
I have gotten to the point where i am able to calculate the angles where walls begin or end and cast a ray from the center of my mouse point. But i am not able to draw proper traingles from my ray-segment list. Do you guyz have any any idea how i connect the points (x1,y1,x2,y2) to make proper triangles? Please take a look at the picture below.

Image 1:

Image 2:

Thanks in advance :slight_smile:

Lets call the 2d position of your mouse position M and the endpoints E0,E1,E2,…,Ej,…En

You could use the angles from M to Ej to determine your starting point and then go around in order of increasing/decreasing angle.
You could choose the first endpoint by determining which vector M to Ej has the largest angle difference from his next neighbor.

Hello again!

Thanks for your replay VatteRGer.

I had already tested those methods you mentioned earlier.

Here is the problem if i go in decreasing order:

It get solved if i go in incrcreasing order, then the problem above gets solved:

Now a new problem come up. When i am inside a box. How do i deal with this now? :slight_smile:

If i connect the last endpoint with the first endpoint, i get the same problem as going in decreasing order.

Oh, so you want it to work from inside and outside these boxes with that same way of sampling points, i didn’t think about that.

Do some samples against a bounding structure (multi-segmented circle) and close the triangle fan!
To get sharper corners you could do 2 slightly offset (perpendicular from the ray) extra samples where rays hit endpoints directly.

There is a smarter way to detect these corners, but i don’t know anything else that’s reliable right now, maybe do another ray-cast from where rays hit the geometry directly at endpoints.

Thank alot VaTTeRGeR! You gave me a pretty good idea what to do :). :wink: