Reflect velocity during CollisionEvent

I pulled the Collision Event from the Physics Example project. I wanted to add reflecting the incoming velocity as an option instead of just applying the impulse.

However, when I debug my code, it seems like the object(Ball) is colliding multiple times. Is there a way to avoid this?

This is what I am doing:

    [UpdateAfter(typeof(EndFramePhysicsSystem))]
    public class CollisionEventSystem : JobComponentSystem
    {
        private BuildPhysicsWorld _physicsWorld;
        private StepPhysicsWorld _stepPhysicsWorld;
        private EntityQuery _impluseGroup;

        protected override void OnCreate()
        {
            _physicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
            _stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
            _impluseGroup = GetEntityQuery(new EntityQueryDesc {
                All = new ComponentType[] { typeof(CollisionEventImpulse) }
            });
        }

        protected override JobHandle OnUpdate(JobHandle inputDependencies)
        {
            var handle = new CollisionEventSystemJob() {
                ColliderEventImpulseGroup = GetComponentDataFromEntity<CollisionEventImpulse>(true),
                PhysicsVelocityGroup = GetComponentDataFromEntity<PhysicsVelocity>()
            }.Schedule(_stepPhysicsWorld.Simulation, ref _physicsWorld.PhysicsWorld, inputDependencies);

            handle.Complete();
            return handle;
        }
    }

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

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

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

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

            if (isBodyARepulser && isBodyBDynamic) {
                var impulseComponent = ColliderEventImpulseGroup[entityA];
                var velocityComponent = PhysicsVelocityGroup[entityB];

                if (impulseComponent.Reflect == 1) {
                    velocityComponent.Linear = reflect(velocityComponent.Linear, collisionEvent.Normal);
                }
                else {
                    velocityComponent.Linear = impulseComponent.Impulse;
                }

                PhysicsVelocityGroup[entityB] = velocityComponent;
            }

            if (isBodyBRepulser && isBodyADynamic) {
                var impulseComponent = ColliderEventImpulseGroup[entityB];
                var velocityComponent = PhysicsVelocityGroup[entityA];

                if (impulseComponent.Reflect == 1) {
                    velocityComponent.Linear = reflect(velocityComponent.Linear, collisionEvent.Normal);
                }
                else {
                    velocityComponent.Linear = impulseComponent.Impulse;
                }

                PhysicsVelocityGroup[entityA] = velocityComponent;
            }
        }
    }

Using your code as a template, I am currently trying to change the speed value of my entities ( Update component data on collision ).

Any idea why my update isn’t working? In case you solved your issue: How did you fix it?

I should reply to this thread and say I figured it out myself. Instead of using the velocity component’s linear property, i generated my own direction vector and set a constant magnitude