# How to find the exit point of a ray?

Hi. I’m trying to create a ray-cast/traced glass effect, and I need a bit of help.
I’m trying to find the “exit point” of a cast ray inside any part.

Here’s some clarification:

(The angle shown at the entry point is calculated using Snell’s law)

I’m trying to avoid casting as many rays as possible. 100% accuracy and efficiency is my goal, however, it’s alright if I have to cast an extra ray or two, if absolutely necessary of course.
I’m hoping there’s some weird mathy solution for this, and it needs to work on any shape (sphere, cube, cylinder, etc.).
I haven’t tried much with this, but I have thought of overshooting a ray and shooting another back to get the place where it exited, but that sounds very expensive and inaccurate, because the part might be complex and it could overshoot it too much.

Any help is appreciated. Thanks in advance!

I think the best solution would be essentially what you already said by taking another ray.

You would only need one more ray.
The origin of this ray would need to be entryPoint + directionOfRefraction*(hitPart.Size.magnitude+0.01)
Then you can just cast back towards the entryPoint

The hitPartSize magnitude plus the small number would handle the worst case scenario of it being opposite corners for entry and exit, so everything should work with those numbers for a single object. But it is limited to only working with convex shapes

I don’t think it’s possible to get a ray to collide with an object it starts inside making it hard to get the exact position on more complex shapes like unions and meshes, but the default roblox part shapes should all work.

4 Likes

Alright, yeah, I figured. Thanks for the reply though! I’m gonna keep this unsolved for a bit in case anybody else has a different solution.

You could calculate it with a sinus, cosinus and tanus rule called, Trigonometic Ratios (but you’ve to calculate it twice as it’s 3 dimensional and not 2 dimensional!)

You got the “adj” (aka the size of the object you hit) and the degrees/radiants
Note if you calculate this e.g. with google tell the calculator if you’re using radiant/degree (tho I think tlr22’s solution would be better in this case, as this wouldn’t work for a few cases)
Good luck!

1 Like

Sounds interesting, but yeah, I agree with you. The first method is definitely simpler and seems to work pretty decently already. Thanks again.