• ### Announcements

#### Archived

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

# Distance from a plane?

## Recommended Posts

WhatEver    125
Is this correct? TrinaglePos=Tx*Nx+Ty*Ny+Tz*Nz; PointPos=Px*Nx+Py*Ny+Pz*Nz; Distance=sqrt(PointPos-TrianglePos); T is a Triangle vertex. P is the Point in space. N is the Normal. If I''m right, I''m really starting to understand what the Dot Product does.

##### Share on other sites
char_e    122
I''m unclear on what you meant, but I do understand the topic title. Given a plane ax+by+cz+d=0 and a point P(x0,y0,z0), then the distance of the point from the plane is given by

abs(a*x0+b*y0+c*z0+d) / sqrt(a^2+b^2+c^2)

##### Share on other sites
LilBudyWizer    491
TrianglePos and PointPos is the distance of two parallel planes from the origin in units of the magnitude of the normal. TrianglePos is -D in char_e''s equation. So you want abs(P.N-T.N)/|N|. If |N|=1 then that is just abs(P.N-T.N).

I assume you have a normal with a magnitude of the unit distance of the plane from the origin. That makes P.N=N.N for every point in the plane, |N|=D and N.N=|N|^2=D^2 so P.N=D^2. If you divide through by D though you get P.N/D=D or P.N/|N|=D for every point in the plane. P.N/|N|!=D for any point outside the plane though. Instead it is the distance from the origin of a plane containing that point with the same normal from the origin, call that D2. The directed distance from one plane to the other is D2-D. So P.N-T.N=D*(D2-D).

##### Share on other sites
WhatEver    125
Thank you guys!

I guess I didn''t quite understand it...but I was close :D

I get it now.

##### Share on other sites
WhatEver    125
Here's my code then:

DistanceFromPlane=(VectorDot3f(n, p)+d))/VectorLength3f(n);

Edited by - WhatEver on January 4, 2002 3:35:01 PM

##### Share on other sites
LilBudyWizer    491
I would suggest testing it with something like the plane z=5 and the point (0,0,2). That makes it nice and simple to calculate the distance from the origin and from the point to the plane. If it comes out wrong then most likely the sign on d is wrong.

##### Share on other sites
WhatEver    125
Thank you LilBudyWizer, I will do that.

This is the method I thought up to mirror the position of an object to the other side of a plane(multiplying the distance from the plane by the planes flipped normal and then adding the result to the player pos)...so I can mirror the world for a stenciled mirror in OpenGL. Is this how the pros do it? I can''t think of any other way.

I just thought of something though. I can''t simply reverse the direction of the players view angle...I KNOW! I can multiply the angle vector by the flipped normal also :D. I think that will work...yeah it should.

##### Share on other sites
LilBudyWizer    491
I''m afraid I can''t help you with the reflection. It is a bit beyond my limited knowledge of computer graphics in general and OpenGL specifically. I mainly just play with math. If I understand what you are saying then the only point I would make is reversing left and right. If image in a mirror is reversed from what you would see if the mirror was a window and you were on the other side looking out.

The normal gives you a direction orthogonal to plane. The normal times the directed distance of a point from the plane is a displacement vector from the point to the plane. What I mean by a directed distance is positive on one side and negative on the other or basically removing the abs() on the calculation of the distance. The reflected position of the point is P+2*D*N. That puts you the same distance from the plane on the other side of the plane. You can reflect a vector V across a plane with V-2*(N.V)*N.

##### Share on other sites
WhatEver    125
Ah yes, I forgot to mention that after mirroring the view vector and position that I would have to flip everything with a glScale call...I don''t think that will work though. Maybe I''ll have to use a lookat matrix and mirror the lookat vectors. That should work.

I''m just trying to straighten out the theory before I actually code this effect. I''m busy doing other things right now, so when I actually attempt to make a mirror, then I can do it with little debugging <------ I like that idea .

##### Share on other sites
WhatEver    125
quote:
If image in a mirror is reversed from what you would see if the mirror was a window and you were on the other side looking out.

And that is exactly how I have it in my mind!