1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

3D Finding intersection of a ray with a plane

Discussion in 'Programming' started by Morendral, Feb 13, 2020.

  1. Morendral

    Morendral Member

    Joined:
    Jun 27, 2016
    Posts:
    401
    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!
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,893
    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.
     
  3. Morendral

    Morendral Member

    Joined:
    Jun 27, 2016
    Posts:
    401
    @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.
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,893
    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.
     

Share This Page