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

Reflect velocity during CollisionEvent

Discussion in 'Physics Previews' started by hardcodednumber, Nov 26, 2019.

  1. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    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:
    Code (CSharp):
    1.    
    2.     [UpdateAfter(typeof(EndFramePhysicsSystem))]
    3.     public class CollisionEventSystem : JobComponentSystem
    4.     {
    5.         private BuildPhysicsWorld _physicsWorld;
    6.         private StepPhysicsWorld _stepPhysicsWorld;
    7.         private EntityQuery _impluseGroup;
    8.  
    9.         protected override void OnCreate()
    10.         {
    11.             _physicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    12.             _stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    13.             _impluseGroup = GetEntityQuery(new EntityQueryDesc {
    14.                 All = new ComponentType[] { typeof(CollisionEventImpulse) }
    15.             });
    16.         }
    17.  
    18.         protected override JobHandle OnUpdate(JobHandle inputDependencies)
    19.         {
    20.             var handle = new CollisionEventSystemJob() {
    21.                 ColliderEventImpulseGroup = GetComponentDataFromEntity<CollisionEventImpulse>(true),
    22.                 PhysicsVelocityGroup = GetComponentDataFromEntity<PhysicsVelocity>()
    23.             }.Schedule(_stepPhysicsWorld.Simulation, ref _physicsWorld.PhysicsWorld, inputDependencies);
    24.  
    25.             handle.Complete();
    26.             return handle;
    27.         }
    28.     }
    29.  
    30.     [BurstCompile]
    31.     struct CollisionEventSystemJob : ICollisionEventsJob
    32.     {
    33.         [ReadOnly] public ComponentDataFromEntity<CollisionEventImpulse> ColliderEventImpulseGroup;
    34.         public ComponentDataFromEntity<PhysicsVelocity> PhysicsVelocityGroup;
    35.  
    36.         public void Execute(CollisionEvent collisionEvent)
    37.         {
    38.             var entityA = collisionEvent.Entities.EntityA;
    39.             var entityB = collisionEvent.Entities.EntityB;
    40.  
    41.             var isBodyADynamic = PhysicsVelocityGroup.Exists(entityA);
    42.             var isBodyBDynamic = PhysicsVelocityGroup.Exists(entityB);
    43.  
    44.             var isBodyARepulser = ColliderEventImpulseGroup.Exists(entityA);
    45.             var isBodyBRepulser = ColliderEventImpulseGroup.Exists(entityB);
    46.  
    47.             if (isBodyARepulser && isBodyBDynamic) {
    48.                 var impulseComponent = ColliderEventImpulseGroup[entityA];
    49.                 var velocityComponent = PhysicsVelocityGroup[entityB];
    50.  
    51.                 if (impulseComponent.Reflect == 1) {
    52.                     velocityComponent.Linear = reflect(velocityComponent.Linear, collisionEvent.Normal);
    53.                 }
    54.                 else {
    55.                     velocityComponent.Linear = impulseComponent.Impulse;
    56.                 }
    57.  
    58.                 PhysicsVelocityGroup[entityB] = velocityComponent;
    59.             }
    60.  
    61.             if (isBodyBRepulser && isBodyADynamic) {
    62.                 var impulseComponent = ColliderEventImpulseGroup[entityB];
    63.                 var velocityComponent = PhysicsVelocityGroup[entityA];
    64.  
    65.                 if (impulseComponent.Reflect == 1) {
    66.                     velocityComponent.Linear = reflect(velocityComponent.Linear, collisionEvent.Normal);
    67.                 }
    68.                 else {
    69.                     velocityComponent.Linear = impulseComponent.Impulse;
    70.                 }
    71.  
    72.                 PhysicsVelocityGroup[entityA] = velocityComponent;
    73.             }
    74.         }
    75.     }
    76.  
     
  2. stefan-falk

    stefan-falk

    Joined:
    Nov 24, 2019
    Posts:
    15
  3. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    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