Given a polygon with a surface normal, and a point, I need to find find where the point is in relation to the poly, either inside - the side the normal points to, or outside. What I have found from searching online, is that the dot product of the point and surface normal should result in 0 at the surface and a positive number inside the surface. So far I have:

1

floatdistance = Vector3f.dot(surface.normal, point);

And this almost works - except that the 0 position is not on the surface, but some distance away. Why?

My guess is the "point" variable you're passing in is the actual point you're testing. What it needs to be is the direction to that point from some point on the polygon (say, the center - though any point will do - probably easiest to pick one of the vertices).

I'd be careful - it sounds like your problem is a bit ill-defined. "Inside a polygon" doesn't make much sense in 3d. What the dot product will tell you is which side of the plane the test point is on. In that case, you're assuming that all the points on the polygon are on the same plane, which isn't necessarily the case unless it's a triangle.

Oh, I see! Thank you that does help. Do you happen to know how to calculate the direction vector?

[edit] Never mind I've got it, it works great!

1 2 3 4 5 6 7 8 9 10

Vector3fdirection = Vector3f.sub(surfacecenter, cameraposition, null);floatangle = Vector3f.dot(surfacenormal, direction);if(angle > 0.0) {//On the back side of the face }if(angle <= 0.0) {//On the front side of the face }

You got two things backwards, btw. Because it's two, they cancel each other out and it works, but still - your direction vector is actually pointing away from the point. The other thing is it ought to be <0 for the back side of the face. A minor, pedantic point, but still.

Also, I wouldn't call the dot product an angle, it's most certainly not It's the cosine of the angle between the two vectors, but only if the vectors are normalized. (Normalized means length = 1).

BTW: An alternate way to think about this problem is to forget about vectors and think of it in terms of the implicit plane equation, so the result is zero if the point is in the plane, positive if on the oriented side (that in which the normal points) and otherwise negative. If the plane equation is normalized a non-zero results give the orthogonal distance of the point to the plane. Computationally they are identical to one another.

I didn't word that very well, so yes if the point is on the plane then indeed it's zero distance from the plane. The vector-centric version (subtracting any point on the plane from the test point) is basically saying: translate the problem to the origin and find the distance to the plane now through the origin.

d = dot(N,P-V)

Where 'V' is any point on the plane. Translating the implicit plane equation into vectors:

d = dot(N,P) + m

Expanding the vector-centric version yields:

d = dot(N,P) - dot(N,V)

So you get: m = -dot(N,V).

In both cases I think it's more informative to think of the dot as being the parallel projection and in this case distance when normalized.

I am still having a bit of an issue with this. It worked fine with my original test level (a box) , but I've since tested it with some slightly more complex levels. First of all some info - My levels are made up of non-convex sectors, which are made up of non-convex surfaces. I'm looping through each sector, then through each surface of the sector. If it passes the above calculation for every surface of a sector, then I know what sector the camera is in.

The problem is, it starts to return a negative cosine for some surfaces when it shouldn't. For example, I've made a test level with 3 box sectors. It works fine for the first 2 sectors, but one surface of the third sector always returns a negative. Any thoughts?

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org