Search Unity

I'm measuring 8 m/s in game acceleration due to gravity

Discussion in 'Physics' started by Boogz, Dec 15, 2014.

  1. Boogz

    Boogz

    Joined:
    Dec 9, 2014
    Posts:
    42
    8m/s/s*

    Am I doing something wrong, or is the physics engine quirky?

    Create an object of any mass (1 is easy) that is effected by gravity.

    Attach a script in update that applies an upward force.

    Set the y (upward) component of the force relative to deltaTime, massOfObject, and k

    "k" comes out to 491.5'ish. k*deltaTime comes out to ~8 m/s/s.

    That's almost -20% off Unity. nopenopenope
     
    Last edited: Dec 15, 2014
  2. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,765
    Try doing the AddForce in the FixedUpdate method instead of Update, FixedUpdate is called from the Physics loop and Update only once per frame so you are likely just missing out on the odd call to AddForce.
     
  3. AdamScura

    AdamScura

    Joined:
    Mar 25, 2012
    Posts:
    55
    I don't understand your experiment. What is K?

    I ran my own test with the following script.

    Code (CSharp):
    1.  
    2.     public struct Sample
    3.     {
    4.         public float T;
    5.         public float Y;
    6.         public float V;
    7.     }
    8.  
    9.     public Sample[] Samples = new Sample[10];
    10.  
    11.     IEnumerator Start()
    12.     {
    13.         // Wait 1 second to get rid of any CPU spikes during load
    14.         yield return new WaitForSeconds(1);
    15.         this.rigidbody.useGravity = true;
    16.  
    17.         // Do the test
    18.         int sampleCount = 11;
    19.         this.Samples = new Sample[sampleCount];
    20.         for (int i = 0; i < sampleCount; i++)
    21.         {
    22.             this.Samples[i].T = Time.time;
    23.             this.Samples[i].Y = this.transform.position.y;
    24.             this.Samples[i].V = this.rigidbody.velocity.y;
    25.             yield return new WaitForSeconds(1);
    26.         }
    27.  
    28.         // Report the results
    29.         for (int i = 0; i < sampleCount; i++)
    30.         {
    31.             Sample sample = this.Samples[i];
    32.             Sample prevSample = i > 0 ? this.Samples[i - 1] : sample;
    33.             float totalT = sample.T - this.Samples[0].T;
    34.             float deltaT = sample.T - prevSample.T;
    35.             float deltaV = sample.V - prevSample.V;
    36.             float measuredG = 2 * sample.Y / (totalT * totalT);  // Because y = 1/2 * g * t^2
    37.             Debug.Log("T=" + totalT.ToString("0.00") + " " +
    38.                       "V=" + sample.V.ToString("0.00") + " " +
    39.                       "Y=" + sample.Y.ToString("0.00") + " " +
    40.                       "DV/DT=" + (deltaV / deltaT).ToString("0.00") + " " +
    41.                       "G=" + measuredG.ToString("0.00"));
    42.         }
    43.     }
    44.  
    Output:
    Code (csharp):
    1.  
    2. T=0.00  V=  0.00 Y=  0.00  DV/DT=  NaN G=  NaN
    3. T=1.00  V= -9.81 Y= -5.00  DV/DT=-9.79 G=-9.98
    4. T=2.00  V=-19.62 Y=-19.82  DV/DT=-9.80 G=-9.88
    5. T=3.01  V=-29.43 Y=-44.44  DV/DT=-9.79 G=-9.84
    6. T=4.01  V=-39.24 Y=-78.87  DV/DT=-9.80 G=-9.83
    7. T=5.01  V=-49.05 Y=-123.12 DV/DT=-9.80 G=-9.82
    8. T=6.01  V=-58.86 Y=-177.17 DV/DT=-9.81 G=-9.82
    9. T=7.01  V=-68.67 Y=-241.03 DV/DT=-9.80 G=-9.82
    10. T=8.01  V=-78.48 Y=-314.71 DV/DT=-9.81 G=-9.81
    11. T=9.01  V=-88.29 Y=-398.19 DV/DT=-9.81 G=-9.81
    12. T=10.01 V=-98.10 Y=-491.48 DV/DT=-9.79 G=-9.81
    13.  
    We can see it is pretty damn close to 9.8 m/s^2. There are some fluctuations but that's only because of variations in frame rate.

    If anything Unity will come up a bit shy of 9.8 m/s^2 when you are looking at 1 or 2 frames (I have tested this). It is because Unity actually uses Euler integration to time step the physics simulation, which does not take into account acceleration that occurs between frames.

    I hope this helps clear things up.