another angle quest

Blitz3D Forums/Blitz3D Programming/another angle quest

D4NM4N(Posted 2005) [#1]
is there a command to return the angle of slope on a triangle?


Banshee(Posted 2005) [#2]
Isn't that the tan() ? I never have been sure of a use for/of the tan() function, i'm cos()+sin() slap-happy but tan() always reveals a bikini line...


D4NM4N(Posted 2005) [#3]
hi, im not sure, i think tan(a)=a/o (or something like that) but in 3d its hard to find the adacent? (if thats the bottom one) i need something that takes all 3 verts and decides the vertical slop angle


Banshee(Posted 2005) [#4]
I think what your saying is your after a command that reads a triangle in and measures the vertical range and atan2()'s it to the horitzontal range.

Is that right? atan2() can give a bearing by comparing two measurements - so if you give the highestVertexY()-lowestVertexY() as one parameter and the range

dx=highestVertexX-lowestVertexX
dz=highestVertexZ-lowestVertexZ
sqr((dx*dx)+(dz*dz))

as the other parameter and that should work, although you'll need to figure out which parameter is which and whether to subtract high from or the other way around to get the number to return based off the right direction (crossing the numbers over rotates 90 degrees).



It's tan() that confuses me, I cant work out how it gives a bearing from one number (don't explain - I wont find a use for it anyway).

Anyway I hope that's right, it's gone midnight so any mistakes I just made wont get fixed tonight because i'm too tired to spot them...


octothorpe(Posted 2005) [#5]
It's tan() that confuses me...


Tan() is simply the opposite of Atan(). It gives you the slope (rise/run) of a line at the angle provided. Atan2(x,y) = Atan(float(x)/y)

is there a command to return the angle of slope on a triangle?


I believe what you want to do is find the pitch of the triangle's normal. Disclaimer: I have no idea what I'm talking about.

I swiped the Triangle Normal calculation from Yet Another LightMapper (update 1.5 ) by Marcelo and perverted it to only return the Y component of the vector.

ang# = ACos(GetTriangleNormalY(x1,y1,z1,x2,y2,z2,x3,y3,z3))

Function GetTriangleNormalY#(x1#, y1#, z1#, x2#, y2#, z2#, x3#, y3#, z3#)
	ux# = x2# - x1#
	uy# = y2# - y1#
	uz# = z2# - z1#
	vx# = x3# - x1#

	vy# = y3# - y1#
	vz# = z3# - z1#

	nx# = (uy# * vz#) - (vy# * uz#)
	ny# = (uz# * vx#) - (vz# * ux#)
	nz# = (ux# * vy#) - (vx# * uy#)

	; Normalize it
	NormLen# = Sqr((nx*nx) + (ny*ny) + (nz*nz))
	If NormLen > 0
		nx = nx/NormLen : ny = ny/NormLen: nz = nz/NormLen
	Else
		nx = 0 : ny = 0 : nz = 1
	EndIf
	; just the jumbles!  the jumbles!
	Return ny
End Function


I tested this on some real life triangles that I just made up. It seems to work.


big10p(Posted 2005) [#6]
D-Grafix, am I missing something here? You can simply use ATan2() to give you the angle of the hypotenuse of a right-angle triangle. Will your three verts always represent a right-angled triangle, though?


D4NM4N(Posted 2005) [#7]
No the verts are not always at right angle.

Its quite a difficult one, take a triangle of paper and spin it all ways in the air and you will see what i mean.

im trying to implement a feature in my software that textures a slope if at a certain angle.
I think i need to do this:

1: calculate highest point, medium and lowest

2: average the two lower heights

3: create a virtual rightangle tri between the heighest point and the point of average on the other two.

3a:calculate the opposite(vertical) by the height of the heighest and the height of the averaging point

3b:calculate the adjacent as the distance of this point to the base of the vertical

4: do a standard trig sum on this to return desired check angle

its bit 3b i have trouble with. I dont know if this method works or not because i cannot work out the adjacent.


Shambler(Posted 2005) [#8]
Surely the triangle normal is sufficient to find the triangles slope like octothorpe says?

I sometimes use the Y component to check if a triangle is too steep for the player to stand on and if so make them fall off.


D4NM4N(Posted 2005) [#9]
is this always accurate though even if the normals change? i dont know much about normals.
But if the vertex positions are changed dynamically does this effect the normals' values?
or can i updatenormals() and will this recalculate the values so the returned slope is accurate

or am i simply blowing wind? as i say im not really up on normals


Banshee(Posted 2005) [#10]
The normal wont update until you call updatenormals and that can be a slow command on a large mesh. If the retexturing is being done on the fly it might be a bit slow.

I think the suggestion I made works though doesn't it? The two range calculations work out the length of the two funnily named edges of the triangle and atan2 returns the angle based on those edge-lengths.


D4NM4N(Posted 2005) [#11]
its a batch process not realtime so speed isnt an issue

so what does vertexnx() actually do in laymans terms, as ive mever used it and the help is a bit sketchy.

also the vertex routine above, how do i work out the angle from it or is that slope


Ross C(Posted 2005) [#12]
Normals tell you what direction the triangle is facing. It's basically the angle :o) I wouldn't know how to actually return the angle of the triangle though.


octothorpe(Posted 2005) [#13]
The normal wont update until you call updatenormals


You're talking about vertex normals, which are completely different from triangle normals. A triangle normal is a mathematically derived vector describing the angle the triangle is facing. It is calculated from the positions of the triangle's vertices and nothing more.

A vertex normal is a programmatically derived vector describing the angle the vertex is supposed to look like it's facing. Vertex normals are set by hand and can be tweaked. They are simply a property of a vertex and do not even need to be normalized. They're used for lighting, to make a triangle look rounded instead of flatshaded.

Look at the code I posted above and you'll see it isn't using VertexN*() functions, or for that matter, any functions other than Sqr().

Incidently, UpdateNormals() sets all the vertex normals in a mesh to be the average of all the triangle normals the vertex belongs to - or, more accurately, is positioned on the corner of.

also the vertex routine above, how do i work out the angle from it or is that slope


The use of ACos() above turns the vector component into an angle.


D4NM4N(Posted 2005) [#14]
so the angle returned is from the vertical do the dir the tri is facing


D4NM4N(Posted 2005) [#15]
thanks octothorpe, works fine!