Raytracing is not random

function earlyReflections(numBounces)
	local rayOrigin = head.Position
	local speakerPosition = speaker.Position -- Get the position of the speaker part

	for i = 1, numBounces do
		local theta = math.rad(math.random(0, 360))  -- Azimuthal angle
		local phi = math.rad(math.random(0, 180))    -- Polar angle (0 to 180 degrees)

		local randomDirection = Vector3.new(
			math.sin(phi) * math.cos(theta),  -- x
			math.sin(phi) * math.sin(theta),  -- y
			math.cos(phi)                     -- z
		).Unit * 100

		local currentOrigin = rayOrigin
		local currentDirection = randomDirection

		local SURFACE_OFFSET = 0.05

		local rayResult = raycast(currentOrigin,	 currentDirection, {character})

		if rayResult then
			createRayPart(currentOrigin, rayResult.Position)
			local directionToSpeaker = (speakerPosition - rayResult.Position).Unit -- This is now a unit vector
			local distanceToSpeaker = (speakerPosition - rayResult.Position).Magnitude

		
			currentDirection = directionToSpeaker * distanceToSpeaker

			createRayPart(rayResult.Position, speakerPosition)

			break
		else
			createRayPart(currentOrigin, currentOrigin + currentDirection.Unit * 100)
			break
		end
	end
end

so I have about 200 rays at once, but from the picture you can clearly see that there doesn’t seem to be 200, but the reality is that most are superimposing on the same location. My question is why, I am using math.random yes?

Because you are using math.random(). You should probably change it for an index variable that increases gradually.

What’s the raycast function?
If you remove the createRayPart(currentOrigin... bit, what effect does it have on the outcome?

The direction being generated does indeed seem to be random, so I suspect something else is happening.

could elaborate a bit more? I don’t quite understand

function raycast(origin, direction, excludeInstances)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = excludeInstances
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude

	local raycastResult = workspace:Raycast(origin, direction, raycastParams)

	if raycastResult then
		print("Raycast hit: " .. raycastResult.Instance.Name)
	else
		print("Raycast did not hit anything.")
	end

	return raycastResult
end	

I’ll remove createRayPart(currentOrigin... and let you know what happens.

I’m assuming they mean to go based off the last rays random location instead.

that shouldn’t be the case. my code first randomizes the directions before casting them, here:

		local theta = math.rad(math.random(0, 360))  -- Azimuthal angle
		local phi = math.rad(math.random(0, 180))    -- Polar angle (0 to 180 degrees)

		local randomDirection = Vector3.new(
			math.sin(phi) * math.cos(theta),  -- x
			math.sin(phi) * math.sin(theta),  -- y
			math.cos(phi)                     -- z
		).Unit * 100

figured out the issue. the raycast’s were literally casting off the raycast parts. i added them to the exclude list and now its working just fine.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.