围绕另一个任意点旋转任意点的问题

5
我编写了以下方法,用于将任意点绕另一个任意点旋转一定角度,并在一段时间内完成旋转。该点现在运动不规律,但最终到达了我认为是期望位置的附近。我需要让它平滑地移动到目标角度。
请注意,这些点与游戏对象无关。
从下面的图中可以看出,我试图将贝塞尔曲线的一个点(使用LineRenderer绘制)绕另一个点旋转一定角度,并在一定时间内完成旋转。这些点中没有一个与包含贝塞尔曲线的游戏对象的位置重合。

enter image description here

IEnumerator runMovement()  {

    yield return new WaitForSeconds(2.0f);
    Vector2 pivot = points[3];

    StartCoroutine(RotateControlPointWithDuration(pivot, 2.0f, 90.0f));

}

   IEnumerator RotateControlPointWithDuration(Vector2 pivot, float duration, float angle)
{
    float currentTime = 0.0f;
    float ourTimeDelta = 0;
    Vector2 startPos = points[0];

    ourTimeDelta = Time.deltaTime;
    float angleDelta = angle / duration; //how many degress to rotate in one second

    while (currentTime < duration)
    {
        currentTime += Time.deltaTime;
        ourTimeDelta = Time.deltaTime;

        points[0] = new Vector2(Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) - Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.x,
                                                        Mathf.Sin(angleDelta * ourTimeDelta) * (startPos.x - pivot.x) + Mathf.Cos(angleDelta * ourTimeDelta) * (startPos.y - pivot.y) + pivot.y);

        yield return null;
    }
}

我建议您添加动画gif或链接到一个视频,展示您当前的问题。此外,最好再添加另一个视频,展示您具体要做什么。 - Programmer
1
此外,要听从 @程序员 说的一切 :) - Fattie
四元数与你所想和处理的内容根本没有任何联系,无论从任何方面来看都毫无关联,它们甚至都没有模糊的联系。 - Fattie
2
@Fattie仍然存在一些不稳定的运动。我刚刚更新了问题。我真诚地认为这个问题有许多动态因素,使它与您提供的链接不同。 - TenOutOfTen
@Fattie 我编辑了问题。我正在尝试使用您的分数建议,但不太确定如何使其工作。如果能提供更多信息或可能的答案,将不胜感激 :) - TenOutOfTen
显示剩余6条评论
2个回答

1
您想要的模式只是:


public IEnumerator HowToSmoothly()
    {
    // move "X" from value "A" to value "B"

    float duration = 2.5f;
    float delta = B - A;

    float startTime = Time.time;
    float finishTime = Time.time+duration;

    while(Time.time<finishTime)
        {

        float soFarTime = Time.time-startTime;
        float fractionThisFrame = soFarTime / duration;
        float valueThisFrame = A + delta * fractionThisFrame;

        X = valueThisFrame
        if (X > B) X = B;

        yield return 0;
        }

    X = B;
    yield break;
    }

0

我可以在四元数方面真正帮助你,但我可以提供一些关于随时间移动的建议。我曾经学到的一个艰苦的教训是尽可能避免使用协同程序。虽然它们看起来很不错,但是要正确使用它们有点困惑,并且Unity使用它们的方式并不完全符合C#其他领域中使用它们的方式。

尽可能地,我只使用每帧更新的Unity类。这样更容易理解和调试。在您的情况下,缺点是您需要为每个要旋转的点添加一个新组件,但这不应该是什么大问题。

public class PointRotator : MonoBehaviour

{

bool rotating = false;
float rate = 0;
float angle;

Vector3 point;
Vector3 pivot;

public void Rotate(Vector3 point, Vector3 pivot, float duration, float angle)
{
    this.point = point;
    this.pivot = pivot;
    this.rate = angle/duration;
    this.angle = angle;

    rotating = true;
}

public void Update()
{
    if (rotating)
    {
        // use quartonian.Lerp with Time.deltatime here

    //if(angle > quartonian angle){rotating = false)
    }
}

}

尝试这样做,看看你的问题是否解决。否则,您可能需要更深入地研究四元数,或者因为您在二维平面上,所以只能手动进行三角计算。


我将计时多个点的旋转,它们可能会达到数百个,目前我无法确定。这也是我想使用协程的另一个原因。 - TenOutOfTen
@TenOutOfTen,除非您用一两句话解释您想要做什么,否则我们无法提供更多帮助。祝好! - Fattie
你考虑过将点变成游戏对象吗?这会使解决方案更简单。 - pseudoabdul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接