Jump to content

Figuring out angle using latitude and longitude + height


Recommended Posts

2 objects a vessel and point on ground. We do I have both long and latitude.

And height assuming ground point is 0 and height is current vessel altitude.

How do I figure out the math for angle between the two objects? This is a line of site type equation. I'm guessing that with a keostationary orbit a good line of site is 45 degrees.

Looked at the old finePrint example for stationary satellite point but it's wrong. (Assuming he fixed it when it went stock but I can't see the example)

The ground point is not an object just random point on ground using longitude and latitude.

Thanks thus has stopped me cold in my update since complicated math is not my thing lol.

Link to comment
Share on other sites

What angle do you need? The angle between the ground at the point, and the line to the hip? The angle at the center of the planet between the point and the ship?

You want to be Googling for Spherical Coordinates. It's heady enough stuff that I don't want to figure it out, but with a bit of perseverance there's nothing that should stop you.

Link to comment
Share on other sites

Take two vectors, v and w.

Let v = |v| be the magnitude of v: v = sqrt(vx2 + vy2 + vz2).

The magnitude of the cross product is: |v x w| = v w sin(θ), where θ is the angle between v and w.

First break v and w into cartesian from spherical coordinates. Calculate:

vx = v cos(lat) cos(lon)

vy = v cos(lat) sin(lon)

vz = v sin(lat)

Same for w, but use the latitude and longitude to w.

For a point on the surface of Kerbin, for example, v = 600000 m + altitude of the ground above sea level.

For a point above a planet, w = (radius of planet) + altitude.

Now I'm going to assume that instead of angle θ, the angle between v and w, you want the angle between the zenith (directly overhead) of your ground location and whatever you have in orbit. I

For that we need the vector pointing from the ground to the thing in orbit: u = w - v.

So calculate: ux = wx - vx, uy = wy - vy, uz = wz - vz, and u = sqrt(ux2 + uy2 + uz2)

So the angle ̉ۢ between the zenith and the orbiter is obtained from the cross product of v and u: sin(̉ۢ) = |v x u| / (v u).

All that is missing how to calculate the magnitude of the cross product:

|v x u| = sqrt( (vy uz - vz uy)2 + (vz ux - vx uz)2 + (vx uy - vy ux)2 )

Now just take the inverse sine: ̉ۢ = asin(|v x u| / (v u)).

Questions?

Edited by Yasmy
Mixed up latitude and co-latitude. Silly conventions.
Link to comment
Share on other sites

What angle do you need? The angle between the ground at the point, and the line to the hip? The angle at the center of the planet between the point and the ship?

You want to be Googling for Spherical Coordinates. It's heady enough stuff that I don't want to figure it out, but with a bit of perseverance there's nothing that should stop you.

Angle between the ship and point on ground. Basically the same thing finePrint did with the keep station in line of sight contract that we have now. The only thing is the version of fineprint on github this contract doesn't work. Works now in game but don't have acess to that code.

This is what he used in finePrint old version


double anglediff = 180 - Math.Abs(Math.Abs(v.longitude - longitude) - 180);
bool longitudeMatch = (anglediff <= 45);

It doesn't work very well 😬 all math.abs does is make negative numbers positive.

- - - Updated - - -

Take two vectors, v and w.

Let v = |v| be the magnitude of v: v = sqrt(vx2 + vy2 + vz2).

The magnitude of the cross product is: |v x w| = v w sin(θ), where θ is the angle between v and w.

First break v and w into cartesian from spherical coordinates. Calculate:

vx = v cos(lat) cos(lon)

vy = v cos(lat) sin(lon)

vz = v sin(lat)

Same for w, but use the latitude and longitude to w.

For a point on the surface of Kerbin, for example, v = 600000 m + altitude of the ground above sea level.

For a point above a planet, w = (radius of planet) + altitude.

Now I'm going to assume that instead of angle θ, the angle between v and w, you want the angle between the zenith (directly overhead) of your ground location and whatever you have in orbit. I

For that we need the vector pointing from the ground to the thing in orbit: u = w - v.

So calculate: ux = wx - vx, uy = wy - vy, uz = wz - vz, and u = sqrt(ux2 + uy2 + uz2)

So the angle ̉ۢ between the zenith and the orbiter is obtained from the cross product of v and u: sin(̉ۢ) = |v x u| / (v u).

All that is missing how to calculate the magnitude of the cross product:

|v x u| = sqrt( (vy uz - vz uy)2 + (vz ux - vx uz)2 + (vx uy - vy ux)2 )

Now just take the inverse sine: ̉ۢ = asin(|v x u| / (v u)).

Questions?

My head exploded but I'll try putting this into code. Lol.

First let me get my scientific calculator out.

Link to comment
Share on other sites

Couldn't you just turn your latitude, longitude and altitude into world x,y,z coordinates, get the appropriate vectors, and use Unity's built in Vector3.angle?

I don't know never used unity vector3.angle.

I can look it up thanks for pointing it out.

Link to comment
Share on other sites

Vector3 groundUpVec = (groundPoint - vessel.mainBody.position); // ground normal at ground station
Vector3 groundToVesselVec = (groundPoint - vessel.rootPart.transform.position); // direction of vessel from ground station
float angle = Vector3.Angle(groundUpVec, groundToVesselVec); // angle between ground normal and vessel direction

If it says ~180, switch the order of subtraction on one of the vectors around

Link to comment
Share on other sites

Vector3 groundUpVec = (groundPoint - vessel.mainBody.position); // ground normal at ground station
Vector3 groundToVesselVec = (groundPoint - vessel.rootPart.transform.position); // direction of vessel from ground station
float angle = Vector3.Angle(groundUpVec, groundToVesselVec); // angle between ground normal and vessel direction

If it says ~180, switch the order of subtraction on one of the vectors around

Thanks for this I guess it's time I finally learned about vectors with the math yasmy showed me and everything else posted should not have issues creating new vectors with longitude and lat as references. For vessel glad it's already done for me. :)

I will post my results and code I used when I have it done, and tested.. I hope.

Edited by malkuth
Link to comment
Share on other sites

So I got this to work but something I did not think about. Have to work out at first I thought I messed up still might have. But it seems that the angle is recorded even if Vessel is behind the object (IE kerbin is between the vessel and the station) and in front of it.

I take it this is because the vector3.angle converts all negatives to positives?

Welp its pretty obvious that the angle thing is not going to work for figuring out Line Of sight since angles are recorded from all areas...

Guess Check out remote tech to see how they do the Line Of sight with kerbin KSC.

This is the code by the way.. Does work for angles not just what I had in mind.


private float CheckVectorAngle2Objects(Vessel v, double LongT,double latT,double ratT)
{
double targetX = ratT * Math.Cos(latT) * Math.Cos(LongT);
double targetY = ratT * Math.Cos(latT) * Math.Sin(LongT);
double targetZ = ratT * Math.Sin(latT);
Vector3 groundStation = new Vector3((float)targetX, (float)targetY, (float)targetZ);
Vector3 groundStationUpVec = (groundStation - v.mainBody.position);
Vector3 groundToVesselVec = (groundStation - v.rootPart.transform.position);
float angle = Vector3.Angle(groundStationUpVec, groundToVesselVec);
[B]Vector3 cross = Vector3.Cross(groundStationUpVec, groundToVesselVec);
if (cross.y < 0) angle = -angle;[/B] //this by the way is how I fixed the negative issue.
Debug.Log("vessel info: " + v.mainBody.position.ToString() + " GroundStation: " + groundStation.ToString() + "Angle: " + angle);
return angle;
}

Maybe I can do it via distance from groundstation to vessel? Or maybe I just need to find out how to do the whole if vessel is occluded by body thing.

Edited by malkuth
Link to comment
Share on other sites

Vector3.Angle is 0-180 degrees (with 0 being up in theory), but if all you need is a 45 degree cone pointing up from the ground station it should work just fine. Is it returning the same angle for a vessel overhead as one behind the planet (that would be odd...)?

Edited by Crzyrndm
Link to comment
Share on other sites

Vector3.Angle is 0-180 degrees, but if all you need is a 45 degree cone pointing up from the ground station it should work just fine. Is it returning the same angle for a vessel overhead as one behind the planet (that would be odd...)?

Unless the math is wrong its no where close to what its suppose to be. I'm at KeoStationary orbit (little less to test out how much it moves) and the angle is still moving around like its not almost stationary.

by the way this is how I'm using the code above in game to check for Angle. been thinking about adding an ingame representation of groundStation to make sure the math is right.


float GroundToVesselAngle = CheckVectorAngle2Objects(FlightGlobals.ActiveVessel,longitude,latitude,targetBody.Radius);
bool AngleCheck = (GroundToVesselAngle <= 45 && GroundToVesselAngle >= 0);

Edited by malkuth
Link to comment
Share on other sites

KSP has a method for getting world position from lat/long, that would drop one variable out.

v.mainBody.GetWorldSurfacePosition(lat,long,alt);

Other than that, I'm out of ideas for now

Dam I will try that. you can do it with anything I just found out so I I can figure out how to transform that into a vector I might be pretty golden.

Edited by malkuth
Link to comment
Share on other sites

That actually worked neat. Still have to work out numbers because they seem backwards.. But on the right track.

Nope don't work the angle just gets all crazy when the vessel is behind the Tracking station. Bummer. Back to the drawing board.

Edited by malkuth
Link to comment
Share on other sites

Backwards how? If its saying an overhead vessel is at 180 degrees, switch one of the vector subtractions around (ie. reverse the direction)

hard to explain whats happing. But the angle goes from 0 to 180 like its suppose to. when the vessel gets behind the object for its no longer in site in no longer goes from 0 to 180 but 0 to about 90 then goes back to 0 and rinse in repeat..

I can keep messing with it but doesn't seem like its going to work.

New code by the way.


private float CheckVectorAngle2Objects(Vessel v, double LongT,double latT)
{
Vector3 groundStation = new Vector3();
groundStation = Planetarium.fetch.Home.GetWorldSurfacePosition(latT, LongT, 0);
Vector3 groundStationUpVec = (groundStation - v.mainBody.position);
Vector3 groundToVesselVec = (v.rootPart.transform.position - groundStation);
float angle = Vector3.Angle(groundToVesselVec, groundStationUpVec);
Debug.Log("Angle: " + angle);
return angle;
}
}

- - - Updated - - -

Think your right the values are reversed. But I think I can make this work now. Thanks for the help.

- - - Updated - - -

And to show you the success of making this work. :) Woohoo. Final code been fixed above for current values im using.

rpXGWNY.png

- - - Updated - - -

And finally the whole package works.. Now for in game play testing and I should be able to release the newest version of Mission Controller with its new contracts. :)

cvmuIxb.png

Edited by malkuth
Link to comment
Share on other sites

Glad it appears to be working. As a simple test, try putting a vessel at altitude 600000 m, and behind the ground station by 60 degrees longitude.

This makes a 30-60-right triangle, and the angle reported should be 90 degrees. I.e., the satellite should be on the horizon.

Then wait until the satellite is behind the ground station by 90 degrees latitude. It should have dropped below the horizon. This makes a 1-2-sqrt(5) triangle,

and the angle reported should be (180-asin(2/sqrt(5)) = 116.6 degrees.

It's always good to check your code with simple test cases you can do by hand.

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...