Skip to main content

Beginning to Refract

·149 words·1 min

I ran into a situation that generated an infinite number of ray bounces … never again. Now I track the number of bounces and stop casting rays after five. I also added a label with the ray number.

I’m getting a weird bounce between ray 2 and ray 3. For some reason it’s rotating by PI/2 instead of PI/6. Actually, even the bounce from ray 1 to ray 2 is wrong. That’s shifting by PI/3. But that PI/6 was just for testing. No point in debugging this until I’ve implemented refraction so let’s do that.

First some code clean up.

From Wikipedia:

n = 1 for a vacuum n = 1.52 for window glass

n₁ ✗ sin θ₁ = n₂ ✗ sin θ₂

I’ll assume a vacuum for everything that’s not a block. So when a ray enters a block I get:

θ₂ = asin( sin(θ₁) / n₂ )