EntityComponentSystemSamples icon indicating copy to clipboard operation
EntityComponentSystemSamples copied to clipboard

Incorrect collider event sample

Open Camarent opened this issue 5 years ago • 1 comments

Hi!

I recently tried to create a game on dots and I used Unty Physics Sample as reference for few things. I found collider event example there which can be incorrect.

I am talking about sample below: `[UpdateAfter(typeof(EndFramePhysicsSystem))] unsafe public class CollisionEventImpulseSystem : JobComponentSystem { BuildPhysicsWorld m_BuildPhysicsWorldSystem; StepPhysicsWorld m_StepPhysicsWorldSystem;

EntityQuery ImpulseGroup;
protected override void OnCreate()
{
    m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
    m_StepPhysicsWorldSystem = World.GetOrCreateSystem<StepPhysicsWorld>();
    ImpulseGroup = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[] { typeof(CollisionEventImpulse), }
    });
}

[BurstCompile]
struct CollisionEventImpulseJob : ICollisionEventsJob
{
    [ReadOnly] public ComponentDataFromEntity<CollisionEventImpulse> ColliderEventImpulseGroup;
    public ComponentDataFromEntity<PhysicsVelocity> PhysicsVelocityGroup;

    public void Execute(CollisionEvent collisionEvent)
    {
        Entity entityA = collisionEvent.Entities.EntityA;
        Entity entityB = collisionEvent.Entities.EntityB;

        bool isBodyADynamic = PhysicsVelocityGroup.Exists(entityA);
        bool isBodyBDynamic = PhysicsVelocityGroup.Exists(entityB);

        bool isBodyARepulser = ColliderEventImpulseGroup.Exists(entityA);
        bool isBodyBRepulser = ColliderEventImpulseGroup.Exists(entityB);

        if(isBodyARepulser && isBodyBDynamic)
        {
            var impulseComponent = ColliderEventImpulseGroup[entityA];
            var velocityComponent = PhysicsVelocityGroup[entityB];
            velocityComponent.Linear = impulseComponent.Impulse;
            PhysicsVelocityGroup[entityB] = velocityComponent;
        }
        if (isBodyBRepulser && isBodyADynamic)
        {
            var impulseComponent = ColliderEventImpulseGroup[entityB];
            var velocityComponent = PhysicsVelocityGroup[entityA];
            velocityComponent.Linear = impulseComponent.Impulse;
            PhysicsVelocityGroup[entityA] = velocityComponent;
        }
    }
}

protected override JobHandle OnUpdate(JobHandle inputDeps)
{
    JobHandle jobHandle = new CollisionEventImpulseJob
    {
        ColliderEventImpulseGroup = GetComponentDataFromEntity<CollisionEventImpulse>(true),
        PhysicsVelocityGroup = GetComponentDataFromEntity<PhysicsVelocity>(),
    }.Schedule(m_StepPhysicsWorldSystem.Simulation, 
                ref m_BuildPhysicsWorldSystem.PhysicsWorld, inputDeps);

    return jobHandle;
}

} `

It may produce job dependency problems. You can look at this post on unity forum about this problems: Post on Unity Forum

Camarent avatar Jan 18 '20 08:01 Camarent

The latest version of Unity Physics now allows users to GetOutputDependency and AddInputDependency to each Physics System. This means that users can appropriately chain their dependent jobs.

Furthermore the upcoming release will add a AddInputDependencyToComplete function on the BuildPhysicsWorld system. This allows users to add dependent jobs that must be completed before the Physics world does any further work for that step.

carpetbagger avatar Oct 13 '20 09:10 carpetbagger