Jump to content

How do I use a "PQS" collider to find terrain height at lat/long on body?


Recommended Posts

I see hints that the thing called the "PQS" is what you use to get the terrain height for a body when the body is too far away for its terrain polygons to be fully loaded into Unity. (That seems to be roughly near 30-40 km altitude on Kerbin).

Can someone point me to the right way to use it?

Say for example, I have the following givens:

a CelestialBody called 'b'.

A latitude 'lat'.

A longitude 'long'.

Given B, lat, and long, how do I find the altitude of terrain at that position of the body?

Link to comment
Share on other sites

You need:


Vector3d rad = new Vector3d (Math.Cos (lat) * Math.Cos (lon) , Math.Sin (lat) , Math.Cos (lat) * Math.Sin (lon));
double elevation = b.pqsController.GetSurfaceHeight (rad) - b.pqsController.radius;

The lat and long must be converted to radians. pqsController.GetSurfaceHeight returns the elevation above the center of the planet, hence the radius subtraction.

You can see the full method here: https://github.com/S-C-A-N/SCANsat/blob/master/SCANdata.cs#L68-L76

Quite a few mods use essentially the same method.

Link to comment
Share on other sites

You need:


Vector3d rad = new Vector3d (Math.Cos (lat) * Math.Cos (lon) , Math.Sin (lat) , Math.Cos (lat) * Math.Sin (lon));
double elevation = b.pqsController.GetSurfaceHeight (rad) - b.pqsController.radius;

Ah, so basically pqscontroller.GetSurfaceHeight(v) gets the height above sea level at the position that vector v's ray intersects the surface, assuming the vector's tail is origined at the body's center?

The reason I ask is that getting the lat/long is actually quite indirect, while working in the world of unity 3d World coords is actually more direct and involves fewer conversions, given where I'm starting from. I was assuming I'd have to do the work to get it into lat/long terms. If I don't have to do that and can live entirely in the world of 3D coords that's great.

Link to comment
Share on other sites

Ah, so basically pqscontroller.GetSurfaceHeight(v) gets the height above sea level at the position that vector v's ray intersects the surface, assuming the vector's tail is origined at the body's center?

The reason I ask is that getting the lat/long is actually quite indirect, while working in the world of unity 3d World coords is actually more direct and involves fewer conversions, given where I'm starting from. I was assuming I'd have to do the work to get it into lat/long terms. If I don't have to do that and can live entirely in the world of 3D coords that's great.

I've never actually tried it using a world position instead of lat/long, but I assume it should work assuming you calculate the correct vector. CelestialBody has a lot of position vectors, one of which probably works.

Also note that it doesn't return height above sea level, merely height above the center of the planet. CelestialBody.pqsController.radius should give you the height of sea level (or "sea level") above the center of the planet, so you can use that to figure out the actual terrain elevation.

Link to comment
Share on other sites

Has anyone tried using RayIntersection() ? It looks like it's almost exactly what I need, but it doesn't actually seem to work like I'd expect.

You call it like this, apparently:

body.pqsController.RayIntersection( origin, rayVec, out hitDist )

But the vectors origin and rayVec, when expressed in Unity World coords, give the exact same false hits that I'm getting from Physics.RayCast, and I verified that they are where I think they are by drawing a line with LineRenderer to see if the coords are what I think they are.... they are.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...