Finding on what side of a polygon a point lays.

Hi guys, this problem has been driving me nuts, I hope you can help!

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:

float distance = 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).

So instead of “point”, you’d need “point - <point on polygon’s plane>”.

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!

                       Vector3f direction = Vector3f.sub(surface center, camera position, null);
	                        float angle = Vector3f.dot(surface normal, direction);
                                if(angle > 0.0)
				{
					//On the back side of the face
				}
				if(angle <= 0.0)
				{
                                      //On the front side of the face
                                }


Cool, glad you got it working!

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 :slight_smile: 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.

Aha, so:

(normalized surface normal) dot (point - <any point on plane>) = orthogonal distance to plane, from point?

That’s handy, and makes sense if you think about it since that’s what cosine really is.

In that case a zero result would also give the orthogonal distance though, no? :slight_smile: I think you may have been talking about something else there, then.

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?

public void getsector()
	{
		campos = new Vector3f(camera.posx, camera.posy, camera.posz);
		for(int i=0; i<level.getworld_sectors(); i+=1)
		{
				int g = level.sector[i].surfaces[0];
			Inner:
			for(int s=0; s<level.sector[i].surfaces[1]; s+=1)
			{
					direction = Vector3f.sub(campos, level.surface[g+s].surfacep, null);
					float cos = Vector3f.dot(level.surface[g+s].normal, direction);
				
				if(cos < 0.0f)
				{
					break Inner;
				}
				if(cos > 0.0f)
				{
					if(s == level.sector[i].surfaces[1]-1)
					{
						System.out.println("Sector: "+i);
						break;
					}
				}			
			}
		}
	}

[edit]

So I figured it out, it turns out my opengl translations were screwed up, so the camera wasn’t where I thought. So the above code works fine!