Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

"isKinematic" mode but for Unity Physics [disabling integrator for custom behaviour]?

Discussion in 'Physics for ECS' started by Mr-Mechanical, Nov 27, 2019.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I've run across a very specific use case: where I need to use collision detection/intersection data of convex shapes, all with positions changing in a "non-physics" based manner. Is there an easy way to make a "Kinematic" mode for Unity physics, thus making it easy to disable the integrator for this purpose of extracting collision info? Basically I have my own integration and collision response system I'd like to experiment combining Unity Physic's collision system with. Thank you. The advice is extremely helpful.
     
  2. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    If you mark all of your rigid bodies as kinematic (= no PhysicsMass component) and also mark the colliders as raising collision events (Material.EnableCollisionEvents), then they will all generate contacts and jacobians and the solver will only raise collision events rather than apply impulses. (If you don't mark them to raise events they will just early out during collision detection).

    This gives you close to what you want I think. The collision events won't give you the full contact manifold you may be looking for, so you might want to use an IContactJobs to extract that info during the step (see DisplayContactsSystem in the examples for how to use that).
     
    Mr-Mechanical likes this.
  3. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    This is great! Fantastic! I just need to understand more about what kind of collision info will be extracted out of this setup. I still have to look at collision events, but will I get GJK/EPA info? If this is the case-I think that will probably be good enough for my use case. Thanks a lot, I believe I'm getting very close to solving this.
     
    Last edited: Nov 27, 2019
  4. Scorr

    Scorr

    Joined:
    Jul 2, 2013
    Posts:
    73
    I've been wanting to do non-realistic physics as well using kinematic bodies and have been casting colliders to get collisions. Collision events sound like they would be a better approach. However if I mark a kinematic body to raise collision events I get the following error when it collides:
    This is using Entities 0.2.0 and Physics 0.2.4. Could someone confirm this? Or am I doing something wrong here?
     
  5. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    What kind of info will be missing from the contact manifold when using this kinematic + collision events setup? Are the Jacobians necessary for contacts (or are they exclusively generated for joints-in this case I wouldn't have to worry) since I have a system that only requires GJK/EPA output data for collision response and doesn't require any jacobians data(should I think about removing the Jacobians for optimization)?

    I'm extremely close to solving this problem I really appreciate the detailed help, it is extraordinarily helpful for my project.
     
  6. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    If you check out the ModifyContactJacobian demo, it shows all the major information available, and modifiable for the Contacts and Jacobians. At runtime Unity Physics doesn't actually differentiate between 'kinematic' bodies and any other dynamic bodies, they are just dynamic bodies with infinite mass and go through the same code paths. So all the same contact manifold information is available.
    Jacobians are created for Joints and for Contacts. If you don't need the information in from the
    IJacobiansJob
    , then in your
    IContactsJob 
    set
    manifold.JacobianFlags |= JacobianFlags.Disabled
     
    Last edited: Nov 28, 2019
    Mr-Mechanical likes this.
  7. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    I'm seeing this as well. Will take a look.
     
  8. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    So, there is a bug here with collision events and kinematic bodies. I'll check in the fix for the next version but if you have Unity Physics as a local package you can tweak
    SolveInfMassPair
    in com.unity.physics\Unity.Physics\Dynamics\Jacobians\ContactJacobian.cs to call
    ExportCollisionEvents 
    rather than write to the stream directly.

    Code (CSharp):
    1.         public void SolveInfMassPair(
    2.             ref JacobianHeader jacHeader, MotionVelocity velocityA, MotionVelocity velocityB,
    3.             Solver.StepInput stepInput, ref NativeStream.Writer collisionEventsWriter)
    4.         {
    5.             // Infinite mass pairs are only interested in collision events,
    6.             // so only last iteration is performed in that case
    7.             if (!stepInput.IsLastIteration)
    8.             {
    9.                 return;
    10.             }
    11.  
    12.             // Calculate normal impulses and fire collision event
    13.             // if at least one contact point would have an impulse applied
    14.             for (int j = 0; j < BaseJacobian.NumContacts; j++)
    15.             {
    16.                 ref ContactJacAngAndVelToReachCp jacAngular = ref jacHeader.AccessAngularJacobian(j);
    17.  
    18.                 float relativeVelocity = BaseContactJacobian.GetJacVelocity(BaseJacobian.Normal, jacAngular.Jac, velocityA, velocityB);
    19.                 float dv = jacAngular.VelToReachCp - relativeVelocity;
    20.                 if (jacAngular.VelToReachCp > 0 || dv > 0)
    21.                 {
    22.                     ExportCollisionEvent(0, ref jacHeader, ref collisionEventsWriter);
    23.                     return;
    24.                 }
    25.             }
    26.         }
     
    Mr-Mechanical and florianhanke like this.
  9. Scorr

    Scorr

    Joined:
    Jul 2, 2013
    Posts:
    73
    That works, thanks! I didn't expect such a fast fix.
     
    steveeHavok likes this.
  10. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Thank you for the clear response informing me of this very useful flag.