0

I have a virtual box that contain 2 of 3DPoint one the minimum (x,y,z) and the second is the maximun (x,y,z)

I have a ray with center point and direction vector

How can I check if the vector has Intersection with this virtual box

(I have all the method of dotProduct, crossProduct, distance etc.) but I don't know how I need to start find if there are intersection s points,

In the attach image I try to show 2 states, one that the ray has intersection and the other without. How can I find it by code

For now I need to find just boolean if there are intersections points, and I don't need to find actually this points.

public class BoundaryVolume {

    public Point3D min;
    public Point3D max;
....

public boolean boundingIntersection(Ray ray) {

     //Point3D p0 = ray.get_POO();
     //   Vector v = ray.get_direction();

     //   Vector v1 = min.subtract(p0);
     //   Vector v2 = max.subtract(p0);
     //   double s1 = v.dotProduct(v1.crossProduct(v2).normalized());
     //   if (isZero(s1)) return false;
    
    
}
}

Ray:

public class Ray {

 private    Point3D _POO;
 private    Vector _direction;
....
}

I Want to check if There are Points that enter image description here

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45
24sharon
  • 1,859
  • 7
  • 40
  • 65
  • is the box always AABB (axis aligned)? check this out: [Cone to box collision](https://stackoverflow.com/a/62257945/2521214) You can use `line closest(axis a0,convex_mesh m0)` and test if returned line has zero length or not. You can optimize it a lot as it is for universal convex mesh not just for AABB ... – Spektre Jun 22 '20 at 08:15

2 Answers2

1

Let ray has starting point rx, ry, rz and direction vector dx, dy, dz, two corners of axis-aligned box are A and B (with B components larger than A components).

In parameteric form ray might be represented as

x = rx + t * dx
y = ry + t * dy
z = rz + t * dz

where t is parameter in range 0..Infinity

Get t parameters for intersections of the ray with planes A.x, B.x, A.y and so on.

t_ax = (A.x - rx) / dx
t_bx = (B.x - rx) / dx
t_ay = (A.y - ry) / dy
...

Choose positive values of parameters and for every calculate whether point of intersection lies in corresponding rectangle

y_ax = ry + t_ax * dy
z_ax = rz + t_ax * dz
if (A.y<=y_ax<=B.y) and ((A.z<=z_ax<=B.z)) 
    ray intersects a face
if not - continue with the next face
MBo
  • 77,366
  • 5
  • 53
  • 86
0

This is my final working code:

public boolean boundingIntersection(Ray ray) {
            
        Point3D po = ray.get_POO();    
        double dirfra_x =  1.0f / ray.get_direction().get_head().get_x().get();
        double dirfra_y =  1.0f / ray.get_direction().get_head().get_y().get();
        double dirfra_z =  1.0f / ray.get_direction().get_head().get_z().get();


        // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
        // r.org is origin of ray
        double t1 = (min.get_x().get() - po.get_x().get()) * dirfra_x;
        double t2 = (max.get_x().get() - po.get_x().get()) * dirfra_x;
        double t3 = (min.get_y().get() - po.get_y().get()) * dirfra_y;
        double t4 = (max.get_y().get() - po.get_y().get()) * dirfra_y;
        double t5 = (min.get_z().get() - po.get_z().get()) * dirfra_z;
        double t6 = (max.get_z().get() - po.get_z().get()) * dirfra_z;


        double tmin = Math.max(Math.max(Math.min(t1, t2), Math.min(t3, t4)), Math.min(t5, t6));
        double tmax = Math.min(Math.min(Math.max(t1, t2), Math.max(t3, t4)), Math.max(t5, t6));

        // if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
        if (tmax < 0)
        {
            return false;
        }
        // if tmin > tmax, ray doesn't intersect AABB
        if (tmin > tmax)
        {
            return false;
        }
        return true;
        
    }
24sharon
  • 1,859
  • 7
  • 40
  • 65