The intersection position of the 3d vector and the cube

At the moment I have a complete mind.

I have a cube (voxel), which is determined by the center point and the width, height and depth 1. If I have a vector starting from the center of the field (for example, (-0.7, -0.7, -0.7)), how can I calculate the point where the vector intersects the outer part of the field?

EDIT: the field is always aligned along the axis. The problem is in 3d.

thank

+3
source share
1 answer

Any point on the surface of your box will have at least one coordinate equal to 0.5 or -0.5, and all the rest will be -0.5 <= c <= 0.5.

So, find the coordinate with the highest absolute value, and then draw a vector to make this coordinate equal to +/- 0.5.

Something like this might work:

if (fabs(x) > fabs(y) && fabs(x) > fabs(z))
    y *= 0.5 / fabs(x)
    z *= 0.5 / fabs(x)
    x *= 0.5 / fabs(x)
else if (fabs(y) > fabs(z))
    x *= 0.5 / fabs(y)
    z *= 0.5 / fabs(y)
    y *= 0.5 / fabs(y)
else
    x *= 0.5 / fabs(z)
    y *= 0.5 / fabs(z)
    z *= 0.5 / fabs(z)
+2

Source: https://habr.com/ru/post/1753252/


All Articles