# How to calculate intersection point between line and plane?

This topic is 2803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have line modelled with struct:

typedef struct Ray
{
D3DXVECTOR3 origin;
D3DXVECTOR3 direction;
} Ray;

so its formula from mathematical point of view is equal to:
p(x,y,z) = origin + k*direction
And the task is to check if this line (ray) intersect one of the eight planes which box has. Due to same reason the following code doesn't work properly.:/

bool RayBoxIntersectionTest(Ray* ray, BoundingBox* box)
{
D3DXPLANE plane;
float height = box->max.y - box->min.y;
float width = box->max.x - box->min.x;
float length = box->max.z - box->min.z;

// p6--------p7
// p2-------p3 |
// | | | |
// | p5 | p8
// p1-------p4
D3DXVECTOR3 p[8] = {D3DXVECTOR3(box->min.x, box->min.y, box->min.z),
D3DXVECTOR3(box->min.x, box->min.y+height, box->min.z),
D3DXVECTOR3(box->min.x+width, box->min.y+height, box->min.z),
D3DXVECTOR3(box->min.x+width, box->min.y, box->min.z),
D3DXVECTOR3(box->min.x, box->min.y, box->min.z+length),
D3DXVECTOR3(box->min.x, box->min.y+height, box->min.z+length),
D3DXVECTOR3(box->min.x+width, box->min.y+height, box->min.z+length),
D3DXVECTOR3(box->min.x+width, box->min.y, box->min.z+length)};
float k0 = 0, k1 = 0;

//Plane p1,p2,p3
D3DXPlaneFromPoints(&plane,&p[0],&p[1],&p[2]);
for(u32 i=0; i<2; ++i)
{
if(i == 1)
plane.d += length;
if(SolveEquationPlaneVsRay(&plane, ray, &k0, &k1))
{
D3DXVECTOR3 p = ray->origin + k0*ray->direction;
if(!box->isPointInside(p))
{
p = ray->origin + k1*ray->direction;
if(box->isPointInside(p))
return true;
}
else
return true;
}
}

//Plane p1,p2,p5
D3DXPlaneFromPoints(&plane,&p[0],&p[1],&p[4]);
for(u32 i=0; i<2; ++i)
{
if(i == 1)
plane.d += width;
if(SolveEquationPlaneVsRay(&plane, ray, &k0, &k1))
{
D3DXVECTOR3 p = ray->origin + k0*ray->direction;
if(!box->isPointInside(p))
{
p = ray->origin + k1*ray->direction;
if(box->isPointInside(p))
return true;
}
else
return true;
}
}

//Plane p1,p5,p4
D3DXPlaneFromPoints(&plane,&p[0],&p[4],&p[3]);
for(u32 i=0; i<2; ++i)
{
if(i == 1)
plane.d += height;
if(SolveEquationPlaneVsRay(&plane, ray, &k0, &k1))
{
D3DXVECTOR3 p = ray->origin + k0*ray->direction;
if(!box->isPointInside(p))
{
p = ray->origin + k1*ray->direction;
if(box->isPointInside(p))
return true;
}
else
return true;
}
}

return false;
}
bool SolveEquationPlaneVsRay(D3DXPLANE* plane, Ray* ray, float* k0, float* k1)
{
//Rownanie plaszczyzny n*p+d=0, gdzie n = (plane.a,plane.b,plane.c) i d=plane.d
//n*(po+ku)+d=0 |()2 , po = ray->origin, u = ray->direction
//2kupo + kkuu + popo - dd/(nn) = 0, gdzie uu=1
D3DXVECTOR3 n(plane->a, plane->b, plane->c);
float b = 2.0f * D3DXVec3Dot(&ray->origin, &ray->direction);
float c = D3DXVec3Dot(&ray->origin, &ray->origin) - (plane->d*plane->d / D3DXVec3Dot(&n, &n));
// find the discriminant
float discriminant = (b * b) - (4.0f * c);
// test for imaginary number
if( discriminant < 0.0f )
return false;
discriminant = sqrtf(discriminant);
*k0 = (-b + discriminant) / 2.0f;
*k1 = (-b - discriminant) / 2.0f;

return true;
}

BTW it seems that there isn't any option to nest the attached code like in another forums.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 15
• 9
• 11
• 15