3D Finding intersection of a ray with a plane

Morendral

Member
Hello all, I'm asking for a bit of help here with trying to figure out how to find the 3D coordinates of where a ray intersects a given z plane (shooting for 0 but I'm not sure if that works). I used to have a script that worked, but no longer. I'm using the wonderful SMF, and the latest thing Snidr released has a great built in 2d to 3d function which provides a vector from the camera, given the camera and screen coordinates (in this case the window position of the mouse). I've been looking for a way to get a formula or pseudo code from the web but I've been unsuccessful so far; its either been using to many built in functions, or there are math symbols or concepts which I am unfamiliar with.

Any help would be appreciated, especially pseudocode which could help me figure out the final solution on my own. Thanks!
 

FrostyCat

Member
Come on, this is one of the most basic formulas in 3D computer graphics.

Using the same conventions as on this page:
Code:
///@func ray_plane_intersect(l0x, l0y, l0z,  lx, ly, lz, p0x, p0y, p0z, nx, ny, nz)
///@param l0x
///@param l0y
///@param l0z
///@param lx
///@param ly
///@param lz
///@param p0x
///@param p0y
///@param p0z
///@param nx
///@param ny
///@param nz
var l0x = argument0,
    l0y = argument1,
    l0z = argument2,
    lx = argument3,
    ly = argument4,
    lz = argument5,
    p0x = argument6,
    p0y = argument7,
    p0z = argument8,
    nx = argument9,
    ny = argument10,
    nz = argument11,
    t = dot_product_3d(p0x-l0x, p0y-l0y, p0z-l0z, nx, ny, nz)/dot_product_3d(lx, ly, lz, nx, ny, nz);
return [l0x+t*lx, l0y+t*ly, l0z+t*lz];
Then finding a ray's collision point with the z=0 plane is simply ray_plane_intersect(l0x, l0y, l0z, lx, ly, lz, 0, 0, 0, 0, 0, 1).

Please, learn what dot products, cross products and normals are before you touch 3D graphics. The instructions are not difficult to follow, and most practitioners in the subject commit the formula to memory. When you do 3D graphics without basic vector math (most of which is basic arithmetic on repeat), all you're doing is wasting everyone's time.
 

Morendral

Member
@FrostyCat Thank you for posting this. That's actually one of the pages that I came across, but I am having trouble understanding how to determine the p0. I understand that it is meant to represent the distance from the plane to the origin, but I don't understand how to find that. Any example I have come across uses a predefined point (which is what I believe I am trying to find in the first place), or an equation.
 

FrostyCat

Member
p0 is simply any point on the plane. You're expected to have p0 known because you can't form a plane without at least a known point.
 
Top