I am currently working on a “projector” system for a project, where a part fires off rays at specified angles in relation to its look direction, then places a specific part where the ray lands, “drawing” an image. The issue I have run into is for whatever reason the ray angles given are being reflected over the Y axis, for example 30 degrees is becoming -30 degrees.
I self taught myself most of the math involved in this, so I am not sure if the saving or applying is wrong.
(the saved Cframe is just preserving the part’s relative rotation to the projector)
Saving code:
--X controls up and down, Y is left and right
local function getAngles(part)
local dY = part.Position.X - projector.Position.X
local dX = part.Position.Y - projector.Position.Y
local aY = math.atan(dY/50)
local aX = math.atan(dX/50)
return Vector3.new(aX, aY, 0)
end
for i, part in ipairs(parts) do
local angleData = Instance.new("CFrameValue")
angleData.Name = part.Name
angleData.Value = CFrame.fromEulerAnglesXYZ(part.CFrame:ToEulerAnglesXYZ()) * (CFrame.fromEulerAnglesXYZ(projector.CFrame:ToEulerAnglesXYZ()):Inverse())
angleData:SetAttribute("Angles", getAngles(part))
angleData.Parent = newPose
end
Applying code:
for i, position in ipairs(pose:GetChildren()) do
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist
params.FilterDescendantsInstances = {workspace.Map}
local cframe = projector.CFrame * CFrame.Angles(position:GetAttribute("Angles").X, position:GetAttribute("Angles").Y, position:GetAttribute("Angles").Z)
local ray = workspace:Raycast(cframe.Position, cframe.LookVector*100, params)
if not ray then continue end
parts[position.Name].CFrame = CFrame.new(ray.Position) * (CFrame.fromEulerAnglesXYZ(projector.CFrame:ToEulerAnglesXYZ()) * position.value)
end