I’m wanting to calculate the gradient(steepness) of a triangle, given the x,y,z coordinates of each point of the triangle. This is so I can use different materials on the triangle in a terrain according to the terrains slope.
Any copy/paste code around for doing this ? Searches so far have not succeeded
I think what you want is the normal… which is the cross product of two the edges but you also may already have it in the mesh. Not sure what your source data is.
Well, then you have to figure out how you want to turn that into triangles… then take two of the edges and do cross product. You can easily manufacture edges from the grid. x, y, z where y is height and x, z are grid locations.
Not sure the normal is what I’m after. eg if a triangle is lying flat in the x-z plane, the normal will point straight up which implies a non zero gradient which is what a horizontal surface has.
What I’m after is a scalar indicating the rise in height as a fraction of the horizontal distance it covers. eg 1.0 would be a 45 deg slope in 2D.
Calculate the angle between the normal and it’s projection on the ground plane (X-Z or whatever your ground is). For example: create a vector from the X-Z of the normal (setting Y=0) and take the cross-product between the normal and this vector.
As pspeed said, you need the normal.
Slope like that only works in 2D but you have the y value of the vector which is as close as you will get to a “slope”. It’s the sine of the angle… you should be able to produce whatever you want from that with a little trig. (Note: if you use asin() or similar then you are doing it wrong… no reason to bring angles into it.)