Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I draw a right triangle when I only have the hypotenuse?

First off, I'm doing this on roblox, so remember that I'm dealing with three axes. But it is a triangle, not a pyramid. I'm using raycasting.

Hypothetical triangle

I have the hypotenuse (AB) already drawn, so I know how long it is and everything. I also have the position at the start and the end of this hypotenuse (in 3D roblox space). I have the direction that AC needs to travel. I just don't have the length of it.

That's what I'm trying to find. There's not really any code to show, because it's all correct. My methods are just wrong, and I can't figure it out. Hopefully this is possible. Explain it in layman's terms, please.

1 comment

1

I'd like to be high on potenuse.

1 answer

✓ Accepted answer

If I understand your question correctly, what you're asking for is called vector projection.

Given a vector v and a direction d, find the vector y that points in the direction d but is the "same amount in that direction as v"

Formally, y = x * d + p where x is a real (the distance AC), and p(C-->B) is perpendicular to d (A-->C)

You can take the dot product with respect to the unit vector d to yield the equatoin

y . d = x * d.d + p.d. Since d is unit and d is perpendicular to p, this simplifies to x = y . d.

Thus the distance is given by, in Lua,

local distanceInDirection = vector:Dot(direction)

-- e.g.,

local A = Vector3.new(10, 50, 30)
local B = Vector3.new(18, 36, 7)
local direction = Vector3.new(-1, -1, 0).unit

local vector = B - A
local x = vector:Dot(direction)

local C = A + x * direction

3 comments

0

It works, so thanks, I just don't understand how vector:Dot(direction) gives you the "projection". Because  dot product is just a1b1 + a2b2 + a3*b3

0

It... just does. An equivalent definition is a:Dot(b) is a.magnitude * b.magnitude * math.cos(theta) where theta is the angle between them.

0

(It's 1 when same direction, and shrinks to 0 as you rotate towards 90 degrees away)

This discussion is preserved as it was. New answers, comments, and votes are closed.