Search Unity

Predict object position

Discussion in 'Scripting' started by JonRurka, Nov 19, 2016.

  1. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    The intention of this code segment is to predict the location of an object based on it's forward velocity and angular velocity. However, the position is not showing where it's supposed to be; first when going straight the predicted position is shown as the same as current position, and second when turning the position is almost correct, but with a larger turning radius than expected (as shown in the image).

    Code (csharp):
    1.  
    2.                 Quaternion currentQuat = Quaternion.AngleAxis(syncEndRotation.eulerAngles.y, Vector3.up);
    3.                 Vector3 currentDir = currentQuat * Vector3.forward;
    4.  
    5.                 float predictedYrot = syncEndRotation.eulerAngles.y + ((AngularVel) * (averagePing));
    6.                 Quaternion predictedQuat = Quaternion.AngleAxis(predictedYrot, Vector3.up);
    7.                 Vector3 predictedDir = predictedQuat * Vector3.forward;
    8.  
    9.                 float radius = Vel / AngularVel;
    10.                 float angle = AngleDiff(predictedQuat.eulerAngles.y, syncEndRotation.eulerAngles.y);
    11.                 float predictedDist = (radius * Mathf.Sin(angle / 2f)) / 2;
    12.  
    13.                 //Quaternion RadiusDirQuat = Quaternion.AngleAxis(syncEndRotation.eulerAngles.y + 90, Vector3.up);
    14.                 //Vector3 radiusDir = RadiusDirQuat * Vector3.forward;
    15.                 //Debug.DrawRay(syncEndPosition + Vector3.up, radiusDir * radius, new Color(1, 0, 0.5f, 1)); // turning radius
    16.  
    17.                 //Vector3 predictedPos = syncEndPosition + Direction * Vel * (averagePing * 10); <-- old
    18.                 Vector3 predictedPos = syncEndPosition + (predictedDir * predictedDist);
    19.  
    20.                 Debug.DrawRay(syncEndPosition + Vector3.up, Vector3.down, Color.green); // current position
    21.                 Debug.DrawRay(syncEndPosition + Vector3.up, currentDir, Color.gray); // current direction
    22.                 Debug.DrawRay(syncEndPosition + Vector3.up, predictedDir * predictedDist, Color.yellow); // predicted direction
    23.                 Debug.DrawRay(predictedPos + Vector3.up, Vector3.down, Color.blue, 1f); // predicted position
    24.  
    25.     private float AngleDiff(float a, float b) {
    26.         float angle = a - b;
    27.         if (angle > 180)
    28.             angle -= 360;
    29.         if (angle < -180)
    30.             angle += 360;
    31.         return angle;
    32.     }
    33.  
     
    Last edited: Nov 19, 2016