Boogz
December 15, 2014, 8:18am
1
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
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.
I don’t understand your experiment. What is K?
I ran my own test with the following script.
public struct Sample
{
public float T;
public float Y;
public float V;
}
public Sample[] Samples = new Sample[10];
IEnumerator Start()
{
// Wait 1 second to get rid of any CPU spikes during load
yield return new WaitForSeconds(1);
this.rigidbody.useGravity = true;
// Do the test
int sampleCount = 11;
this.Samples = new Sample[sampleCount];
for (int i = 0; i < sampleCount; i++)
{
this.Samples[i].T = Time.time;
this.Samples[i].Y = this.transform.position.y;
this.Samples[i].V = this.rigidbody.velocity.y;
yield return new WaitForSeconds(1);
}
// Report the results
for (int i = 0; i < sampleCount; i++)
{
Sample sample = this.Samples[i];
Sample prevSample = i > 0 ? this.Samples[i - 1] : sample;
float totalT = sample.T - this.Samples[0].T;
float deltaT = sample.T - prevSample.T;
float deltaV = sample.V - prevSample.V;
float measuredG = 2 * sample.Y / (totalT * totalT); // Because y = 1/2 * g * t^2
Debug.Log("T=" + totalT.ToString("0.00") + " " +
"V=" + sample.V.ToString("0.00") + " " +
"Y=" + sample.Y.ToString("0.00") + " " +
"DV/DT=" + (deltaV / deltaT).ToString("0.00") + " " +
"G=" + measuredG.ToString("0.00"));
}
}
Output:
T=0.00 V= 0.00 Y= 0.00 DV/DT= NaN G= NaN
T=1.00 V= -9.81 Y= -5.00 DV/DT=-9.79 G=-9.98
T=2.00 V=-19.62 Y=-19.82 DV/DT=-9.80 G=-9.88
T=3.01 V=-29.43 Y=-44.44 DV/DT=-9.79 G=-9.84
T=4.01 V=-39.24 Y=-78.87 DV/DT=-9.80 G=-9.83
T=5.01 V=-49.05 Y=-123.12 DV/DT=-9.80 G=-9.82
T=6.01 V=-58.86 Y=-177.17 DV/DT=-9.81 G=-9.82
T=7.01 V=-68.67 Y=-241.03 DV/DT=-9.80 G=-9.82
T=8.01 V=-78.48 Y=-314.71 DV/DT=-9.81 G=-9.81
T=9.01 V=-88.29 Y=-398.19 DV/DT=-9.81 G=-9.81
T=10.01 V=-98.10 Y=-491.48 DV/DT=-9.79 G=-9.81
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.