Search Unity

Best time in fixed step sim to add impulse, raycast, more

Discussion in 'Physics for ECS' started by BigRookGames, Jan 2, 2021.

  1. BigRookGames

    BigRookGames

    Joined:
    Nov 24, 2014
    Posts:
    330
    I have been messing around with the sorting of various systems within the FixedStepSimulationSystemGroup and was curious about what is considered the best time to apply all of the custom interactions.

    For my use case, there are impulses, explosion force, raycasts, overlap checks, positional offsets, animation offsets, etc.. all done each cycle. I see that a few other posts recommend doing this after the fixed step physics and I was wondering if this is always the case.

    Because there are animations and positional /rotational offsets within the systems, wouldn't it be best to apply these prior to the physics system running its calculations? So that if a positional change moves an object to where it is overlapping another, it will push back before the end of the cycle?
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    The main question is whether you can use broadphase information from the previous frames physics information or need the latest updated broadphase information.
    Before BuildPhysicsWorld is updated the broadphase is based on the transforms and velocities from the previous frame. This means that you may be missing hits when you do queries against the old broadphase data. You can run queries immediately after BuildPhysicsWorld is updated, but you shouldn't change ComponentData (e.g. Translation, Rotation and PhysicsVelocity) information between BuildPhysicsWorld and ExportPhysicsWorld as ExportPhysicsWorld will override the changes with the new physics simulation data.

    After ExportPhysicsWorld, if you critically need the data to be fully in sync and up to date, check out the 'Synchronize Collision World' flag in the PhysicsStep component. This will recalculate the broadphase information after StepPhysicsWorld so that everything is updated to the latest, at the expensive of the extra work.

    So it really depends on the use-case. If you are using velocity information to feed into animation blend weights you can run your system before physics. If you are needing the new body position for a look at IK you'll need to grab them after the physics updates.