Search Unity

Stuttering when calling Physics2D.Simulate

Discussion in 'Physics' started by pontus_unity892, Dec 19, 2018.

  1. pontus_unity892

    pontus_unity892

    Joined:
    Nov 7, 2018
    Posts:
    7
    Hello! I've been trying to get this to work (https://docs.unity3d.com/ScriptReference/Physics2D.Simulate.html)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BasicSimulation : MonoBehaviour
    4. {
    5.     private float timer;
    6.  
    7.     void Update()
    8.     {
    9.         if (Physics2D.autoSimulation)
    10.             return; // do nothing if the automatic simulation is enabled
    11.  
    12.         timer += Time.deltaTime;
    13.  
    14.         // Catch up with the game time.
    15.         // Advance the physics simulation in portions of Time.fixedDeltaTime
    16.         // Note that generally, we don't want to pass variable delta to Simulate as that leads to unstable results.
    17.         while (timer >= Time.fixedDeltaTime)
    18.         {
    19.             timer -= Time.fixedDeltaTime;
    20.             Physics2D.Simulate(Time.fixedDeltaTime);
    21.         }
    22.  
    23.         // Here you can access the transforms state right after the simulation, if needed...
    24.     }
    25. }
    I basically create an empty scene, add this script to an object, create a sphere with 2d collider and rigidbody on it, set its interpolate value to interpolate, and then of course disable auto simulation.

    When i press play it didn't appear to move as smooth as i had hoped, and when i changed the fixed timestep to something big, in this case 0.1, you can really see how jagged its movement is. But when i turn auto simulation on again, it moves smooth! You can see how it looks in the video below.



    Any ideas of what might be going on? It seems as the interpolate doesn't work when you call Physics2D.Simulate. This is on the latest version of Unity(2018.3.0f2)
     
  2. pontus_unity892

    pontus_unity892

    Joined:
    Nov 7, 2018
    Posts:
    7
    Hmmm, apparently if you call Physics2D.Simulate in FixedUpdate you get rid of the stuttering. So it has do do with the call order?
     
  3. herb_nice

    herb_nice

    Joined:
    May 4, 2017
    Posts:
    170
    You are on the right track by turning autosimulation off, but your update loop still quantizes simulation time to rate that is potentially very different than your display's refresh rate.

    There are many demons at play here. after a mountain of headaches I was able to get smooth procedural animation and camera synchronized with physics almost entirely jitter-free, but calling Physics2d.simulate() during fixed update was not the thing that worked. infact the dated concept of fixed update is a major source of jitter in unity...

    this thread goes in to a lot of detail about what people have tried, what works and what doesn't: https://forum.unity.com/threads/time-deltatime-not-constant-vsync-camerafollow-and-jitter.430339/
     
    pontus_unity892 likes this.
  4. pontus_unity892

    pontus_unity892

    Joined:
    Nov 7, 2018
    Posts:
    7
    But it is smooth as long as auto simulation is on, and when i turn it off and use Physics2D.Simulate it stutters (but when i call it in FixedUpdate it's smooth again!). Even Unity claim that the example code is "...an example of a basic simulation that implements what's being done in the automatic simulation mode." which doesn't seem to be the case? Hmmmmm...