Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Projectile delta time trajectory offset

Discussion in 'Physics' started by Qriva, Dec 12, 2022.

  1. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    I need someone to explain to me why the following is happening:
    There is projectile launched along the arc, calculated with this piece of code:
    Code (CSharp):
    1. // Source: https://forum.unity.com/threads/projectile-trajectory-prediction.664909/
    2. public static bool CalculateHighTrajectory(Vector3 start, Vector3 end, float muzzleVelocity, float gravity, out float angle)
    3.         {
    4.             Vector3 dir = end - start;
    5.             float vSqr = muzzleVelocity * muzzleVelocity;
    6.             float y = dir.y;
    7.             dir.y = 0.0f;
    8.             float x = dir.sqrMagnitude;
    9.  
    10.             float uRoot = vSqr * vSqr - gravity * (gravity * (x) + (2.0f * y * vSqr));
    11.  
    12.             if (uRoot < 0.0f)
    13.             {
    14.                 // Target is out of range.
    15.                 angle = -45.0f;
    16.                 return false;
    17.             }
    18.  
    19.             float r = Mathf.Sqrt(uRoot);
    20.             float bottom = gravity * Mathf.Sqrt(x);
    21.  
    22.             angle = -(Mathf.Atan2(vSqr + r, bottom) * Mathf.Rad2Deg);
    23.             return true;
    24.         }

    This works, however projectile is falling just tiny bit too short. After review, it looks like this is caused by integration and fact that gravity is applied first, what produces some kind of offset. To compensate this I added additional initial force equal to
    -gravity * delta
    , but then projectile goes too far by exactly same amount, so instead I applied half of this force and now it works correctly.
    You can see below orange sphere what is the target/landing position and two trails, left one with correction and second one without it.
    upload_2022-12-12_15-18-59.png

    My question is: why I need to apply only half of delta?
    Also is it actually the correct way to fix this problem?
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    This is probably the same issue as https://forum.unity.com/threads/not...owing-seb-lagues-kinematic-tutorials.1038586/ . Which I think you have already figured out. You can see at the end I made a small post about the correction factor and the 0.5 scalar to produce an average acceleration effect over the timestep (which is lost in the PhysX optmisation). Assuming this is the issue for you too, then your modification is the correct approach.
     
    Edy and Qriva like this.
  3. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    Thank you, yes this sounds correct. It comes from the fact it assumes the same velocity for duration of delta, but obviously it's not - gravity does job all the time.