normal vector. how to compute it?

hi,

I have some difficulties to compute a normal vector between two vectors AB * AC. Is someone could help me please?

thanks!!

hi,

use the crossproduct!

void Vector3f::CrossProd(const Vector3f& vec,Vector3f& normal)
{
normal.x = yvec.z-zvec.y;
normal.y = -xvec.z+zvec.x;
normal.z = xvec.y-yvec.x;
}
Snippet from my library.

Happy coding,

Martin

here is my cross product function:

void crossProduct(vertex3D a, vertex3D b, vertex3D c, vertex3D d)
{
d[0]=(b[1]-a[1])(c[2]-a[2])-(c[1]-a[1])(b[2]-a[2]);
d[1]=(-(b[0]-a[0])(c[2]-a[2])-(c[0]-a[0])(b[2]-a[2]));
d[2]=(b[0]-a[0])(c[1]-a[1])-(c[0]-a[0])(b[1]-a[1]);
normal(d);
}

can you see a mistake?
My normal vectors point into the wrong direction.

Hi, the cross product is like this ->

void vec3_t::CrossProduct(const vec3_t &v1,const vec3_t &v2)
{
v[0] = ( v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1] );
v[1] = ( v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2] );
v[2] = ( v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0] );
}

Old GLman

Hi, I have this tick that I always exactly want to know what something does and why…

i know what this does, but what is the mathamatical explaination (im just 15 years old so i dont have this on school yet) but can anyone post an URL on the mathamatical explaination of this crossproduct??

thx alot

The direction of the normal is dependent on the order of the three vertices to use. Switch a and c around and it will point in the correct direction.

The dot product of two vectors is 0 if they are orthogonal to each other. You want a vector that is orthogonal to two vectors. Thus you have the equation system:

v1 * normal = 0
v2 * normal = 0

v11 * normal1 + v12 * normal2 + v13 * normal3 = 0 (1)
v21 * normal1 + v22 * normal2 + v23 * normal3 = 0 (2)

Now set normal1 = 1, just to eliminate one unknown (this step will be done backwards at the end). You’ll get:

v11 + v12 * normal2 + v13 * normal3 = 0 (1)
v21 + v22 * normal2 + v23 * normal3 = 0 (2)

Solve (1) for normal2

normal2 = (-v11 - v13 * normal3) / v12

Insert into (2):

v21 + v22 * (-v11 - v13*normal3)/v12 + v23 *normal3 = 0

Solve for normal3:
normal3 = (v11v22-v12v21)/(v12v23-v13v22)

Solve (1) for normal3:
normal3 = -(normal2*v12+v11)/v13

Insert into (2):
v21 + v22normal2 + v23 * -(normal2v12+v11)/v13 = 0

Solve for normal2:
normal2 = (v13v21-v11v23)/(v12v23-v13v22)

Thus we have the vector:

normal1 = 1
normal2 = (v21v13-v11v23)/(v12v23-v13v22)
normal3 = (v11v12-v21v12)/(v12v23-v13v12)

Now we can scale the vector with (v12v23-v13v12), so we finally have:
normal1 = (v12v23-v13v22)
normal2 = (v13v21-v11v23)
normal3 = (v11v22-v12v21)

[This message has been edited by Michael Steinberg (edited 05-05-2002).]