Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Multiple collision events

Discussion in 'Physics Previews' started by hardcodednumber, Jun 12, 2020.

  1. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    Hi i'm tying to make a simple brick breaker game so I can understand ECS and Physics. I am having trouble with my ball colliding with the paddle. I want the ball to reflect off the paddle and maintain the same speed. However when I I use the ICollisionEventsJob and solve, it receive multiple events. Is there something I am doing wrong?

    Or is the problem that the ball is moving too fast and intersecting itself into the paddle?

    here is my Job code:
    Code (CSharp):
    1.  [BurstCompile]
    2.     struct CollisionEventSystemJob : ICollisionEventsJob
    3.     {
    4.         private const int BallSpeed = 40;
    5.  
    6.         [ReadOnly] public ComponentDataFromEntity<CollisionEventImpulseData> ColliderEventImpulseGroup;
    7.         public ComponentDataFromEntity<PhysicsVelocity> PhysicsVelocityGroup;
    8.         [ReadOnly] public ComponentDataFromEntity<Translation> TranslationGroup;
    9.  
    10.         public void Execute(CollisionEvent collisionEvent)
    11.         {
    12.             var entityA = collisionEvent.Entities.EntityA;
    13.             var entityB = collisionEvent.Entities.EntityB;
    14.  
    15.             var isBodyADynamic = PhysicsVelocityGroup.Exists(entityA);
    16.             var isBodyBDynamic = PhysicsVelocityGroup.Exists(entityB);
    17.  
    18.             var isBodyARepulser = ColliderEventImpulseGroup.Exists(entityA);
    19.             var isBodyBRepulser = ColliderEventImpulseGroup.Exists(entityB);
    20.  
    21.             var translationA = TranslationGroup[entityA];
    22.             var translationB = TranslationGroup[entityB];
    23.  
    24.             if (isBodyARepulser && isBodyBDynamic) {
    25.                 var impulseComponent = ColliderEventImpulseGroup[entityA];
    26.                 var velocityComponent = PhysicsVelocityGroup[entityB];
    27.  
    28.                 if (impulseComponent.reflect == 1) {
    29.                     var direction = normalize(velocityComponent.Linear);
    30.                     var normal = collisionEvent.Normal;
    31.                     var velocity = reflect(direction, normal) * BallSpeed;
    32.  
    33.                     velocityComponent.Linear = velocity;
    34.                 }
    35.  
    36.                 PhysicsVelocityGroup[entityB] = velocityComponent;
    37.             }
    38.  
    39.             if (isBodyBRepulser && isBodyADynamic) {
    40.                 var impulseComponent = ColliderEventImpulseGroup[entityB];
    41.                 var velocityComponent = PhysicsVelocityGroup[entityA];
    42.                 var translation = TranslationGroup[entityA];
    43.  
    44.                 if (impulseComponent.reflect == 1) {
    45.                     var direction = normalize(velocityComponent.Linear);
    46.                     var normal = collisionEvent.Normal;
    47.                     var velocity = reflect(direction, normal) * BallSpeed;
    48.                     Debug.Log($"Normal: {normal} Dir: {direction} Relect: {velocity}");
    49.                     velocityComponent.Linear = velocity;
    50.                 }
    51.  
    52.                 PhysicsVelocityGroup[entityA] = velocityComponent;
    53.             }
    54.         }
    55.     }
    I attached the debug of the frames, where the blue arrow up is the first frame.
    I am using ECS preview.6 - 0.10.0 and Physics preview 0.3.2
     

    Attached Files:

  2. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    Also, if I set my linear velocity to something constant like float3.up * BallSpeed, I don't receive multiple events
     
  3. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    I figured it out, I needed to move the ball outside the collision volume. I just pushed the ball along the velocity vector and everything is work! Furthermore, I tried switching my simulation to Havok, and that also pushed the ball outside the the collision.
     
    petarmHavok likes this.