How can I Visualize a Triangle based on the the Distance of an Object?

Hi, so I was wondering on how I could Visualize the Distance of a Player from the Position of an Object.

A already know about the pythagorean theorem with the equation of a² + b² = c², which for this case, can get you the Distance (the Hypotenuse) between p0 (start position) and p1 (end position).

I already know how to Visualize c, as it is simple to do, but how would I add a and b to line up with c to create a Triangle?

I’m not asking for a script, just asking how I could do this, as im not entirely sure how to do it properly.

1 Like

a² + b² = c² in 2D space, say X-Y plane. a and b are the differences:

a = p1.X - p0.X
b = p1.Y - p0.Y

Given the points p0 and p1, there are two possibilities to obtain a third point q to form a triangle
q = (p0.X, p1.Y) or q = (p1.X, p0.Y).

in 3D, the Pythagorean theorem is a² + b² + c² = d², with d as the hypotenuse. (just in case)

1 Like

Not sure what you mean, but I’ll give you what I could piece together from your post.
If you have the hypotenuse [you have I presume], you can multiply it by sin to get the opposite side of the hypotenuse
then multiply the hypotenuse by cos to get the side adjacent to the hypotenuse

not sure if that’s what you want, maybe elaborate more?

Forgot to add an Image, but basically, im trying to figure out how I can make a and b Visible, kind of like this where you see a Right Triangle:

pt_example

The hypotenuse would basically be the Distance between p0 and p1 with a Right Triangle below (The part I want to Visualize)

Assuming that this triangle is relative to world space, you can represent the point that the legs of a right triangle intersect, on the x axis or y axis. in the picture, A represents the starting position, C represents the ending position, and B represents the point of “right-triangleness”

B = (C.x, 0)
or
B = (0, C.y)

1 Like

So I spent some time playing around with stuff, and this is what I achieved, so Im wondering if I did this correctly?

local char = script.Parent

local p0 = Vector3.zAxis*100
local p1 = char.HumanoidRootPart.Position -- The Position of the Character


local C3Default = Color3.new(1,1,1) -- default color of point

local function spawnPoint(letter, color ,pos) -- creates a Marker to determine a point
	local sphere = Instance.new("Part")
	sphere.Shape = Enum.PartType.Ball
	sphere.Size = Vector3.one*.1
	sphere.Color = typeof(color) == "Color3" and color or C3Default
	sphere.Name = #letter < 2 and letter or "_"
	sphere.Position = typeof(pos) == "Vector3" and pos or Vector3.zero
	sphere.CanCollide = false
	sphere.Anchored = true

	sphere.Parent = workspace

	return sphere
end

local function addBeam(x, y) -- Adds beams from a to b

	local current = {}
	local bFolder = workspace:FindFirstChild("Beams")
	if not bFolder then
		bFolder = Instance.new("Folder")
		bFolder.Name = "Beams"
		bFolder.Parent = workspace
	end

	for _,v in ipairs{x, y} do
		local typeresult = (v == x)
		local AttName = typeresult and "startPos" or "endPos"

		local realAtt = Instance.new("Attachment")
		realAtt.Name = AttName
		realAtt.Parent = v
		current[AttName] = realAtt
	end

	local beam = Instance.new("Beam")

	beam.Name = "Point"
	beam.Attachment0 = current.startPos
	beam.Attachment1 = current.endPos
	beam.FaceCamera  = true
	beam.Width0      = .1
	beam.Width1      = .1

	beam.Parent = bFolder
	current.beam = beam

	return current
end

local function getAxis(x, y, axis: string): number -- gets the difference between an axis
	return (x[axis] - y[axis])
end

local function parag(x, y, z) -- Pythagorean theorem
	return math.sqrt((x or 0)^2 + (y or 0)^2 + (z or 0)^2 )
end

-- Creates points
local a = spawnPoint("a", nil, p0)
local b = spawnPoint("b", nil, p1)



local y = getAxis(p0, p1, "Y") -- Gets Difference betwen a Axis

local mid = spawnPoint("m", nil, p) -- spawns a "mid point" to form the right angle

-- Adds Beams
local ab = addBeam(a, b)
local bm = addBeam(b, mid)
local ma = addBeam(mid, a)

-- mid point posiion
local p = Vector3.new(0, y, 0)

mid.Position = p0 - p -- sets position to mid point

while true do -- while loop to set how it would work for the Player
    -- Its basically the same thing you saw above

	p0 = char.HumanoidRootPart.Position
	p1 = b.Position
	
    -- Sets p0 and p1 to the most recent Positons
	a.Position = p0
	b.Position = p1

	local y = getAxis(p0, p1, "Y")
	print(y)
	p = Vector3.new(0, y, 0)

	mid.Position = p0 - p
	--print(mid.Position)
	task.wait()
end

Sorry if I made this look confusing, I may need to elaborate for some parts.

if you already have the endpoint and startpoint, why use the Pythagoreion theorem?
I see in the code above that the parag function isnt used but why use it in the first place?

local XPoint = p1.X
local YPoint = p1.X + p1.Y
local ZPoint = p1 --x+y+z

1 Like

I thought I would’ve needed it, but I guess not really.

1 Like