Advertisement

Distance to a plane

Started by December 17, 2002 10:28 PM
10 comments, last by MattCarpenter 22 years, 2 months ago
To find the distance to a plane, here are the 2 numbers you need:

* The normal of the plane
* The planeshift of the plane

You can find the normal of the plane by calculating the cross product of 2 vectors that lie on your plane. So if you have the verticies of 3 points pA, pB, and pC, you know that vectors pA - pC and pB - pC lie on the plane. So the normal can be calculated:

// pA, pB, and pC are all points on your plane
Vector v1 = pA - pC;
Vector v2 = pB - pC;

normal = CrossProduct(v1, v2);
for (int i = 0; i < 3; i++)
{
normal /= normal.length();
}

The last line few lines normalizes the normal. When you normalize a vector, you keep it direction the same, but you reduce its length to 1 unit. You always need normalized normals.

After you have the normal, you need the planeshift value. The planeshift value is the dot product of the normal of a plane and a point on the plane. So you calculate planeshift:

float planeshift = DotProduct(normal, pA);

Now that you have the planeshift and the normal of the plane, you can find the distance using the plane equasion. So you get:

float distance = DotProduct(normal, object.position) - planeshift;

After you find your distance, then the real fun begins. If you''re using spherical bounding boxes, you also need to subtract the radius of the object tested. Also, just knowing how far away an object is away from a plane DOES NOT tell you if it passed through a wall or other polygon. Understand a wall can be a mile away from you, but it''s PLANE extends in all 4 directions to infinity. Thus, you still need to test if the object is passing through the plane WITHIN the boundaries of the polygon.

Hope this helped ^^



"You TK''ed my chicken!"
distance= (-n*B -d)n*n

d=-n*A
distance= (n*A-n*B)n*n
http://www.8ung.at/basiror/theironcross.html

This topic is closed to new replies.

Advertisement