I'm having some issues with some math in my code, and am totally stuck. Here is my cry for help!

Problem:

I have a point in space, and a sphere. The sphere moves, the point in space [at this point] does not. What I need, is to know if any of the tris on this sphere, are facing the point. Note, I'm calculating this every frame, and the sphere could move anywhere in the world.

My Approach So Far:

For every face [tri] of this sphere, I am calculating a plane. (foot#1 for code). After calculating this plane for every face, I test each plane for being 'facing' the point or not (foot#2 for code). I am testing to see results, by coloring the tris different colors if they are facing or not.

The problem:

This doesn't seem to work really at all. I get a few positives, but they don't make sense at all really, and they don't change when I move my sphere around.

I got the general idea of this from an article on nvidia's developer site, the code they use looks something like this (foot #3), but the variable names they are using don't seem to make a lot of sense to me.

- Make sure both object coordinates and light position are in the same coordinate system! Your code looks as if you transform the light into object coordinates and the object into world coordinates.
- Calculate the face normal (cross product, have your coordinate system's handedness (is that a word http://www.opengl.org/discussion_boards/ubb/wink.gif) right!)
- Calculate the vector from footpoint to light.
- Normalize(!) both vectors.
- Dot product of normal and light vector gives the cosine of the angle between the two.
If the result is < 0 the triangle is facing away from the light, == 0 is edge on, > 0 faces the light.

Kent767

12-02-2002, 04:01 AM

im not good at math or anything... but as a rule floats/doubles etc.. are hard to get exact results with... floats rarely hit exactly 0... maybe define two bounds for zero -.00000000000001 and .0000000000001 for example
then check the result of your calculations against those values...

Thanks a lot, I got that algorithm working!!! I've got a question though, getting a >= on the dot should give me > 0 if it the tri faces the light, but it's backwords of this... perhaps my normals are backwords ?

Relic

12-02-2002, 07:49 AM

That can happen if you have your cross product done in the opposite direction.
If you have to vectors like this

B
|
|
+------A

and you're working in a right handed coordinate system then A x B points out of the screen, but B x A points into the screen.

You should strive to have your triangle's front face winding to be counterclockwise in a right handed coordinate system, then all fits together mathematically.

Do the famous computer graphics aerobics. http://www.opengl.org/discussion_boards/ubb/biggrin.gif
Right handed: Thumb is x pointing to the right, forefinger is y pointing up, middle finger is z pointing towards you.
Left handed: Thumb is x pointing to the right, forefinger is y pointing up, middle finger is z pointing away from you.