How do I calculate the reflection normal from the inside of a sphere?

Given this function:

local function Reflect(Normal, SurfaceNormal)
	return Normal - (2*Normal:Dot(SurfaceNormal) * SurfaceNormal)
end

doesn’t allow me to reflect any vectors in the appropriate format due to the Normal & SurfaceNormal being equal to each-other. Instead, it just negates the direction due to them being equal to each other.

Is there a way for me to calculate this? I’ve read an article but it seems it would only work on certain cases + would need to adjust the vector by that specific case.

2 Likes

Can you provide where or how you are using this reflection code and why your Normal (direction) is the same as your SurfaceNormal…?

Perhaps i wasn’t clear enough. In my code, the surface normal is being calculated by the difference of points between the origin and my given point.

local r = 1 -- radius
local a = Vector3.new() -- point "a" is the center of the sphere
local b = Vector3.new(0, r, 0) -- point "b" is my given point (mouse point, idk)
local c = (a-b).Unit -- this is my surface normal
local d = (b-a).Unit -- this is my direction normal.

With those given variables, the Reflection normal will always be opposite of my direction normal [c] because they’re negatives of each other.
What i’m looking for is the solution to disregard these spherical conditions and resulting in a reflected normal shooting in another direction other than the opposite of my initial direction

So you’re essentially casting from the center of your sphere to the surface of your sphere, and wanting an object to bounce around inside your sphere?

If you want the object to bounce around randomly and not reflect, then make it bounce around randomly and don’t reflect. Two ways to do this are:

a) randomizing the direction the cast reflects
or
b) add a small, random offset to the origin of your cast (maybe 1/10th of your radius) and then your directions should be a little more random.

So you’re essentially casting from the center of your sphere to the surface of your sphere, and wanting an object to bounce around inside your sphere?

Yes that is correct!
And although that is a sweet fix, it can’t be random for specific reasons. Thank you for helping out though!