Checking line of sight for any point on a part

Hello,

I’m wondering if it’s possible to check line of sight for any point on a part?

Suppose the following setup, blue is the field of view, red is the obstruction, and green is the part for which we want to check line of sight:

From looking from the field of view, we can tell that the green part is indeed visible from the cone:

Is there a way to determine if the cone can actually “see” the green part?

I have three proposed solutions, but they all have some drawback:

1- Cast a ray from the cone to the origin, the issue is that in this case, this would give us a false positive because the ray is hitting the obstruction. In some cases, it can also be outside of the cone’s FOV as well:

2- Cast a ray from the cone to the closest point on surface to the cone’s origin, this has the same issues as above:

3- Cast many rays from the cone in a circular direction. The issue with this is that there would be many rays cast, and it could hinder performance, and it may not necessarily be accurate either.

Any ideas/proposed alternatives?

1 Like

What you can do is get every part that the cone can see whether it can be seen or not, if the cone sees the green part then use the raycast to check if there is any intersection between the green part and the point of ray origin.

If the camera perspective is accurate. There would be indeed a ray that will hit the brick. I don’t think there is any definitive solution to this that uses only one ray, not that I know of.

But you don’t really have to fire a ton of rays, because within that part there exist a lot points that aren’t being obstructed (as can be seen, only a small portion of the green part is visible, the rest are hidden, in this case, both origin and the closet point are obstructed).

My idea would be to select a specified number of offsets from the origin (assuming the part is always a cube), so you can try to fire a few rays to the corners, center of the edges,… of the part and see if it still intersect with it there. You can make it more accurate by defining more offsets, of course.


I believe this is better than firing a ton of rays, though you would still have to fire more than one.

1 Like

What if you do like @FG920gaming said, but fire the rays from the Part to the origin of the field of view?
If any of those rays hits the origin then you’ll know that the part is visible.

1 Like

Oops I misread your post, Once again you can use the cones to get all the parts.

after which you fire a ray straight, the ray will be at the center of the cone. slowly from the center, offset the ray and get every intersection to the end position which is the GreenPart and only stop the ray once it hits the GreenPart or the ray has extended outside of the cone boundary.

This way you only need to use 1 ray and it won’t be as too performant intensive.

Yeah this was one of the things I was considering as well and this seems to be the best approach thus far, but there are still edge cases with this, such as a small opening but a large part – these predefined offsets are in green, from my camera’s perspective, it would intersect the red parts:

The chances that we would encounter such a case would be minimal but it would be ideal if somehow the solution would account for these cases. If there aren’t any ideas, I will accept this as an answer, however.

@SelfDeclared I’m not sure I understand, isn’t that just the equivalent of casting many rays?

@Scottifly The problem with this is finding the point on the part from which to cast the ray.

You can first use a cone to detect all parts inside it.

After that, fire a single ray straight from the center of the cone toward the GreenPart (your target).

Then, gradually adjust the ray’s direction to ensure it keeps moving toward the GreenPart. The ray should only stop if:

  1. It hits the GreenPart (success).
  2. Or, it goes outside the cone’s boundary (failure).

This way, you’re only using one ray and adjusting its direction instead of casting multiple rays, making it much more efficient.

I use ChatGPT to fix my english I hope you can understand this better.

How do I do this without casting a new ray though? It’s a single method call, to my knowledge there isn’t any way to adjust it after it has been cast.

Oh yep that’s true but the distance from the center to the green part wouldn’t require too much raycasting as it’s shorter, if you want to use even lesser you can offset the distance in studs more when you cast again.

I guess with this method it’s more accurate since you are actually going to get the green part back if the part is actually there.

I believe that if we were to go down this path, the problem you are facing now is basically a slider. Where you will determine if you want to exchange performance for accuracy, as in more set points.

An idea that I have is that you will check out the more simple spots first, which is the points that we’re using right now. Then through a loop or recursive function move on to check on the other spots (points along the edge, points on the surface), these points can either be calculated or predetermined. This process will stop when the ray see the target part.

This is as efficient as I could come up to my knowledge, it still falls to the slider issue, whether you want more performance or more accuracy, which you can adjust by determining how many points you will use.

1 Like

I am just going to go with a visibility check for the origin of the part. It still leaves the edge cases but in most cases it should be fine and it shouldn’t have any performance implications due to it being one ray per part.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.