How would I "flatten" a reflected raycast?

Hey there! I just had a quick question and hope someone who knows math better than I do can help me figure it out.

So I roughly know how to reflect a raycast using the direction of the ray and the surface normal.
However, I’d like to “flatten” the reflection so the reflected raycast is more pressed against the wall.

If I recall a raycast is roughly reflected like so.

local reflection = ray_direction - (2 * ray_direction:Dot(raycast_result.Normal) * raycast_result.Normal)

However now I’d like to sorta push the reflected ray more against the wall, that’s all really.

I personally dont know that much, but a shortcut could work.
the shortcut im talking about is extrapolating your reflected vector. as in: Reflected:Lerp(Normal,-1)
Technically it works, but with out much predictive control.
combined with Vector3:Angle(Vector3), you could technically control the angle at which the vector is skewed, or refracted. Since we can get the angle between the normal and the Reflected/Original vector, we can control the angle by getting that, and by dividing it by the angle we wish to skew by, we get a interpolation value between 0 and 1 which can be used as an extrapolation value for Vector3:Lerp(Vector3, -0.1)
Though I actually have no idea if this even works, im just using my current resources

1 Like

Actually seems very logical, I forgot about Vector:Angle() being a thing.

I suppose if I get the Cross() product using the incoming ray’s direction and the normal I can just use that to construct a CFrame of it too I assume?

I was thinking something like this…

local cross = raydir:Cross(castresult.Normal)

local angle = raydir:Angle(castresult.Normal)

local normal_aligned_cframe = CFrame.lookAt(castresult.Position, cross, castresult.Normal)

Just a rough sketch but I think something as simple as this should give a CFrame that’s aligned to the normal of the surface and original direction of the raycast.

And after that it’s just flipping the CFrame 180 degrees and tweaking the angle to get a desired reflection?

I hope this is fast, performance isn’t a huge deal but if a faster/better alternative exists I’ll gladly hear about it.

1 Like