Does physic predictions work on simulation data?

Hi, physic predictions are working quite nice, especially with impulses and dynamic physic bodies. I can get it to work with PhysicsVelocity.ApplyLinearImpulse() before BuildPhysicWorld, but what I wonder is, is it also possible to use physic preditions with PhysicsWorld.ApplyImpulse() (working on physic simulation data, instead of ECS components)?

I get the error “To guarantee safety, you must include Broadphase: prepareStaticBodyDataJob as a dependency of the newly scheduled job.” when doing this

[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(BuildPhysicsWorld))]
[UpdateBefore(typeof(StepPhysicsWorld))]
public partial class PredictionSwitchingApplyInputSystem : SystemBase
{
    private GhostPredictionSystemGroup m_GhostPredictionSystemGroup;
    private BuildPhysicsWorld buildPhysicsWorld;

    protected override void OnCreate()
    {
        base.OnCreate();
        RequireSingletonForUpdate<PredictionSwitchingSpawner>();
        m_GhostPredictionSystemGroup = World.GetExistingSystem<GhostPredictionSystemGroup>();
        buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    }

    protected override void OnStartRunning() {
        base.OnStartRunning();
        buildPhysicsWorld.RegisterPhysicsRuntimeSystemReadWrite();
    }

    protected override void OnUpdate()
    {
        var tick = m_GhostPredictionSystemGroup.PredictingTick;
        PhysicsWorld world = this.buildPhysicsWorld.PhysicsWorld;

        Dependency = Entities
            .ForEach(
                (
                    Entity entity,
                    DynamicBuffer<PredictionSwitchingInput> inputBuffer,
                    in Translation translation,
                    in PredictedGhostComponent prediction
                ) => {
                    if (!GhostPredictionSystemGroup.ShouldPredict(tick, prediction)) {
                        return;
                    }

                    inputBuffer.GetDataAtTick(tick, out PredictionSwitchingInput input);

                    float3 impulse = new float3();

                    if (input.horizontal > 0)
                        impulse = new float3(10, 0, 0);
                    if (input.horizontal < 0)
                        impulse = new float3(-10, 0, 0);
                    if (input.vertical > 0)
                        impulse = new float3(0, 0, 10);
                    if (input.vertical < 0)
                        impulse = new float3(0, 0, -10);

                    int ceIdx = world.GetRigidBodyIndex(entity);
                    world.ApplyImpulse(ceIdx, impulse, translation.Value);
                }
           ).Schedule(Dependency);
    }
}

yes you can do that and should work. IIRC you still need to get the JobHandle from the build system and add it as depending to the Entities.ForEach, since that job write on some native array and dependency tracking is not resolved correctly.

1 Like

Thank you CMarastoni, that’s good news!
Ok then I guess this question is more related to Unity Physics, because I have no idea how to get this dependency.
So I added my question there.