Strange raycasting errors occurring when distance from raycast origin to world origin is far

Hello everyone.
I’ve been doing a lot of things involving raycasting recently, but one thing that’s been bugging me recently is how raycasting can become imprecise far from the world’s center.

As shown below:


(Near world origin - so far, so good…)


(Far from world origin - not so good now.)

I’d like to know - is this a problem with the Roblox engine itself, or my code?

My code being:

local function GetReflectedNormal(RayNormal, SurfaceNormal)
	-- using formula r = d - 2(d ⋅ n)n
	return RayNormal - (2 * RayNormal:Dot(SurfaceNormal) * SurfaceNormal)
end

local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.FilterDescendantsInstances = {workspace.LaserPointer}

local function CastLightRay(Origin, Direction, LightBounces, Wavelength)
	local LightRayTable = {}
	
	for CurrentLightBounce = 1, LightBounces do
		local LightRay = workspace:Raycast(Origin, Direction, RayParams)
		
		if not LightRay then
			LightRayTable[CurrentLightBounce] = {Origin = Origin, Direction = Direction}
			break
		else
			LightRayTable[CurrentLightBounce] = {Origin = Origin, Direction = LightRay.Position}
			
			Direction = GetReflectedNormal(CFrame.lookAt(Origin, Direction).LookVector, LightRay.Normal) * 5000
			Origin = LightRay.Position
		end
	end
	
	return LightRayTable
end


local gizmo = require(game.ReplicatedStorage.gizmo)
gizmo.setScale(5)
local function VisualiseLightRay(LightRayTable)
	for i, v in pairs(LightRayTable) do
		gizmo.drawLine(v.Origin, v.Direction)
	end
end

game:GetService("RunService").Heartbeat:Connect(function()
	gizmo.clear()
	VisualiseLightRay(CastLightRay(
		workspace.LaserPointer.EmitPoint.Position,
		workspace.LaserPointer.EmitPoint.CFrame:ToWorldSpace(CFrame.new(-5000, 0, 0)).Position,
		48))
end)

(gizmo - visualisation library by @Arbeiters, very cool, 10/10)
(workspace.LaserPointer - model consisting of:
image
)

Thanks for any help.

1 Like

How far is this actually from the world origin?

1 Like

Sorry I didn’t clarify. This is happening 105 studs away from the origin (on -x axis).

(Extra detail: The opposite effect occurs in the other direction - +x axis.


It’s not as noticeable however.)

Huh, that’s pretty strange.

I don’t think thats an error on your code, more like on Roblox’s side (correct me if I’m wrong though).

I have heard about position-precision loss when going farther from the world origin due to the limitations of using float point precision, but I’m not sure if this is related to it.

I don’t believe this is floating-point imprecision, but somehow the opposite.

The ray length used in the above screenshots was set to 5000 studs - which I believe is the maximum raycast distance anyway.

I tried setting the ray distances to 20 studs - which should fix floating-point imprecision usually because the engine doesn’t have to deal with long distances (although 5000 studs is not so far as to cause floating point imprecision anyway) - but instead I got this.

The ray is not even casting in the right direction anymore, which does lead me to believe that this could be an issue of the engine. (The white point is where the ray was supposed to be cast - the direction, if you will)

(Just to compare - 5000 stud cast distance


The point and ray are in the right direction.)

I’ll submit a bug report on this, because to me this is a pretty big issue. Glad to know it’s not because of my code.

2 Likes

Bump, I noticed this same issue in my game and took me 2 days to figure out it was the raycasting, at first I thought it was the physics engine, but then I made my ray longer and then I figured out it was an issue with how far objects are from the world origin.

2 Likes