# having problem to detect whether polygon blocks 3d ray's path

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

## Recommended Posts

hi, im having a problem to detect whether polygon blocks 3d ray's path. i have the code, and i just don't know whats wrong with it, im using this function "ispolygonblocksray(...)" to know if a camera is seeing point in space (if the point is not behind smoe object) when im using the code the render is partially good : i don't see any rays coming behind a surface, but im still losing some of the rays that were supposed to come and there is nothing that interfere between the camera to these rays. all other functions except ispolygonblocksray are good. im calling the ispolygonblocksray like this : if (ispolygonblocksray(r, xintersect, yintersect, zintersect, camerax, cameray, cameraz) == 0) to determine whether to present "xintersect, yintersect, zintersect" point or not. here's the code (im also posting the dependicies of this code):
struct vector
{
float x;
float y;
float z;
};

// r->plane[counter2].equx is the "a" coffient in the "ax+by+cz+d = 0" equation
// r->plane[counter2].equy.. etc..

bool ispolygonblocksray(raytracestruct *r, float x1, float y1, float z1, float x2, float y2, float z2)
{
int counter2 = 0;
vector p;
float x,y,z,t;

while (counter2 != r->numberofplanes)
{
calculatelineplaneintersection(x1, y1, z1, x2, y2, z2, r->plane[counter2].equx, r->plane[counter2].equy, r->plane[counter2].equz, r->plane[counter2].equd, &x, &y, &z, &t);
if (t > 0)
{
p.x = x;
p.y = y;
p.z = z;
if ((ispointonpolygon(r->plane[counter2].a, r->plane[counter2].b, r->plane[counter2].c, r->plane[counter2].d, p)) == true)
{
if (((x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1)) < ((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1)))
return 1;
}
}
++counter2;
}
return 0;
}

//equx is the "a" coffient in the ax+by+cz+d = 0 plane equation, etc for all..

void calculatelineplaneintersection(float a1, float a2, float a3, float b1, float b2, float b3, float equx, float equy, float equz, float equd, float *x, float *y, float *z, float *treturn)
{
float t1, t2, t;
t = 0;
t1 = -(equx*a1)-(equy*a2)-(equz*a3)-equd;
t2 = (equx*b1)-(equx*a1)+(equy*b2)-(equy*a2)+(equz*b3)-(equz*a3);
if (t2 != 0)
t = (float)t1/(float)t2;
*treturn = t;
if (t > 0)
{
*x = a1+((float)t*b1)-((float)t*a1);
*y = a2+((float)t*b2)-((float)t*a2);
*z = a3+((float)t*b3)-((float)t*a3);
}
else
return;
}

bool ispointonpolygon(vector a, vector b, vector c, vector d, vector p)
{
vector abxap, bcxbp, cdxcp, daxdp;
float abxapdotbcxbp, cdxcpdotdaxdp, abxapdotcdxcp;

abxap = cross_product(vectorize(a, b), vectorize(a, p));
bcxbp = cross_product(vectorize(b, c), vectorize(b, p));
cdxcp = cross_product(vectorize(c, d), vectorize(c, p));
daxdp = cross_product(vectorize(d, a), vectorize(d, p));
abxapdotbcxbp = dot_product(abxap, bcxbp);
cdxcpdotdaxdp = dot_product(cdxcp, daxdp);
abxapdotcdxcp = dot_product(abxap, cdxcp);

if ((abxapdotbcxbp >0) && (cdxcpdotdaxdp > 0) && (abxapdotcdxcp > 0))
return 1;
else
return 0;
}

vector vectorize(vector a, vector b)
{
vector ab;
ab.x = b.x-a.x;
ab.y = b.y-a.y;
ab.z = b.z-a.z;
return ab;
}

float dot_product(vector a, vector b)
{
float result;
result = (a.x*b.x)+(a.y*b.y)+(a.z*b.z);
return result;
}

vector cross_product(vector a, vector b)
{
vector c;
c.x = (a.y*b.z)-(a.z*b.y);
c.y = (a.z*b.x)-(a.x*b.z);
c.z = (a.x*b.y)-(a.y*b.x);
return c;
}

thanks for any positive comment

##### Share on other sites
The algorithm I use goes like this:

Precompute the polygon dominant axis, that is if the normal is <0,0,1> then it would be z-dominant, so store this for future use.

Calculate the intersection point with the plane.

Translate all vertices so that the intersection point lies at (0,0,0)

Project all points on the the non-dominant axiis (in this case it's z so we would project everything to the x,y plane)

Draw an arbitrary ray from the center and calculate the number of times it crosses a polygon boundry (using a ray like <0,1> or <1,0> would probably be best, but it's up to you)

If the number of times it crosses is odd, then it intersects, if even then it does not intersect.

Ray-polygon intersection algorithm

A technique based on barycentric coordinates

(edit) You might try passing your vectors by reference instead of by value, and you should look into SSE intrinsics if you'll be doing a lot of ray shooting

##### Share on other sites
SSE intrinsics ??
what's that?
and anyways im not working in the way you suggested, i already have my working mechanism for intersecting rays. but its just that some points are disappearing.. and i don't know why the reason.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634132
• Total Posts
3015731
×