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.
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.
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