Reflection result

I really do not know if the formula I got is correct or the right way. But it does seem like the rays are reflecting. The reason why I created this though is because it acts weird after the 2nd interaction. I told the script to run the function 3 times so I could see what was going on. I was wondering how to make it stop acting weird like its doing now.
Here is the photo:


Script:

local part = game.Workspace.Part 

local function reflection(vector,normalvector)
	return vector / 2 * vector:Dot(normalvector) - normalvector
end

local function drawray(origin,direction)
	local mid = origin+direction/2
	local part = Instance.new("Part",workspace)
	part.Anchored = true
	part.BrickColor = BrickColor.Red()
	part.Material = Enum.Material.Neon
	part.Size = Vector3.new(0.1,0.1,direction.Magnitude)
	part.CFrame = CFrame.new(mid,origin)
end

local function castRay()
	local origin = part.Position
	local direction = part.CFrame.LookVector * 20

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {part}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local result = workspace:Raycast(origin,direction,params)
	drawray(origin,direction)
	
	if result then
		--while wait(1) do
			local normalvector = result.Normal
			local position = result.Position
			local reflection = reflection(position,normalvector)
			direction = result.Position + reflection
			result = workspace:Raycast(position,direction)
			drawray(position,direction)
		--end
	end
end

castRay()
castRay()
castRay()

--result.Position 
--result.Position + reflection

I looked at the the size.Z of the reflected parts and it was over 1000.


You had for the most part the correct formula with some minor mistakes

1 Like

Ahhh it works better now. The problem now is that the script keeps saying result.Normal is nil after the first 2 intersections.

local part = game.Workspace.Part 

local function reflection(vector,normalvector)
	--return vector / 2 * vector:Dot(normalvector) - normalvector
	return -2*normalvector*vector:Dot(normalvector) - vector
end

local function drawray(origin,direction)
	local mid = origin+direction/2
	local part = Instance.new("Part",workspace)
	part.Anchored = true
	part.BrickColor = BrickColor.Red()
	part.Material = Enum.Material.Neon
	part.Size = Vector3.new(0.1,0.1,direction.Magnitude)
	part.CFrame = CFrame.new(mid,origin)
end

local function castRay()
	local origin = part.Position
	local direction = part.CFrame.LookVector * 20 

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {part}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local result = workspace:Raycast(origin,direction,params)
	drawray(origin,direction)
	
	if result then
		while wait(1) do
			local normalvector = result.Normal
			local position = result.Position
			local reflection = reflection(position,normalvector)
			direction = result.Position + reflection
			result = workspace:Raycast(position,direction)
			drawray(position,direction)
		end
	end
end

castRay()

NVm I will figure this out by myself