Sign in to follow this  

How to calculate intersection point between line and plane?

This topic is 2124 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:

[i]typedef struct Ray
{
D3DXVECTOR3 origin;
D3DXVECTOR3 direction;
} Ray;[/i]

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.:/

[i]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;
}[/i]
BTW it seems that there isn't any option to nest the attached code like in another forums.

Share this post


Link to post
Share on other sites

This topic is 2124 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this