Ray and square/rectangle intersection in 3D

前端 未结 3 541
鱼传尺愫
鱼传尺愫 2021-01-06 05:37

Hei. Are making a game and are looking for a ray intersection onto a square or a rectangle only in 3D space. Have search the web and found many solutions but nothing i can u

3条回答
  •  情歌与酒
    2021-01-06 06:07

    Create a vector equation for a line in R3, then solve for the intersection of that line in the plane of the rectangle that you are testing it against. After that, it's simple enough to test if that point of solution lies within the bounds.

    the parameter t of the solution can be found with:

    t = (a * (x0 - rx) + b * (y0 - ry) + c * (x0 - rz)) / (a * vx + b * vy + c * vz)
    

    where:

    a(x - x0) + b(y - y0) + c(z - z0) = 0
    

    is the equation of the plane that your rectangle lies on

    and:

     = 
    

    is the vector equation of the line in question.

    note that:

    
    

    is the initial point of the vector equation, and

    
    

    is the direction vector of the above equation

    After that, plugging the parameter t into your vector equation will give you the point to test for distance.

    enter image description here

提交回复
热议问题