CFrame simplification?

local lookVector = camera.CFrame.LookVector
local startPoint = camera.CFrame.Position
local endPoint = startPoint + (unitRay.Direction*100)
	
local vekter = (CFrame.lookAlong( endPoint , lookVector ):Inverse() * (CFrame.lookAlong( startPoint , lookVector ))).Position

This works, but can it be simplified? Thanks!

local startPoint = camera.CFrame.Position
local endPoint = startPoint + (unitRay.Direction*100)
local Vector = endPoint - startPoint

image


It doesn’t output the same thing…

local vekter = (CFrame.lookAlong(unitRay.Direction * 100, camera.CFrame.LookVector):Inverse() * camera.CFrame).Position
2 Likes

Only assumptions can be made from the snippet of code you provided. If you want the full answer, you need to provide the full snippet, which includes the unitRay variable

local function makeRay(xVal,yVal, iteration)

	local unitRay = camera:ViewportPointToRay(xVal,yVal)
	
	local startPoint = camera.CFrame.Position
	local lookVector = camera.CFrame.LookVector
	local endPoint = startPoint + (unitRay.Direction*500)
	
	local pos = (CFrame.lookAlong( endPoint , lookVector ):Inverse() * (CFrame.lookAlong( startPoint , lookVector ))).Position

end

picButton.MouseButton1Click:Connect(function()

	makeRay(0,0,1)
	makeRay(screenSize.X,0, 2)
	makeRay(0,screenSize.Y, 3)
	makeRay(screenSize.X,screenSize.Y, 4)

end)
1 Like

multiplying 2 lookAlongs is very obscure, what kind of vector you’re receiving from that?

I am getting the position of the endpoint relative to the camera.
As if the camera was a (0, 0, 0), pointing down, and the endpoint was at (x, y, z). But instead, now the camera is in the sky at an angle looking sideways, and the endpoint was somewhere 500 studs away from it. If you don’t understand, I can explain further :D

Well, since you know what the inverse() of a CFrame is doing, essentially how it’s getting the offset by dot products:

-- // Uses the code from your makeRay function
local unitRay = camera:ViewportPointToRay(xVal,yVal)
local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction, params)

local cf = camera.CFrame
local lookVector = cf.LookVector
local startPoint = cf.Position
local endPoint = startPoint + (unitRay.Direction*100)

-- // I replaced your `(CFrame.lookAlong( startPoint , lookVector ))`
-- // Part with just cf, as they essentially have the exact same CFrame
-- // Since you didn't enter the 3rd arguement in CFrame.lookAlong(pos, dir, relative_up)
local vekter = (CFrame.lookAlong( endPoint , lookVector ):Inverse() * cf).Position

-- // As you know endCF:Inverse() * startCF is *similar* to 
-- // This in vector terms as they're both offsets
local offset = startPoint - endPoint
-- // Then you do the magic with the original CF you have
local right_vector = offset:Dot(cf.RightVector) -- // Relative X
local up_vector = offset:Dot(cf.UpVector) -- // Relative Y
-- // You have to flip look vector just because that's how it is I guess
local look_vector = offset:Dot(-cf.LookVector) -- // Relative Z 
	
print(vekter, "<-- vekter")
print(Vector3.new(right_vector, up_vector, look_vector), "<-- other")
print("=============================")

Idk which is more performant but yeah, you can test that. ( :

I left your code in so you can copy pasta this into your makeRay function if you want and test for yourself.

1 Like

Both area about the same, but the “vekter” option is just a hair better…
Thank you!

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