Jump to content
• Advertisement

# How to calculate intersection point between line and plane?

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

If you intended to correct an error in the post then please contact us.

## 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.

#### Share this post

##### Share on other sites
Advertisement

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5
• Advertisement

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!