Clipping a line to a plane

This is probably a really simple question, but I’m awful with maths and geometry.

Given a point in the world with a unit vector direction (X) how can I get the point where it intersects with a plane?

Diagram

Blue = What I want to get

I’m doing this as a replacement for raycasting, as I don’t want the hitpoint of the ray to go into the void and instead stop at a level of my choosing. Creating a massive part isn’t possible because the area would be bigger than 1024x1024.

Thanks!

There’s an easier way: if the end position of the ray is below the Y value threshold, then you can consider the ray to have crossed it.

Since the XZ plane is orthogonal with the coordinate system, a simple bit of geometry can be used:

Aim = start + direction*(Sealevel-start.Y)/direction.Y -- triangle scaling laws at work

This code will divide by zero if direction has a zero Y component, so check for that!

4 Likes

This works great, thank you!