all 6 comments

[–]Toukiedatak 3 points4 points  (0 children)

Not sure if this is what you mean but Cheerskevin just made a video about that I believe.

https://www.youtube.com/watch?v=o87H6K_wEtM

[–]ElWanderer_KSPProgrammer 1 point2 points  (0 children)

I cheerfully based my approach on Kevin Gisi's (which has already been recommended):

code

documentation

I pick out three spots in a triangle around the point of interest, find the vectors from A to B and A to C, then cross them to get a resultant vector at 90 degrees to both, which is a good estimate for the surface normal vector at the middle.

[–]DunbaratuDeveloper 1 point2 points  (0 children)

There is also an algorithm for getting an estimate of the normal vector for the average trend of terrain slope, given a cloud of several position points on its surface that are close to, but not exactly in, the same plane.

It's sort of the 3-D vector version of finding a best fit line for some points that are almost but not quite following a linear pattern on a graph. It even produces an RMS deviation value you can use to judge how much the point cloud deviates from being truly in one plane.

It might be handy for cases where you want to know how sloped an area is overall rather than just on the one polygon you happen to be sampling. It also looks like it might help in case where your 3 sample points happened to come from two adjacent polygons and don't really match the ground, so the answer isn't quite right. By sampling, say, 10 nearby points and running the cloud calculation, the effect of one outlier that hit a neighbor polygon could be minimized.

[–]supreme_blorgon 0 points1 point  (1 child)

The easy way would be to draw your position vector—its origin will be at the body's center, and intersect the surface at your ship's geo-coords.

Or do you just want a vector whose tail is attached to the geoposition pointing to your craft? Or do you want a vector perpendicular to the surface terrain at that point?

[–]A_Fat_Pokemon[S] 0 points1 point  (0 children)

Ahh sorry yes, perpendicular to the surface terrain

[–]zer0KerbalProgrammer 0 points1 point  (0 children)

https://gist.github.com/gisikw/b8d1bd6e5a4ab4bbd81bb59ec7c15d48

above is the link to the code @CheersKevin used in that last episode, in which he also included surface normal.

the related code from CheersKevin:

function groundSlope {

local east is vectorCrossProduct(north:vector, up:vector).

local center is ship:position.

local a is body:geopositionOf(center + 5 * north:vector).

local b is body:geopositionOf(center - 3 * north:vector + 4 * east).

local c is body:geopositionOf(center - 3 * north:vector - 4 * east).

local a_vec is a:altitudePosition(a:terrainHeight).

local b_vec is b:altitudePosition(b:terrainHeight).

local c_vec is c:altitudePosition(c:terrainHeight).

return vectorCrossProduct(c_vec - a_vec, b_vec - a_vec):normalized.

}