SDK Question Computing a heading to a base from current position

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
670
Reaction score
89
Points
43
Location
Happy Wherever
Can anyone help with this for an autopilot?

Computing a heading to a base from current position.

Searching drew a blank except for this deep examination.

And this:
Code:
Course between points

We obtain the initial course, tc1, (at point 1) from point 1 to point 2 by the following. The formula fails if the initial point is a pole. We can special case this with:

IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
  IF (lat1 > 0)
     tc1= pi        //  starting from N pole
  ELSE
     tc1= 2*pi         //  starting from S pole
  ENDIF
ENDIF
For starting points other than the poles:

IF sin(lon2-lon1)<0       
   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))    
ELSE       
   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))    
ENDIF 
An alternative formula, not requiring the pre-computation of d, the distance between the points, is:

   tc1=mod(atan2(sin(lon1-lon2)*cos(lat2),
           cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon1-lon2)), 2*pi)
but can't get it to work.

There must be a simple formula, no?:thumbup:
 
I once wrote an AP lua script to fly to KSC and land there. Maybe I'll add some comments later...

Code:
function getHeading (target_lon, target_lat)
	term.out('#003')
    equ = oapi.get_equpos()
	target_lon_b = -80.707265*RAD
	target_lat_b = 28.634564*RAD
	
	londif = target_lon - equ.lng
	latdif = target_lat - equ.lat
	
	londif_b = target_lon_b - equ.lng
	latdif_b = target_lat_b - equ.lat
	
	alph = math.atan(latdif/londif)
	
	heading = 180*RAD-alph-90*RAD
	
	Om = math.atan2(math.sin(londif)*math.cos(target_lat),math.cos(equ.lat)*math.sin(target_lat)-math.sin(equ.lat)*math.cos(target_lat)*math.cos(londif))
	
	var_a = math.sin(latdif_b/2) * math.sin(latdif_b/2) + math.sin(londif_b/2) * math.sin(londif_b/2) * math.cos(equ.lat) * math.cos(target_lat_b)
	var_c = 2*math.atan2(math.sqrt(var_a),math.sqrt(1-var_a))
	distance = 6371 * var_c
	
	
	if equ.lng > target_lon then
		heading = heading + 180*RAD
	end
	
	if equ.lng > target_lon then
		Om = 360*RAD + Om
	end
	
	sethead = 330*RAD-Om
	
	setHeading(330*RAD-sethead*2)
end

I wanted to fly the ship to KSC at an heading of 330 (to line up with the runway), so ignore the last lines (-- is used for comments in lua). Shouldn't be that hard to get it into C++.

EDIT: Om is the heading from shipt to target.
 
Last edited:
:hesaid:

That's what I used.
For atmospheric flight, rotation can be ignored.
 
I did some of this about 6 years ago for a Navy training sim. Take a look at the Haversine function and do some google searches on that and 'heading'.

Also, a more accurate (and probably far too accurate) method was created by a dude in the 70s. Google 'Vincenty 1975' (his name and the date of the paper) if you want to go that far.
 
Back
Top