Distance from point to polygon plane
Yesterday i wrote a function that returns the distance between a
point and the plane of a polygon. Here it is:
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return(DotProduct(p,&plane->N) - plane->d);
}
Here is what the structures i use look like:
typedef struct tagPlane
{
Vertex N; // Plane normal
float d;
}Plane,*Plane_ptr;
typedef struct tagVertex
{
float x,y,z;
}Vertex,*Vertex_ptr;
And here are the cross and dot product functions:
void CrossProduct(Vertex_ptr u,Vertex_ptr v,Vertex_ptr result)
{
result->x = (u->y*v->z - u->z*v->y);
result->y = -(u->x*v->z - u->z*v->x);
result->z = (u->x*v->y - u->y*v->x);
}
float DotProduct(Vertex_ptr u,Vertex_ptr v)
{
return( (u->x * v->x) + (u->y * v->y) + (u->z * v->z));
}
Here is how i calculate the plane parameters:
Plane wall;
Vertex v1;
Vertex v2;
// Get vector between p2 and p1
VectorSubstract((Vertex_ptr)&p2,(Vertex_ptr)&p1,(Vertex_ptr)&v1);
VectorNormalize((Vertex_ptr)&v1);
// Get vector between p3 and p1
VectorSubstract((Vertex_ptr)&p3,(Vertex_ptr)&p1,(Vertex_ptr)&v2);
VectorNormalize((Vertex_ptr)&v2);
// Calculate plane parameters
CrossProduct((Vertex_ptr)&v2,(Vertex_ptr)&v1,
(Vertex_ptr)&wall.N);
VectorNormalize((Vertex_ptr)&wall.N);
wall.d = DotProduct((Vertex_ptr)&wall.N,(Vertex_ptr)&p1);
The distance function works just fine as long as the polygon i''m testing is standing straight up(like a wall), but when it is sloped the distance is incorrect. To understand this better, here are the points a use:
This is the "straight up" version:
Vertex p1 = {-1.0f, 1.0f, 0.0f},
p2 = { 1.0f, 1.0f, 0.0f},
p3 = { 1.0f,-1.0f, 0.0f};
This is how it looks when renderd:
+--------+
\ |
\ |
\ |
\ |
\ |
\ |
\ |
\|
+
And this is the "sloped" version:
Vertex p1 = {-1.0f, 1.0f, -1.0f},
p2 = { 1.0f, 1.0f, -1.0f},
p3 = { 1.0f,-1.0f, 0.0f};
Here i put the Z values of the upper vertices back into the scene to give it a "downhill" slope (rotated around the X axis) and thats when the distance function fails. However, i tried to but the polygon at diffrent angle around the Y axis and it worked out just fine.
So the question is why does the distance function give incorrect results when the polygon is rotated around the X axis? Are my Dot and/or Cross functions incorrect? etc.
Let me now if none of this doesn''t make any sense or if you need more information.
Thanks.
PS.
I got my information for the DistanceToPlane calculation from the
book "Beginning Direct3d game programming", if that means anything.
don''t have much time but this may help you:
plane: ax + by + cz + d = 0
point: u/v/w
distance = k
k = |(a*u + b*v + c*w + d) / (a^2 + b^2 + c^2)^(0.5)|
please send any questions, comments or if it''s even wrong to
___@swissonline.ch
plane: ax + by + cz + d = 0
point: u/v/w
distance = k
k = |(a*u + b*v + c*w + d) / (a^2 + b^2 + c^2)^(0.5)|
please send any questions, comments or if it''s even wrong to
___@swissonline.ch
first of all i hope u know that the distance calculated is always the pependicular distance from that point
to the plane. I found lot of peoples making mistakes at this point. I hope u dont fall into that group
well.. the function
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return(DotProduct(p,&plane->N) - plane->d);
}
will return correct value ONLY IF the normal of the plane is Normalized. Otherwise it will return incorrect values.
So in order make sure the function always return correct values use the following code
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return( (DotProduct(p,&plane->N)/vectorNormalize(&plane->N) ) - plane->d);
}
If it still doens''t solve ur problem then plz check the CrossProduct function once again. ( sorry i dont
have the time.... )
The DotProduct function seems to be alright to me
browny
to the plane. I found lot of peoples making mistakes at this point. I hope u dont fall into that group
well.. the function
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return(DotProduct(p,&plane->N) - plane->d);
}
will return correct value ONLY IF the normal of the plane is Normalized. Otherwise it will return incorrect values.
So in order make sure the function always return correct values use the following code
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return( (DotProduct(p,&plane->N)/vectorNormalize(&plane->N) ) - plane->d);
}
If it still doens''t solve ur problem then plz check the CrossProduct function once again. ( sorry i dont
have the time.... )
The DotProduct function seems to be alright to me
browny
Z
September 19, 2001 07:31 AM
Thanx for the help guys, but i will not be able to try your
advice right now because i''m in a bit of a hurry.
To browny:
I''m pretty sure that by CrossProduct is correct because
i draw a line for the center of the world (0,0,0) to the
normal vector, and it''s always perpendicular to the triangle.
Now a question about the modified version of the distance
function you wrote:
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return( (DotProduct(p,&plane->N)/vectorNormalize(&plane->N) ) -plane->d);
}
Just to be sure i assume that the "vectorNormalize()" is
supposed to return the length of the normal vector, but i''m
not sure. Please correct me if i''m wrong.
advice right now because i''m in a bit of a hurry.
To browny:
I''m pretty sure that by CrossProduct is correct because
i draw a line for the center of the world (0,0,0) to the
normal vector, and it''s always perpendicular to the triangle.
Now a question about the modified version of the distance
function you wrote:
float DistanceToPlane(Vertex_ptr p,Plane_ptr plane)
{
return( (DotProduct(p,&plane->N)/vectorNormalize(&plane->N) ) -plane->d);
}
Just to be sure i assume that the "vectorNormalize()" is
supposed to return the length of the normal vector, but i''m
not sure. Please correct me if i''m wrong.
September 19, 2001 08:32 AM
VectorNormalaize() will return the normalaized vector of the
input vector (a vector with a lenght of 1)
input vector (a vector with a lenght of 1)
Hallo,
das isch ja schön, dass es sogar i dem forum no än schwiizer hät.
hät ich nöd erwartet.
kudi
das isch ja schön, dass es sogar i dem forum no än schwiizer hät.
hät ich nöd erwartet.
kudi
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
Sorry jocke,
I''ve realized that Physik is also from Switzerland and that''s our language.
kudi
I''ve realized that Physik is also from Switzerland and that''s our language.
kudi
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement