How to make rays intersect the inward faces of parts?

Hello everyone!

The problem:

I’m currently making a path tracer, and I’m trying to get refractive materials to work. However, when I cast a ray from P1 (refracted ray), the issue in “Figure A” happens. And to get this properly working, I would need to somehow get the RaycastResult which intersects the inward face at P2. I apologize in advance for my terrible drawing skills

image


My solution:

My idea is to cast the refracted ray with the magnitude of its direction equal to the magnitude of the part’s size, then cast another ray with the opposite direction of the refracted ray to intersect the outward face of the part at P2. The reason why it should be the magnitude of the part’s size is to not undershoot the part.

But this poses many drawbacks, such as being:

  • inefficient and expensive
  • more prone to floating point accuracy errors
  • can intersect another intruding part on the opposite ray’s way back
  • just really stupid.

image

Is there a way to avoid this issue? And is there any built-in methods on roblox which already do this? I would really appreciate if a graphics programmer who happened to make the raycasting functions come and magically fix all my problems :grin:

Currently there are no built-in Roblox methods that stop if the ray exited the part.

There is one solution you could potentially do, but if you have a lot of reflective objects, then this is pretty meticulous.

You can create a border for every reflective object.

Whenever the incident ray intersects with a reflective object, the refracted ray will check now if it has hit the border you created.

I’m not 100% sure if this works.

1 Like

I just wanted to say that this has worked for me but is very meticulous as you said. And trying to bake that everytime when another part has been added to the workspace is really expensive.

What I did is that I computed the test ray’s origin as a vector3, use RaycastParams to filter everything except the part which I want to get the inward face of, and get its rayResult.

1 Like