Search Unity

Question How to correctly predict the trajectory of a projectile?

Discussion in 'Physics' started by adrian-taylor09, Oct 22, 2020.

  1. adrian-taylor09

    adrian-taylor09

    Joined:
    Dec 22, 2016
    Posts:
    63
    Hi there,

    I'm trying to accurately predict the trajectory of a tennis ball after being launched at some known velocity. Here is my code for predicting the path:

    Code (CSharp):
    1. public static List<Vector3> GetTrajectoryPoints(Vector3 start, Vector3 velocity, float timestep, float maxTime = 3f)
    2.     {
    3.         List<Vector3> points = new List<Vector3>();
    4.         Vector3 prev = start;
    5.         for (int i = 1; ; i++)
    6.         {
    7.             float time = timestep * i;
    8.             if(time > maxTime) break;
    9.             Vector3 pos = GetProjectilePositionAtTime(start, velocity, time);
    10.             points.Add(pos);
    11.             prev = pos;
    12.         }      
    13.         results.points = points;
    14.  
    15.         return points;
    16.     }
    17.     private static Vector3 GetProjectilePositionAtTime(Vector3 start, Vector3 startVelocity, float time)
    18.     {
    19.         return start + startVelocity * time + Physics.gravity * time * time * 0.5f;
    20.     }
    However, the prediction results consistently overshoot the actual projectile path that Unity physics produces, see images below (blue is the predicted path, red is the actual path)

    The rigid body for the ball has no drag or angular drag Screen Shot 2020-10-22 at 9.33.33 PM.png Screen Shot 2020-10-22 at 9.34.06 PM.png Screen Shot 2020-10-22 at 9.34.59 PM.png
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326