• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# point to plane distance

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

## Recommended Posts

How can I find the distance between a point and a plane?

##### Share on other sites
Take the equation of the plane and deduce the normal vector. Then align the normal vector with the point you''re testing, and find the point of intersection between the line (made by scaling the vector in the direction of the plane) and the plane.

I can''t remember the math, but that''s the method.

George D. Filiotis
Are you in support of the ban of Dihydrogen Monoxide? You should be!

##### Share on other sites
I wrote a function to do this last night! I will post it here tomorrow(2/21/2) afternoon.

##### Share on other sites
If you have a plane containing the point p1 with a unit normal of n and want to find the distance of the point p2 from that plane then it is n.p2-n.p1 or n.(p2-p1). So if n=(1,0,0), p1=(1,y1,z1) and p2=(2,y2,z2) then (1,0,0).(2,y2,z2)=2 and (1,0,0).(1,y1,z1)=1 and the distance between them is 1. It is actually a directed distance in that it has a sign. If the point had of been (0,y2,z2) instead it would have been -1. That is the directed distance from the plane to the point and if you want the directed distance from the point to the plane you would just switch p1 and p2. If you do that then the closest point on the plane to the point is p2+n*(n.(p1-p2)). So using the same values (2,y2,z2)+(1,0,0)*((1,0,0).((1,y1,z1)-(2,y2,z2))) = (2,y2,z2)+(1,0,0)*-1 = (1,y2,z2).

##### Share on other sites
Crappy C example:

typedef struct
{
float a,b,c,d;
} PlaneEquation;

typedef struct
{
float x,y,z;
} Point;

float PlaneDist(Point *p,PlaneEquation *thePlane)
{
float dist = thePlane->a*p->x + thePlane->b*p->y + thePlane->c*p->z + thePlane->d;
return dist;
}

##### Share on other sites
Here's various collision detection algorithms I've recently completed:

  double DotProduct(const double v0[3], const double v1[3]){ return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];}void CrossProduct(const double v0[3], const double v1[3], double result[3]){ result[0] = v0[1] * v1[2] - v0[2] * v1[1]; result[1] = v0[2] * v1[0] - v0[0] * v1[2]; result[2] = v0[0] * v1[1] - v0[1] * v1[0];}void VecAdd(const double v0[3], const double v1[3], double result[3]){ result[0] = v0[0] + v1[0]; result[0] = v0[1] + v1[1]; result[0] = v0[2] + v1[2];}void VecSub(const double v0[3], const double v1[3], double result[3]){ result[0] = v0[0] - v1[0]; result[0] = v0[1] - v1[1]; result[0] = v0[2] - v1[2];}bool RaySphereTest(const double o[3], const double d[3], const double c[3], const double r, double result[3]){ double l[3]; VecSub(c, o, l); double d_proj = DotProduct(l, d); double l_proj = DotProduct(l, l); double r_sq = r * r; if(d_proj < 0 && l_proj > r_sq) return false; double m_sq = l_proj - d_proj * d_proj; if(m_sq > r_sq) return false; double q = sqrt(r_sq - m_sq); double t; if(l_proj > r_sq) t = d_proj - q; else t = d_proj + q; result[0] = o[0] + t * d[0]; result[1] = o[1] + t * d[1]; result[2] = o[2] + t * d[2]; return true;}bool RayTriTest(double o[3], double d[3], double v0[3], double v1[3], double v2[3], double result[3]){ double e1[3] = {v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]}; double e2[3] = {v2[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]}; double p[3]; CrossProduct(d, e2, p); double a = DotProduct(e1, p); double f = 1/a; double s[3]; VecSub(o, v0, s); double u = f * DotProduct(s, p); if(u < 0 || u > 1.0) return false; double q[3]; CrossProduct(s, e1, q); double v = f * DotProduct(d, q); if(v < 0 || u + v > 1.0) return false; double t = f * DotProduct(e2, q); result[0] = o[0] + t * d[0]; result[1] = o[1] + t * d[1]; result[2] = o[2] + t * d[2]; return true;}bool SphereSphereTest(const double c0[3], const double r0, const double c1[3], const double r1){ double d_sq = (c0[0] - c1[0]) * (c0[0] - c1[0]) + (c0[1] - c1[1]) * (c0[1] - c1[1]) + (c0[2] - c1[2]) * (c0[2] - c1[2]); if(d_sq > (r0 + r1) * (r0 + r1)) return false; return true;}bool SpherePlaneTest(const double c[3], const double r, const double n[3], const double p[3]){ double tp[3]; VecSub(p, c, tp); if(DotProduct(n, tp) > r) return false; return true;}double VertexPlaneTest(const double v[3], const double n[3], const double p[3]){ double tp[3]; VecSub(p, v, tp); return DotProduct(n, tp);}

Sorry, but I don't believe in commenting my code!

Edited by - acw83 on February 21, 2002 4:02:33 PM

Edited by - acw83 on February 21, 2002 4:04:12 PM

• ### What is your GameDev Story?

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

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015466
×