PDA

View Full Version : normal vector. how to compute it?



zip7000
05-04-2002, 11:59 AM
hi,

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

thanks!!

mphanke
05-04-2002, 12:12 PM
hi,

use the crossproduct!




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


Happy coding,

Martin

zip7000
05-04-2002, 12:31 PM
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.

Old GLman
05-04-2002, 01:01 PM
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

xill
05-04-2002, 02:53 PM
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

CGameProgrammer
05-04-2002, 04:30 PM
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.

Michael Steinberg
05-04-2002, 10:41 PM
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 = (v11*v22-v12*v21)/(v12*v23-v13*v22)

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

Insert into (2):
v21 + v22*normal2 + v23 * -(normal2*v12+v11)/v13 = 0

Solve for normal2:
normal2 = (v13*v21-v11*v23)/(v12*v23-v13*v22)

Thus we have the vector:

normal1 = 1
normal2 = (v21*v13-v11*v23)/(v12*v23-v13*v22)
normal3 = (v11*v12-v21*v12)/(v12*v23-v13*v12)

Now we can scale the vector with (v12*v23-v13*v12), so we finally have:
normal1 = (v12*v23-v13*v22)
normal2 = (v13*v21-v11*v23)
normal3 = (v11*v22-v12*v21)

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