Calculating the Y value of a vector3 so the vector3 lies on a triangle

image
p4 is a vector3 that lies off the triangle
i want to only change the Y value of this vector3 so that it lies on the triangle

Hey there,

You first need to find the equation of the plane, which has a form similar to this:
a*x + b*y + c*z + d = 0
This means, that for a point with (x,y,z), if after substituting in the plane’s equation you get 0, then the point is within the plane.
I used this link to determine the equation’s plane and then using these maths:
image
I determined the y the point p4 should have to enter the plane. From what I understand, you want to keep the X and Z of p4, only changing the Y.

Here’s a Lua code function I wrote that given a p1,p2,p3 and a p4 just like in your drawing, will return a new p4 whose Y is fixed on the triangle plane you want:

function getPointInPlane(p1,p2,p3,p4)
	local v1 = p2-p1
	local v2 = p3-p1
	local c = v1:Cross(v2)
	local y = - ( (c.X*(p4.X-p1.X) + c.Z*(p4.Z-p1.Z)) /c.Y) + p1.Y
	return Vector3.new(p4.X, y, p4.Z)
end

You can use it like this:

newP4 = getPointInPlane(p1,p2,p3,p4)

And now the newP4 should be in the plane.

1 Like

i have already used this i was wanting something a little more efficient lol however it works and i have found another solution on stackoverflow thank you so much for your help

1 Like