Jump to content

How to find ship height above ground (from lowest point)? ~Answered~


Recommended Posts

I'm working on a mod where I'd like to know how much ground clearance the ship has, i.e. height above ground level of the lowest point on the ship in its current orientation.

It's easy to get the height of the control-from-here point, but that's unhelpful for me.

I don't need it to be accurate to the centimeter, so I'm willing to settle for a reasonable approximation if it will significantly simplify the code.

Does anyone have any suggestions for recommended approach?  For example, is there some simple way to get a 3D bounding box for a part, so that I could just iterate through all the parts on the ship to find the lowest extent of a bounding box?  Or is there some simple method I'm missing?

Any advice much appreciated.

Edited by Snark
Link to comment
Share on other sites

On the first thought:

Very cpu-intensive task, be careful.
Part has "collider" public field, Collider class has a box http://docs.unity3d.com/ScriptReference/Collider-bounds.html
Box is in world space, find the closest vertex to the planet center (can be done by http://docs.unity3d.com/ScriptReference/Bounds.ClosestPoint.html with planet position as input).
Use vessel.mainbody.pqscontroller.getSurfaceHeight(), it gives offset of pqs terrain from sea level at passed surface normal vector (can be produced by this.mainBody.GetRelSurfaceNVector() from latitude and longitude of box vertex).
If I was in your pants, i would disable this peace of code on vessel.heightFromTerrain > 200.0f, also I would sort parts by the distance between planet and part.transform every 10-20 physical frames, then have three caches:

  1. previously found closest to terrain part.
  2. 4-5 parts from the start of sorted part list, wich you will check each frame too.
  3. 4-5 random parts from sorted part list, reshuffle them on large angular excitations of vessel.referenceTransform, and each couple of frames. Don't recalculate each frame.
Link to comment
Share on other sites

Excellent, thanks for all the suggestions everyone!  That gives me plenty of avenues to explore.  My problem was simply that I had no idea where to begin.

Excellent point about CPU intensiveness, and I really don't need something super-fancy.  It may be as simple as, for example, caching the lowest point relative to control-from point and then only updating it a couple of times per second.  I've got a pretty low bar, here-- I'm not aiming for "perfect".  All I need is "not laughably bad."

I'll crack the lid on @Diazo's mod, thanks for pointing it out.  He does good stuff and has already been personally helpful to me when I was sorting out a different problem.  Diazo is good people.  :)

(Now if I could just figure out how to track crew assignments in the ship editor...)

Edited by Snark
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...