【问题标题】:Coding a 3d trajectory prediction system编写一个 3d 轨迹预测系统
【发布时间】:2021-03-16 06:58:15
【问题描述】:

好的,所以几个月来(断断续续)我一直在尝试为我正在制作的 VR 游戏创建一个系统,该系统将根据过去十几次中玩家的手移动位置预测玩家的移动位置左右帧。我完全知道这不会完全准确,但也不必如此。令人恼火的是,到目前为止,无论我尝试了什么,我都无法让它发挥足够的作用。

我一直在研究的想法是获取手在最后几帧中移动的平均方向,然后计算出这些方向的平均变化,然后计算出这些方向的平均变化。然后我根据手的当前位置计算它在接下来的十几个帧中的位置。我得到下一个位置,从当前位置开始,沿最后一个已知方向(当前点和前一个点之间的方向)移动该点,然后采用该方向,根据方向的平均变化旋转它,并且然后,通过这些方向的平均变化来旋转该值。

我还没有让这个系统工作,它开始让我发疯。可悲的是,因为它可能向任何方向移动,我不能只使用标准轨迹计算算法,而且我必须使用四元数,这本质上是令人困惑的。

如果您能帮助我解决这个问题,我将不胜感激。无论是帮助我使用当前的方法还是指出不同的方法。

我当前(不工作的代码)在这里只是为了帮助理解我正在尝试做的事情。

void PredictTrajectory()
{
            Vector3 avgDirection = Vector3.zero;
            List<Vector3> directions = new List<Vector3>();
            Vector3 avgChangeInDirection = Vector3.zero;
            List<Vector3> changeInDirections = new List<Vector3>();
            Vector3 avgChangeInChangeInDirection = Vector3.zero;
            float avgDistanceBetweenPoints = 0;

            for (int index = hand.PastLocations.Length - 1; index > 0; index--)
            {
                avgDirection += (hand.PastLocations[index - 1] - hand.PastLocations[index]).normalized;
                directions.Add((hand.PastLocations[index - 1] - hand.PastLocations[index]).normalized);
                avgDistanceBetweenPoints += Vector3.Distance(hand.PastLocations[index - 1], hand.PastLocations[index]);
            }
            avgDirection.Normalize();
            avgDistanceBetweenPoints /= hand.PastLocations.Length - 1;

            avgDirection = hand.PastLocations[0] - hand.PastLocations[1];
            avgDirection.Normalize();
            
            Quaternion avgDirectionChange = Quaternion.identity;
            List<Quaternion> changesInDirection = new List<Quaternion>();
            for (int index = 0; index < directions.Count - 2; index++)
            {
                changesInDirection.Add(Quaternion.FromToRotation(directions[index], directions[index + 1]));
            }
            avgDirectionChange = changesInDirection.Average();

            Quaternion avgChangeInDirectionChange = Quaternion.identity;
            List<Quaternion> changesInChangeInDirection = new List<Quaternion>();
            for (int index = 0; index < changesInDirection.Count - 2; index++)
            {
                changesInChangeInDirection.Add(changesInDirection[index + 1] * Quaternion.Inverse(changesInDirection[index]));
            }
            avgChangeInDirectionChange = changesInChangeInDirection.Average();
            avgChangeInChangeInDirection.Normalize();

            avgChangeInDirectionChange = Quaternion.FromToRotation(avgChangeInDirection, avgChangeInChangeInDirection);

            List<Vector3> points = new List<Vector3>();
            points.Add(hand.transform.position);
            for (int index = 0; index < 200; index++)
            {
                avgDirection = avgDirectionChange * avgDirection;
                points.Add(points.Last() + (avgDirection * avgDistanceBetweenPoints));
                avgDirectionChange = avgChangeInDirectionChange * avgDirectionChange;
            }

            predictedArc = new Arc(points);
            WorldManager.Instance.DrawPredictionArc(predictedArc);
}

我不经常在这里发帖,所以如果我做错了,我很抱歉。

编辑: 因此,尽管我有文字墙,但实际上我可能并没有以简洁的方式准确地说出我想要什么,所以就在这里。

我需要一个可以半准确地预测以非线性方式移动的对象的未来位置的函数。我需要它给我未来多帧的预测位置(例如:如果我想预测接下来的 15 帧,我需要得到 15 个位置,而不是 1 个)。

【问题讨论】:

    标签: c# unity3d 3d quaternions


    【解决方案1】:

    因此,在时间向前 15 步时,您可以这样做,在最后一帧估计速度和加速度:

    Vector3 lastPos = Vector3.zero;
    Vector3 velocity = Vector3.zero;
    Vector3 lastVelocity = Vector3.zero;
    Vector3 acceleration = Vector3.zero;
    float stepTime = 0.1f;
    int predictionSteps = 15;
    Vector3 predictedPos = Vector3.zero;
    List<Vector3> futurePositions;
    ...
    void Update()
    {
      lastVelocity = velocity;
      velocity = (transform.position - lastPos) / Time.deltaTime;
      lastPos = transform.position;
      acceleration = (velocity - lastVelocity) / Time.deltatime;
      predictedPos = transform.position;
      futurePositions = new List<Vector3>();
      for(int i = 0; i < predictionSteps; i++)
      {
        predictedPos += velocity * stepTime;
        futurePositions.Add(predictedPos);
        velocity += acceleration * stepTime;
      }
    }
    

    【讨论】:

    • 我从那个开始但不够准确,我需要半准确地预测未来几帧的位置。如果我只是在寻找它的下一个位置,这很有效,但是当它更进一步时,当玩家的手没有直线移动时就会产生问题。
    • 如果你想预测非线性运动,你需要考虑加速度,我更新了代码,说明我将如何尝试这样做。
    • 嘿伙计,对不起,我还没有回复,我一直很忙,没有机会尝试代码。我也很抱歉,它仍然不是我需要的。我已经更新了我的问题,希望能更清楚地说明我需要什么。
    • 好的,我现在已经更新了,所以你有一些东西可以预测未来 15 帧中的位置,每个帧都存储在一个列表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2020-08-12
    • 2021-10-01
    • 2018-04-15
    • 2020-05-07
    相关资源
    最近更新 更多