extends Node

# spos.x = lat, spos.y=lon, spos.z=height
func latLonToGlobal(spos: Vector3, offset:=Vector3(), origin:=Vector3()):
	var height = spos.z + offset.z
	var latPos = spos.x
	var lonPos = spos.y
	var lat = float(latPos+offset.x)*PI/180
	var w = cos(lat)
	var y = sin(lat)
	var lon = float(lonPos+offset.y)*PI/180
	var x = sin(lon)
	var z = cos(lon)
	return Vector3(x * height * w, y*height, z * height * w)-origin
			
func globalToLatLon(pos: Vector3, offset=Vector3()):
	var height=pos.length()
	height = height + offset.z
	var lon = 0
	if pos.z != 0: #and abs(x/z) < 1:
		lon = atan(pos.x/pos.z)*180/PI
	var lat = sign(pos.y)*90
	if abs(pos.y) < height:
		lat = asin(pos.y/height)*180/PI
	#print("x:"+str(x)+" y:"+str(y)+" z:"+str(z)))
	#print("lat:"+str(lat)+" lon:"+str(lon))
	return Vector3(lat, lon, height)
	
func relativeToAbsolute(rel = float()):
	var absolute = float()
	absolute = rel+180-(sign(rel)*180)
	return absolute

#func globalToRad(pos: Vector3):
#	var floor_normal = pos - Vector3()
#	floor_normal = floor_normal.normalized()
#	floor_normal.basis.
