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
You don't say whether the square/rectangle in 3D is aligned with the coordinate axes or not. Assuming the 3D rectangle R is oriented arbitrarily in space, here is one method. First interesect your ray r with the plane containing R. This can be accomplished by requiring a scale factor s to multiply r and place it on the plane of R, and solving for s. This gives you a point p on the plane. Now project the plane, and R and p, on to one of the coordinate planes {xy, yz, zx}. You only have to avoid projecting perpendicular to the normal vector to the plane, which is always possible. And then solve the point-in-quadrilateral problem in the plane of projection.
Before beginning, check if your line segment lies in the 3D plane of R, and if so, handle that separately.