Archived

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

WhatEver

Distance from a plane?

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Here's my code then:

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

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

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
I think I would first try just flipping left and right in the call to glFrustum. The only errors lists are near or far not being positive or trying to call glFrustum in the middle of a glBegin/glEnd block. I also think I would use two viewports for testing. That should make it easier to figure out just what went wrong. I tend to code by successive approximation so I would first test that flipping left and right worked as expected, then that I reflected the position and view angle correctly, then that I clipped it correctly and finally that I merged them correctly. I''m basically lazy so rather than spending a bunch of time walking through the code I let the computer tell me what the code does.

Share this post


Link to post
Share on other sites