How would I be able to get a point where a line intersect


As you can see from the image, I want to get the position where the two yellow dot intersect base on the player's mouse hit look vector, where z-axis is always constant. For example base on the image above, the player's camera position -1, 1, 3, the mouse look vector is full of decimals to represent it for now is 4, 1, -1, with the result of 2.75, 1, 0.

Just making sure you’re saying the horizontal blue line in your last image is always (0,0,1)?

Actually I think I know what you mean.
image

You can use the rules of similar triangles to solve this. If you divide the Long (blue) vector by the purple Z difference (between the point and the player’s camera), you will get a vector whose Z component must be 1. If you then multiply that vector by the green Z difference, you will get the teal vector you desire.

3 Likes

I finally got it working, and the solution works great.

-- main code
local position = aPosition - (diff / diff.Z) * (aPosition - originPosition).Z
local function generateDebugPointPart(properties: {}): Part
	local part = Instance.new("Part")

	part.Size = properties.Size or Vector3.new(0.1, 0.1, 0.1)
	part.Shape = properties.Shape or Enum.PartType.Block
	part.Color = properties.Color or Color3.new(1, 0 ,0)
	part.Material = Enum.Material.Neon
	part.Parent = script.Parent
	part.Anchored = true
	
	return part
end

local RunService = game:GetService("RunService")

local a = script.Parent.A
local b = script.Parent.B
local origin = script.Parent.Origin

local ab_DebugLine = generateDebugPointPart({ Color = Color3.new(0, 0, 1) })
local bOrigin_DebugLine = generateDebugPointPart({ Color = Color3.new(1, 0, 0) })
local targetDebugPoint = generateDebugPointPart({
	Size = Vector3.new(0.25, 0.25, 0.25),
	Color = Color3.new(0, 1, 0),
	Shape = Enum.PartType.Ball
})

RunService.Heartbeat:Connect(function()
	local aPosition, bPosition, originPosition = a.Position, b.Position, origin.Position
	local targetPosition

	do
		local diff = aPosition - bPosition
		local magnitude = diff.Magnitude
		local cFrame = CFrame.new(bPosition + diff.Unit * magnitude / 2, bPosition)
		
		local position = aPosition - (diff / diff.X) * (aPosition - originPosition).X
		
		ab_DebugLine.CFrame = cFrame
		ab_DebugLine.Size = Vector3.new(0.05, 0.05, magnitude)
		targetDebugPoint.Position = position
		
		diff = position - originPosition
		magnitude = diff.Magnitude
		cFrame = CFrame.new(originPosition + diff / 2, position)
		
		bOrigin_DebugLine.CFrame = cFrame
		bOrigin_DebugLine.Size = Vector3.new(0.05, 0.05, magnitude)
	end
end)

1 Like

I just noticed that this does not depend on the origin’s look vector. How would I make it so the desired position is relative to the origin’s look vector?

It will be correct as long as all vectors are in the same space. The “Known” distances are projected distances. If aligned to world axes, this just means one the axes.

Yeah, but want it to align depending on the origin’s coordinate frame.

Covert all the vectors involved into the origin cframe’s space, then. You can use :PointToObjectSpace()

I already tried this; it does not really work.

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