godot 位置 和 角度 的平滑插值

方案1
/// <summary>
/// Returns a transform interpolated between this transform and another
/// <paramref name="transform"/> by a given <paramref name="weight"/>
/// (on the range of 0.0 to 1.0).
/// </summary>
/// <param name="transform">The other transform.</param>
/// <param name="weight">A value on the range of 0.0 to 1.0, representing the amount of interpolation.</param>
/// <returns>The interpolated transform.</returns>
public readonly Transform3D InterpolateWith(Transform3D transform, real_t weight)
{
Vector3 sourceScale = Basis.Scale;
Quaternion sourceRotation = Basis.GetRotationQuaternion();
Vector3 sourceLocation = Origin;

Vector3 destinationScale = transform.Basis.Scale;
Quaternion destinationRotation = transform.Basis.GetRotationQuaternion();
Vector3 destinationLocation = transform.Origin;

var interpolated = new Transform3D();
Quaternion quaternion = sourceRotation.Slerp(destinationRotation, weight).Normalized();
Vector3 scale = sourceScale.Lerp(destinationScale, weight);
interpolated.Basis.SetQuaternionScale(quaternion, scale);
interpolated.Origin = sourceLocation.Lerp(destinationLocation, weight);

return interpolated;
}

方案2 欧拉角的方案(直线,不是球面) ,不使用 Lerp 函数 

public static class AngleExtensions

{

    public static Vector3 Clamp360(Vector3 eulerAngles)

    {

        return new Vector3(gTweens.Source.Extensions.AngleExtensions.Clamp360(eulerAngles.X), gTweens.Source.Extensions.AngleExtensions.Clamp360(eulerAngles.Y), gTweens.Source.Extensions.AngleExtensions.Clamp360(eulerAngles.Z));

    }

 

    public static Vector3 DeltaAngle(Vector3 current, Vector3 target)

    {

        return new Vector3(gTweens.Source.Extensions.AngleExtensions.DeltaAngle(current.X, target.X),gTweens.Source.Extensions.AngleExtensions.DeltaAngle(current.Y, target.Y), gTweens.Source.Extensions.AngleExtensions.DeltaAngle(current.Z, target.Z));

    }

 

    public static Vector3 GetDestinationAngleDegrees(Vector3 origin, Vector3 destination, RotationMode mode)

    {

        switch (mode)

        {

            case RotationMode.ShortestDistance:

                {

                    Vector3 current = Clamp360(origin);

                    Vector3 target = Clamp360(destination);

                    Vector3 vector = DeltaAngle(current, target);

                    return origin + vector;

                }

            default:

                return destination;

        }

    }

 

    public static Vector3 GetDestinationAngleRadiants(Vector3 origin, Vector3 destination, RotationMode mode)

    {

        return GetDestinationAngleDegrees(origin * 57.29578f, destination * 57.29578f, mode) * 0.01745329f;

    }

}

 

posted @ 2025-08-19 16:30  porter_代码工作者  阅读(10)  评论(0)    收藏  举报