Ballistic - Problem with real trajectory

Hi,

I’m trying to throw an object following a trajectory, and it’s getting me crazy because the object is not following the right trajectory.

I just launch an object of 50.59 and a power of 10. According to the maths and physics, it should land 10m further (gravity of -9.81).

The simulation works fine (blue) and lands exactly at the right point, but the object itself landed a few cm before (white) and has a different trajectory, as you can see on the screenshot:

float angleToShoot;
float power = 10;

void Throw(){

angleToShoot = 50.59f;

Vector3 shootDir = Quaternion.Euler (-50.59f, 0, 0) * Vector3.forward;
force = shootDir * power;

GoToLaunch.transform.GetComponent<Rigidbody> ().velocity = force;
GoToLaunch.transform.GetComponent<Rigidbody> ().useGravity = true;

}


//Draw the "right" trajectory
void OnDrawGizmos(){

float h = 0.0f;

while(transform.TransformPoint(PlotTrajectoryAtTime(h)).y >= 0){
      Gizmos.DrawSphere(transform.TransformPoint(PlotTrajectoryAtTime(h)), 0.2f);
       h = h + 0.1f;
}

}


public Vector3 PlotTrajectoryAtTime (float t) {
       return force*t + Physics.gravity*t*t*0.5f;
}

It’s been driving me crazy for hours, I can’t seem to find what’s the problem…
any help would be highly appreciated :frowning:

This is really weird, it seems that Unity 5.3.4 does not follow the same physics rule than in the past… is there a bug maybe?

I made the following test:
On one sphere I put this code:

Void Start(){

Vector3 shootDir = Quaternion.Euler (-50f, 0, 0) * Vector3.forward;
force = shootDir * 10;
  
GoToLaunch.transform.GetComponent<Rigidbody> ().velocity = force;
}

And on another one:

float startTime;
float force;

void Start(){
   startTime = Time.time;
 
   Vector3 shootDir = Quaternion.Euler (-50f, 0, 0) * Vector3.forward;
   force = shootDir * 10;
}

void Update(){
  
        float t = Time.time - startTime;
        transform.position = force * t + Physics.gravity * t * t * 0.5f;
  
}

The 2nd sphere follows the right trajectory, but not the first one.
This seems to mean that the velocity/physics in Unity 5.3.4 does NOT follow this physics formula: velocity * t + Physics.gravity * t * t * 0.5f ? It was working in previous versions AFAIK…

Could someone test this and check if they have the same behavior? or let me know which obvious thing I am missing…?

Assuming drag is turned off?

This is probably an aftefact of the fixed step simulation in Unity. The kinematic equations assume that a force is applied evenly over the life time of an object. Unity actually only applies this force every 0.02 seconds. The result is a very slight difference in trajectories.

You can fix this by reducing the physics time step, or by by using an iterative solution to build the trajectory. Both of these come with computational overhead.

It might be possible to use some discreet calculus tricks to come to the same solution in a single, non expensive equation. It’s been a while since I did calc, so I couldn’t point you in the right direction here.

Or you could do a simple fudge factor empirically.

Yeah for me I just made my own version trying to use the physics engine and came back with the same result (Using this):

        protected float launchVelocity
        {
            get
            {
                return Mathf.Sqrt(((_weaponRange * -Physics.gravity.y) / Mathf.Sin(2 * 45))) * projectile.GetComponent<Rigidbody>().mass;
            }
        }

        protected float LaunchAngle(float _targetDistance)
        {
                return (Mathf.Rad2Deg * Mathf.Asin( ((_targetDistance * -Physics.gravity.y) / Mathf.Pow(launchVelocity, 2)) ) );
        }

So for me in the end I created my own ballistics physics engine. Purely for ballistics.

The problem indeed comes from Timestep.
Depending on its value, the results are very different… If I set the tilmestep to 0.01 instead of 0.02 I have closer results, although still not perfect.

This means the simulation cannot rely on
launchvelocity * t + Physics.gravity * t * t * 0.5f

I need to find a way to temper it to reflect Unity physics… any idea what might be the formula?

You’re using slightly different angles for both, why? -50.59f and -50f.

This is just a different example, but this is the same angle in both scripts.

The main issue I have is that the simulation calculation is based on real physics, with launchvelocity * t + Physics.gravity * t * t * 0.5f. And I think I need to adapt it to use Time.fixedTime, with the time used by Physics.

Bu I don’t manage to do it so far…