Projectile delta time trajectory offset

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

// Source: https://discussions.unity.com/t/740303
public static bool CalculateHighTrajectory(Vector3 start, Vector3 end, float muzzleVelocity, float gravity, out float angle)
        {
            Vector3 dir = end - start;
            float vSqr = muzzleVelocity * muzzleVelocity;
            float y = dir.y;
            dir.y = 0.0f;
            float x = dir.sqrMagnitude;

            float uRoot = vSqr * vSqr - gravity * (gravity * (x) + (2.0f * y * vSqr));

            if (uRoot < 0.0f)
            {
                // Target is out of range.
                angle = -45.0f;
                return false;
            }

            float r = Mathf.Sqrt(uRoot);
            float bottom = gravity * Mathf.Sqrt(x);

            angle = -(Mathf.Atan2(vSqr + r, bottom) * Mathf.Rad2Deg);
            return true;
        }

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.
8654562--1164933--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?

This is probably the same issue as Not reaching exact jump height using kinematic equation? (following Seb Lague's kinematic tutorials) . 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.

2 Likes

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.