Calculating normal of a quad

Blitz3D Forums/Blitz3D Programming/Calculating normal of a quad

Stevie G(Posted 2003) [#1]
Say I have a xz quad with vertexes of differing y values how do I calculate the normal of the quad?

eg.
vertex 0 = (0,5,0)
vertex 1 = (10,1,0)
vertex 2 = (10,7,10)
vertex 3 = (0,2,10)

Cheers..


Shambler(Posted 2003) [#2]
Although the definition of a quad is just 4 vertices usually these vertices are coplanar i.e. the quad is flat.

To get the normal of a quad is the same as getting the normal for either of its 2 triangles.

Your quad however is not coplanar so it will have two normals, one for each triangle.


Stevie G(Posted 2003) [#3]
Get you. Is there a method that I can use to get the average normal of the quad? I know it has something to do with the cross products. I can't use the inbuilt update normals command for what I'm doing unfortunately.


Shambler(Posted 2003) [#4]
To find the average normal of the quad I'd guess its just a matter of adding the normals of the two triangles together,divide by 2 then normalise the result.

Here's some code which should show you the way...

Type tri
Field vx#[3],vy#[3],vz#[3]
Field tnx#,tny#,tnz#
End Type



Function createtri(vx1#,vy1#,vz1#,vx2#,vy2#,vz2#,vx3#,vy3#,vz3#)

;make our triangle
t.tri=New tri
t\vx[1]=vx1
t\vy[1]=vy1
t\vz[1]=vz1
t\vx[2]=vx2
t\vy[2]=vy2
t\vz[2]=vz2
t\vx[3]=vx3
t\vy[3]=vy3
t\vz[3]=vz3

;subtract vectors
ax#=t\vx[2]-t\vx[1]
ay#=t\vy[2]-t\vy[1]
az#=t\vz[2]-t\vz[1]
bx#=t\vx[3]-t\vx[2]
by#=t\vy[3]-t\vy[2]
bz#=t\vz[3]-t\vz[2]

;calculate face normal using cross product
t\tnx=ay*bz-az*by
t\tny=az*bx-ax*bz
t\tnz=ax*by-ay*bx

;normalize it 
l#=Sqr(t\tnx*t\tnx+t\tny*t\tny+t\tnz*t\tnz)
t\tnx=t\tnx/l
t\tny=t\tny/l
t\tnz=t\tnz/l
End Function 



GitTech(Posted 2003) [#5]
EDIT: Ah, somebody was faster than me :P (And I forgot to normalize the vector :P)

Code to calculate the normal of a triangle:

vx0#=0: vy0#=5: vz0#=0
vx1#=10: vy1#=1: vz1#=0
vx2#=10: vy2#=7: vz2#=10

px#=vx1-vx0
py#=vy1-vy0
pz#=vz1-vz0

qx#=vx2-vx0
qy#=vy2-vy0
qz#=vz2-vz0

nx#=(py*qz)-(pz*qy)
ny#=(pz*qx)-(px*qz)
nz#=(px*qy)-(py*qx)


Btw, this code is not tested, as I'm not using Blitz3D atm :)


Stevie G(Posted 2003) [#6]
Great - thanks guys!!