using System.Collections; using System.Collections.Generic; using UnityEngine; namespace YKMoon.Math { /// /// 星球相关 /// public partial class YKMath { /// /// 纬度类型 /// public enum LatitudeType { S, N } /// /// 经度类型 /// public enum LogitudeType { E, W, } public class CoordinateInfo { /// /// 纬度,弧度 /// public float latitudeValue; /// /// 纬度类型,北纬南纬 /// public LatitudeType latitude { get { if (latitudeValue < 0) { return LatitudeType.S; } else { return LatitudeType.N; } } } /// /// 经度,弧度 /// public float logitudeValue; /// /// 经度类型,东经西经 /// public LogitudeType logitude { get { if (logitudeValue<0) { return LogitudeType.W; } else { return LogitudeType.E; } } } } /// /// 计算经纬度,减少GC用ref /// /// 经纬度信息 /// 星球中心 /// 星球表面点 public static void CalculateEarthCoordinate(ref CoordinateInfo result, Transform planetTransform, Vector3 surfacePos) { float radius = Vector3.Distance(planetTransform.position, surfacePos); var delta = planetTransform.InverseTransformPoint(surfacePos); result.latitudeValue = Mathf.Asin(delta.y / radius) * Mathf.Rad2Deg; result.logitudeValue = Mathf.Atan2(delta.z, delta.x) * Mathf.Rad2Deg; } /// /// 计算经纬度 /// /// 星球中心 /// 星球表面点 /// 经纬度信息 public static CoordinateInfo CalculateEarthCoordinate(Transform planetTransform, Vector3 surfacePos) { CoordinateInfo result = new CoordinateInfo(); CalculateEarthCoordinate(ref result, planetTransform, surfacePos); return result; } /// /// 经纬度转球面坐标 /// /// 半径 /// 纬度 /// 经度 /// 球面坐标 public static Vector3 GetSurfacePosInEarth(float radius, float latitude, float logitude) { Vector3 delta = Vector3.zero; delta.y = Mathf.Sin(latitude / Mathf.Rad2Deg) * radius; var angle = logitude / Mathf.Rad2Deg; delta.z = Mathf.Sin(angle) * radius; delta.x = Mathf.Cos(angle) * radius; return delta; } } }