预测对象位置

时间:2016-11-19 00:27:57

标签: c# math unity3d

此代码段的目的是根据对象的前向速度和角速度来预测对象的位置。但是,这个位置并没有显示它应该在哪里;第一次直行时,预测位置显示为与当前位置相同,第二次向右转时位置几乎正确,但转弯半径大于预期(如图所示)。

syncEndPosition = m_Rigidbody.position;
syncEndRotation = m_Rigidbody.rotation;
AngularVel = m_Rigidbody.angularVelocity.y; // radians per second
Vel = transform.InverseTransformDirection(m_Rigidbody.velocity).z;
averagePing = 0.5f;

Quaternion currentQuat = Quaternion.AngleAxis(syncEndRotation.eulerAngles.y, Vector3.up);
Vector3 currentDir = currentQuat * Vector3.forward;

float predictedYrot = syncEndRotation.eulerAngles.y + ((AngularVel) * (averagePing * 10));
Quaternion predictedQuat = Quaternion.AngleAxis(predictedYrot, Vector3.up);
Vector3 predictedDir = predictedQuat * Vector3.forward;

float radius = Vel / AngularVel;
float angle = AngleDiff(syncEndRotation.eulerAngles.y, predictedQuat.eulerAngles.y);
float predictedDist = (radius * Mathf.Sin(angle / 2f));

Vector3 predictedPos = syncEndPosition + (predictedDir * predictedDist);

Debug.DrawRay(syncEndPosition + Vector3.up, Vector3.down, Color.green); // current position
Debug.DrawRay(syncEndPosition + Vector3.up, currentDir, Color.gray); // current direction
Debug.DrawRay(syncEndPosition + Vector3.up, predictedDir * predictedDist, Color.yellow); // predicted direction

predict position test

0 个答案:

没有答案