假设球体位于位置 [0,0](目前),我使用这种方法来计算球体表面上具有一定半径的点,给定纬度/经度坐标(Vector2 x/y):pub...
假设球体位于位置 [0,0](目前),我使用这种方法根据纬度/经度坐标(Vector2 x/y)计算球体表面上具有一定半径的点:
public static Vector3 GetPointOnSphere(Vector2 coo, float radius)
{
float lati = coo.x * Mathf.Deg2Rad;
float longi = coo.y * Mathf.Deg2Rad;
float x = radius * Mathf.Cos(lati) * Mathf.Cos(longi);
float y = radius * Mathf.Cos(lati) * Mathf.Sin(longi);
float z = radius * Mathf.Sin(lati);
return new Vector3(x, y, z);
}
将其翻译如下:
public static Vector2 GetCoords(Vector3 point, float radius)
{
float lati = Mathf.Asin(point.z / radius) * Mathf.Rad2Deg;
float longi = Mathf.Atan2(point.y, point.x) * Mathf.Rad2Deg;
return new Vector2(lati, longi);
}
我通过相机的光线投射获取表面点,并使用它将球体周围的物体移动到目标坐标:
bool MoveTowardsPoint(Vector2 target, float stop_dist)
{
Vector2 curr = GetCoords();
if (CompareVector2(curr, target, stop_dist))
{
return true;
}
double angularDistance = Math.Acos(Math.Sin(curr.x * Math.PI / 180) * Math.Sin(target.x * Math.PI / 180) +
Math.Cos(curr.x * Math.PI / 180) * Math.Cos(target.x * Math.PI / 180) *
Math.Cos(Math.Abs(curr.y - target.y) * Math.PI / 180)) + 1e-6;
double fr = MoveSpeed * Time.fixedDeltaTime / angularDistance;
Vector2 coords = new Vector2(LerpAngle(curr.x, target.x, fr),
LerpAngle(curr.y, target.y, fr));
transform.position = GetPointOnSphere(coords);
return false;
}
它在球体的某些部分起作用,但我认为在“水平极点”附近,当“fr”增长到无穷大时(尝试过将其限制,但并没有解决核心问题),目标永远无法达到,看起来棋子被迫远离这些极点。该区域相当大,位于地球的两侧。我该怎么办?
MoveTowardsPoint 中使用的方法:
float LerpAngle(double a, double b, double t)
{
double delta = Repeat((b - a), 360);
if (delta > 180)
delta -= 360;
return (float)(a + delta * Clamp01(t));
}
double Repeat(double t, double length)
{
return Clamp(t - Math.Floor(t / length) * length, 0.0f, length);
}
double Clamp(double value, double min, double max)
{
if (value < min) return min;
if (value > max) return max;
return value;
}
double Clamp01(double value)
{
if (value < 0.0) return 0.0;
if (value > 1.0) return 1.0;
return value;
}