3
votes

Im having trouble with calculating whether line/ray intersects a rectangle (thats on a plane) in 3d space.

I have searched and the only thing I found was Ray and square/rectangle intersection in 3D but I just cant quite understand the last steps and how to apply it for my system.

So I have a Ray

struct Ray
{
  Vector3 m_startPoint;  // P0
  Vector3 m_direction;   // Direction Unit Vector
  float m_length;         // Ray length
};

and I have a quad defined by

struct Quad
{
    Vector3 p1;    
    Vector3 p2;
    Vector3 p3;
    Vector3 p4;
    Vector3 normal;
}

What I first did was calculate whether the ray would ever hit the plane using the dot product

float dotProd = D3DXVec3Dot(&ray.m_direction, &quad.normal);

if (dotProd < 0)     // if <0 ray will travel into the plane
{
    // Get the point of intersection
    float distToIntersection
    //Vector3D intersectPoint = ray.m_startPoint + (distToIntersection * ray.m_direction);

    // Check whether the point of intersection is within the bounds of the Quad
}

And this is where I get stuck...

I know its been answered before but I can't make it work for my system so would really appreciate some help.

2
I'm not sure your Quad struct fully defines a quad. Even if I collapse them down to the plane, how do I know e.g. whether (0, 0) and (1, 1) defines a zero-height quad with (1, 1) being a basis vector or a 1x1 quad with (0, 1) and (1, 0) being basis vectors, or any other quad entirely? Are you assuming axis alignment?Tommy
@Tommy Hi, hmmm, possibly. I will adjust the Quad definition so that it's easier to work with anyway.unknownSPY
I suggest that you first perform a ray-plane intersection computation, and then, once you have the point of intersection on the plane, decide if it is inside or outside the rectangle (by projecting to 2D).Joseph O'Rourke
@JosephO'Rourke Thats down the lines of what I wanted to do, Could you show me the method of projecting onto 2D? ThanksunknownSPY

2 Answers

2
votes

"Could you show me the method of projecting onto 2D?"

Maybe this will help. Note the projection of a rectangle is a convex quadrilateral (not generally a rectangle), so you will need four LeftOf( ) tests to verify inclusion of the projected point.


RectProjection
LeftOf( )this textbook

Don't forget to test if the rectangle lies in a plane orthogonal to the xy-plane, in which case you should project to either the xz- or the yz-plane.

0
votes

Your existing dotProd test just tells you whether the direction of your ray is along the normal or away from it.

What you probably want to do is:

  1. get distance of start point and of end point from plane;
  2. if both distances have the same sign then stop — there's no intersection;
  3. otherwise calculate the intersection point — if start distance is s and end distance is e then it's necessarily at the point s / (s - e) along the line;
  4. perform a point-in-rectangle test on the point: if its x is in the x range of the box and its y is in the y range of the box then it's inside and the ray strikes the rectangle. Otherwise it does not.