[英]Coding a 3d trajectory prediction system
好吧,所以幾個月來(斷斷續續)我一直在嘗試為我正在制作的 VR 游戲創建一個系統,該系統將根據過去十幾個幀中的位置預測玩家移動手的位置. 我完全知道這不會完全准確,但也不必如此。 令人惱火的是,到目前為止,無論我嘗試了什么,我都無法讓它發揮足夠的作用。
我一直在努力的想法是獲取手在最后幾幀中移動的平均方向,然后計算出這些方向的平均變化,然后計算出這些方向的平均變化。 然后我根據手的當前 position 計算它在接下來的十幾幀中的位置。 我得到下一個 position,從當前的 position 開始,沿最后一個已知方向(當前點和前一個點之間的方向)移動該點,然后采用該方向,根據方向的平均變化旋轉它,並且然后,通過這些方向的平均變化來旋轉該值。
我還沒有讓這個系統工作,它開始讓我發瘋。 可悲的是,因為它可能向任何方向移動,我不能只使用標准的軌跡計算算法,而且我必須使用四元數,這本質上是令人困惑的。
如果您能夠幫助我解決這個問題,我將不勝感激。 無論是幫助我使用當前的方法還是指出不同的方法。
我當前的(不工作的代碼)在這里只是為了幫助理解我正在嘗試做的事情。
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);
}
我不經常在這里發帖,所以如果我做錯了,我道歉。
編輯:所以盡管我的文字牆,我實際上可能並沒有以簡潔的方式准確地說出我想要的東西,所以就在這里。
我需要一個 function 可以半准確地預測 object 以非線性方式移動的未來位置。 我需要它來給我未來多幀的預測位置(例如:如果我想預測接下來的 15 幀,我需要得到 15 個位置,而不是 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.