Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect collider event sample #171

Open
Camarent opened this issue Jan 18, 2020 · 1 comment
Open

Incorrect collider event sample #171

Camarent opened this issue Jan 18, 2020 · 1 comment

Comments

@Camarent
Copy link

@Camarent Camarent commented Jan 18, 2020

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

@carpetbagger
Copy link
Collaborator

@carpetbagger carpetbagger commented Oct 13, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.