I know that you can find it a polygon is facing the camera using that method.You need the polygon normal and the position of a vertex on that polygon.I don't remember more.

Can someone explain with details,and maybe some drawings?

Thanks

Started by noatom, Jun 29 2013 07:11 AM

9 replies to this topic

Sponsor:

Posted 29 June 2013 - 07:15 AM

You take the dot product of the normal and the vector between the viewpoint and one of the vertices. A negative dot product means the side of the polygon in the direction of the normal is visible from the view point.

**Edited by Brother Bob, 29 June 2013 - 07:16 AM.**

Posted 29 June 2013 - 07:16 AM

If you do the dot product of the normal and the vector from the camera to a point on the polygon it will be negative if they are facing in opposite directions, which means the polygon is facing the camera.

Dot product positive -> vectors are facing same direction (within 90 degrees each way)

Dot product negative -> vectors facing different directions

Dot product 0 -> vectors are perpendicular

You can also use the z-component of the cross product of polygon edge directions in screen space to check if the polygon is back facing.

EDIT: My first answer was incorrect, edited now.

**Edited by Paradigm Shifter, 29 June 2013 - 07:19 AM.**

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Posted 29 June 2013 - 07:38 AM

I know that you can find it a polygon is facing the camera using that method. Can someone explain with details,and maybe some drawings?

There are several formulas to define a dot product, just like there are several formulas that can define a line.

One of those formulas is:

DotProduct = |A| * |B| * cos(AngleBetweenAB)

The dot product equals the magnitude (length) of the first vector, times the magnitude of the second vector, times the cosine of the angle between the two vectors.

If both vectors are normalized (length = 1) they drop out of the equation.

So if we have *normalized vectors* A and B: DotProduct = cos(AngleBetweenAB).

If you remember from the definition of a cosine, angles between -90 degrees and 90 degrees are all positive, with 90 and -90 being exactly zero.

So if one vector is pointing forward and the dot product is positive, the second vector is also pointing forward. It might only be a tiny bit forward, but it is still at least partially forward.

The opposite is also true: if one vector is pointing forward and the dot product is negative, the second vector is pointing backwards. It might only be a tiny bit backward, but it is still at least partially backward.

And since for the normalized angles DotProduct = cos(AngleBetweenAB), that means you can undo the cosine: AngleBetweenAB = arccos(DotProduct)

Sorry no pictures, but I hope that clears it up.

**Edited by frob, 29 June 2013 - 07:57 AM.**

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.

Posted 29 June 2013 - 07:43 AM

If you are just testing relative facing (towards/away from/perpendicular) you don't need to normalise, all that matters is the sign (+ve -> facing same way, -ve -> facing opposite way, 0 -> perp).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Posted 29 June 2013 - 08:37 AM

Ok,got it:

If you are just testing relative facing (towards/away from/perpendicular) you don't need to normalise, all that matters is the sign (+ve -> facing same way, -ve -> facing opposite way, 0 -> perp)

But I still have problems visualizing the test.For example:

What I think happens is:

Obviously,my thoughts are wrong,but can someone say where and why? Thanks

Posted 29 June 2013 - 08:46 AM

The angles you measure in for the two polygons have different references. Notice how on the left side the angle is measured between the normal and the red line, while on the right hand side the angle is measured between the *inverse of the normal* and the red line.

Posted 29 June 2013 - 10:04 AM

The angle is between the blue line and the red line. I thought the blue line was the extended normal from the origin since that is what you measured the angle from and not the black line itself. What are the red and blue lines then in your picture, just to make sure we're talking about the same thing.

Posted 29 June 2013 - 06:29 PM

Your angle measurement is a misinterpretation. Think of the vectors as free vectors, not localized.Now imagine both vectors(the normal and the camera to vertex vector) with their tails attached. The polygon on the left has a normal pointing away from the blue vector. If you do the same on the right, you can see they point the same direction.