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.
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:
<x, y, z> = <rx + vx * t, ry + vy * t, rz + vz * t>
is the vector equation of the line in question.
note that:
<rx, ry, rz>
is the initial point of the vector equation, and
<vx, vy, vz>
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.
The solution is very easy when you define a ray with a point(= vector) and a direction vector, and the rectangle with a point(= vector) and two vectors representing the sides.
Suppose the ray is defined as R0 + t * D
, where R0
is the origin of the ray, D
is an unit vector representing its direction and t
is its length.
The rectangle can be represented with a corner point P0
, and two vectors S1
and S2
which should represent the sides (their length being equal to the length of the sides). You will need another vector N
normal to its surface, which is equal to the unit vector along the cross product of S1
and S2
.
Now, assume the ray intersects the rect at P
. Then, the direction of the ray, D
must make a nonzero angle with the normal N
. This can be verified by checking D.N < 0
.
To find the intersection point, assume P = R0 + a * D
(the point must be on the ray). You need to find the value of a
now. Find the vector P0P
. This must be perpendicular to N
, which means P0P.N = 0
which reduces to a = ((P0 - R0).N) / (D.N)
.
Now you need to check if the point is inside the rect or not. To do this, take projection Q1
of P0P
along S1
and Q2
of P0P
along S2
. The condition for the point being inside is then 0 <= length(Q1) <= length(S1)
and 0 <= length(Q2) <= length(S2)
.
This method is appropriate for any type of parallelograms, not only for rectangles.