We find the interception of the line and the plane. That is our solution.
We want to find a point on the plane, I'll call it r
. That is a point such that the vector that goes from one of the points on the plane to it is perpendicular to the normal of the plane. Meaning that (r - v1).dot(normal) == 0
.
Please notice that I'm using ==
, which is the equality comparison operator. I'm using that because it is an equation, not an assigment.
The point r
must also belong to the line. We can use the parametric form of the line, which is like this: r = v0 + dir * t
. And we will have to first find what parameter t
must be, so we can compute r
.
So we replace r
here:
(r - v1).dot(normal) == 0
Which gives us this:
((v0 + dir * t) - v1).dot(normal) == 0
Rearrange:
(dir * t + v0 - v1).dot(normal) == 0
By distributive property of the dot product:
(dir * t).dot(normal) + (v0 - v1).dot(normal) == 0
Since t
is an scalar we can take it out of the dot product:
t * dir.dot(normal) + (v0 - v1).dot(normal) == 0
Subtract (v0 - v1).dot(normal)
on both sides:
t * dir.dot(normal) == - (v0 - v1).dot(normal)
We can move that negative sign inside the dot product:
t * dir.dot(normal) == (v1 - v0).dot(normal)
And divide both sides by dir.dot(normal)
:
t == (v1 - v0).dot(normal) / dir.dot(normal)
Thus, our point r
is:
var r := v0 + dir * (v1 - v0).dot(normal) / dir.dot(normal)
Then you can read the height component of r
, which is your answer.
By the way Godot has a method that is almost this. But it intersects a ray, not a line: intersect_ray
of the Plane
class.