Search Unity

Render at 120 FPS with physics at 60 FPS

Discussion in 'Physics for ECS' started by argibaltzi, Feb 9, 2021.

  1. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Hello

    I have a system that does SphereCasts and then disables some entities, however some frames this system will run twice due to the higher framerate but the physics has only run once.

    The SphereCast on the second frame returns disabled entities which i assume is because BuildPhysicsWorld has not run yet??

    What is the best way to handle such cases?
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    You're right about the source of the problem! It is not advised to change physics-related data outside of FixedStepSimulationSystemGroup , because you might end up querying stale data. If you'd like to learn more about fixed time step, please see the fixed timestep thread.

    It's best if you can make any changes to PhysicsWorld or physics-related ECS components (including Entity enable/disable) before BuildPhysicsWorld or after ExportPhysicsWorld. There's different options (depending on what you need) and this thread might be a good read.
     
  3. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    I was a little hesitant to do any work inside the FixedStepSimulationSystemGroup as i am not certain what some systems do in particular EndFramePhysicsSystem and EndFixedStepSimulationCommandBuffer
    What do those systems do that i need to consider in my physics simulation?

    If i wanted to apply an explosion on an entity, is after ExportPhysicsWorld the best place? will the results be seen immediately on screen? or its better to do that before BuildPhysicsWorld
     
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Any time after ExportPhysicsWorld and before next BuildPhysicsWorld is good for changing data in PhysicsWorld or ECS components. However, note that PhysicsWorld will be rebuilt in BuildPhysicsWorld, so if you change any data don't expect to see those changes before BuildPhysicsWorld had a chance to run.
    EndFramePhysicsSystem is basically just an end marker for physics step.

    The most important physics systems are:
    • BuildPhysicsWorld – schedules the jobs that create physics runtime data from ECS data for the current simulation step. Resets runtime data in OnUpdate() before spawning any jobs, so it is important to notify this system about any jobs that might be reading runtime data, by passing a job handle to AddInputDependencyToComplete(). Before resetting runtime data, BuildPhysicsWorld will complete all input dependencies and thus allow those jobs to finish working on valid data.
    • StepPhysicsWorld – schedules the jobs that perform physics simulation and execute callbacks. Works on physics runtime data (read/write).
    • ExportPhysicsWorld – schedules the jobs that copy the result of physics step from physics runtime data to ECS components.

    Aside from positioning your system's OnUpdate with UpdateBefore and UpdateAfter attributes, you should link the job(s) you spawn with jobs from previous and next system using GetOutputDependency and AddInputDependency.

    Scheduling physics jobs is not so straightforward at the moment, but we're working on making that easier and better documented.
     
    argibaltzi likes this.
  5. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Is it possible to run a system before the Physics FixedStepSimulation and is it advised? I was thinking to add the input system to run first so i can setup the physics stuff, this way by the end of frame everything should be done and ready to render
     
  6. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    The problem is that you don't know how many ticks outside of FixedStepSimulation will happen, it's not necessarily 1:1. You can read more in the fixed timestep post I linked previously.
     
    argibaltzi likes this.