Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Periodic slowdown with Physics2D.FindMinimumContinuousContact (2020.3.12f1)

Discussion in 'Physics' started by Radiata, Jul 24, 2022.

  1. Radiata

    Radiata

    Joined:
    Feb 15, 2016
    Posts:
    3
    Hi All,

    Hopefully this issue isn't something I've overlooked a solution to, but the title encapsulates my primary issue. Periodically my scene is encountering major slowdown and the profiler is pointing the finger at Physics2D.FindMinimumContinuousContact. The spheres are also escaping the enclosure during these spikes. I only have a vague guess as to what this call is actually doing and wasn't able to find much in googling.

    The scene generally runs at <5 ms, but will start to build up to a peak 200+ ms periodically and settle back down.

    upload_2022-7-24_13-55-19.png

    The general goal for this is basically a 0 G physics simulation for a puzzle game prototype (based on interactions at the molecular scale). At minimum we need to be able to achieve a 5x speed, but my expectations are up to a 12x speed should be our goal. I do intend to at least test converting the project to dots down the road to improve general performance.


    Overview of scene setup:
    • 50 Sphere meshes with 2D circle colliders and 2D rigid bodies
    • 4 cubes with 2d box colliders enclosing the area where the spheres are added
    • Time scaler attached to a slider, to modify the step passed into PhysicsScene2D.Simulate
    Code (CSharp):
    1.    
    2.     public static float gameTimeScale { get; private set; }
    3.  
    4.     [SerializeField] private TimeScalerSO timeScalerSO;
    5.     [SerializeField] private Slider timeSlider;
    6.  
    7.     private void Awake()
    8.     {
    9.         ResetValues();
    10.     }
    11.  
    12.     private void ResetValues()
    13.     {
    14.         timeSlider.minValue = timeScalerSO.minTimeScale;
    15.         timeSlider.maxValue = timeScalerSO.maxTimeScale;
    16.         timeSlider.value = timeScalerSO.resetValue;
    17.         SetTimeScale(timeScalerSO.resetValue);
    18.     }
    19.  
    20.     public void SetTimeScale(float value)
    21.     {
    22.         gameTimeScale = Mathf.Clamp(value, timeScalerSO.minTimeScale, timeScalerSO.maxTimeScale);
    23.     }
    24.  
    • Script to manually simulate physics steps
    Code (CSharp):
    1.  
    2. public class ManualPhysicsSim : MonoBehaviour
    3. {
    4.     [SerializeField] public float physicsRate = 1f;
    5.     private PhysicsScene2D physicsScene;
    6.  
    7.     private void Awake()
    8.     {
    9.         physicsScene = gameObject.scene.GetPhysicsScene2D();
    10.     }
    11.  
    12.     void FixedUpdate()
    13.     {
    14.         if (Physics2D.simulationMode != SimulationMode2D.Script)
    15.         { return; }
    16.  
    17.         physicsScene.Simulate(Time.fixedUnscaledDeltaTime * physicsRate * TimeScaler.gameTimeScale);
    18.     }
    19. }
    20.  
    • All physics objects are using a material with friction 0 and bounciness 1.
    • Rigid Bodies are Dynamic, Continuous, Minimal Mass, no drag/gravity, never sleep.
    • One sphere has a script that adds a force between .05 and .1 on start.
    Code (CSharp):
    1.  
    2.     void Start()
    3.     {
    4.         rigidBody.AddForce(new Vector2(Random.Range(minForce, maxForce), Random.Range(minForce, maxForce)));
    5.     }
    6.  
    Project settings I've modified:
    • Fixed update is .01
    • Bounce Threshold is 0
    • Auto simulation is false
    • Simulation mode is Script

    The general goal for this is basically a 0 G physics simulation for a puzzle game prototype (based on interactions at the molecular scale). At minimum we need to be able to achieve a 5x speed, but my expectations are up to a 12x speed should be our goal. I do intend to at least test converting the project to dots down the road to improve general performance.

    Drive link to a video preview of the project running at 12x speed with the slowdown occurring near the end:
    https://drive.google.com/file/d/1bZal2eXIQsNXq2vVGOxMKV2giOboppqB/view?usp=sharing
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    This'll simply be a lot of continuous contacts being calculated in a single simulation step. This comes directly from Box2D, it's not a Unity function. Continuous collision detection should be used sparingly. You certainly shouldn't use it for a lot of objects in close proximity.
     
  3. Radiata

    Radiata

    Joined:
    Feb 15, 2016
    Posts:
    3
    Would you have any suggestions to direct my search into how to prevent them from escaping the bounds or losing momentum without using continuous?

    It doesn't seem to really preserve energy during collisions and they are far more frequently shooting out of the enclosed area with discrete collisions. In terms of what we hope to achieve, the object density would also ideally be much higher and there would be 2-3 of these enclosures running in separate physics scenes.
     
  4. uncolike

    uncolike

    Joined:
    Jan 17, 2020
    Posts:
    27

    Hi, I got the same issues. Did you resolve this problem?
     
  5. Radiata

    Radiata

    Joined:
    Feb 15, 2016
    Posts:
    3
    Unfortunately we did not. We decided to change the design a bit to get a feel for the prototype faster, and then ended up putting it on the back burner.