Math in Unity : Intersections (Part VI)

Today we will see another example about the lines.
In previous articles we have seen how intersections between segments work, now let’s see how they work between two lines.

We create two lines with new coordinates

New coordinates for our lines

Now, we need to think the red line as a wall and the green line as a trajectory of a ball, or projectile, or everything you want.

If you remember, we talked about the difference between the line and the segment.
The segment has a start point and an end point, the line also, but CONTINUES TO INFINITY IN BOTH DIRECTIONS.

Now, inside Intersection Point() we calculate t.
t goes from the starting point to the ending point and has an interval that goes from 0 (starting point) to 1 (ending point).

If we think the wall as t, it means that every value from 0 to 1 hit the wall, while other value are valid, but outside of wall.

In practice, we make another check.
If t < 0 or t > 1
again, return float.NaN

Now we also create the intersectS.
What we do is take the other Vector try to intersect it with our Vector.

We create both for one simple reason :
if we use this simple Debug.Log under everything becomes clear

Let’s look at this :

In the console we have
t = 0
s = NaN

and the sphere is Instantiated at the point 0 of first Vector.
But if we check for both s and t

And we press Play

The sphere is not Instantiated, because there is no intersection between two lines.

This is how we calculate t and s and “check” the intersections with the lines.