3d Math problem

BlitzMax Forums/BlitzMax Programming/3d Math problem

Craig H. Nisbet(Posted 2009) [#1]
Hey guys,

I'm trying to make a 2d game that is using some 3d-ish features. First off I really suck at math, so this is probably easy and I'm just stupid.

I have a triangle that has 3 points(duh!). Plus I have normals for those points. I'm pulling this point data from an "obj" file. What I would like is the normal for the center of the triangle. Is there a way to get the correct facing normal for a polygon that I have the point normal information for?

I've tried using a function I got from the code archive that gets the normal for a triangle, but it's really picky about the creation order of the points. I get inverted normals half the time. Since I'm using an external program, I don't have control over the creation order of the points.

Hope that makes sense.

Craig


_JIM(Posted 2009) [#2]
Did you try getting an average of the normals of the 3 points? If I were to think of a simple solution, this would be the one. (N1 + N2 + N3) / 3

Not an expert in math myself, but I think this should be it.


Craig H. Nisbet(Posted 2009) [#3]
Yeah I tried that. It works, but it's doesn't give the best results, for what I'm making.


matibee(Posted 2009) [#4]
Or a double step... Average the existing normals as JIM suggested, calculate your own normal as you are doing, compare the two with a Vector Dot Product. If they're not close flip yours.

Are you using a vector mod with this functions?

The function isn't being 'picky' :) there's always two normals to a set 3 points.


Floyd(Posted 2009) [#5]
Calculate a normal for the triangle using the cross product of two vectors pointing along edges of the triangle, which I assume you are already doing. Call this normal N. The vector -N is also perpendicular to the triangle. So which one do you want?

You also have the vector obtained from the sum of the three vertex normals. You don't need to divide by 3 for the average since that doesn't change the direction. I suppose this points roughly in the direction you want, toward the "front" of the triangle. Take the dot product with N. If the result is negative then use -N, otherwise use N.

You may also want to scale N to have length 1.


Warpy(Posted 2009) [#6]
Man, I need to do a blog post about vectors, this has come up a few times recently.
Floyd had it right, but it probably doesn't make any sense if you don't know much maths.

Here's a function to compute the normal of a triangle given as 3 points:
Function computenormal(x1#,y1#,z1#,x2#,y2#,z2#,x3#,y3#,z3#, nx# Var,ny# Var, nz# Var)	'computes normal vector and stores it in nx,ny,nz

	'find vector from point 1 to point 2
	dx1#=x2-x1
	dy1#=y2-y2
	dz1#=z2-z1
	
	'find vector from point 1 to point 3
	dx2#=x3-x1
	dy2#=y3-y1
	dz2#=z3-z1
	
	'compute cross product of these two vectors - this is _a_ normal of the triangle
	nx=dy1*dz2-dz1*dy2
	ny=dz1*dx2-dx1*dz2
	nz=dx1*dy2-dy1*dx2
	
	'divide each of the components by the computed normal's length, to get a normal vector which has length 1
	n#=Sqr(nx*nx+ny*ny+nz*nz)
	nx:/n
	ny:/n
	nz:/n
End Function


If I remember after I get back from holiday, I'll write a nice big post all about vectors.


Arowx(Posted 2009) [#7]
Warpy: Would you like to submit it as an article to the next blitzmaxcoder issue 0.2?


Craig H. Nisbet(Posted 2009) [#8]
Cool! I'll give the code a try Warpy. I'm going to start learning math now. I'm getting tired of not knowing this stuff.