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.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

How to calculate collision impulses to apply with ApplyImpulse()

Discussion in 'Physics for ECS' started by PhilSA, Jun 21, 2022.

  1. PhilSA


    Jul 11, 2013
    I'm facing a situation where I need to solve collision impulses manually instead of relying on the physics engine. Imagine this scenario: a blue trigger collider moves towards a red dynamic rigidbody. We want to pretend that the blue trigger has a mass of X, and we want to calculate the impulse to apply on the red body so that it imitates a collision that would be solved by the physics engine:

    So basically, given this information:
    • collision normal
    • collision point
    • Physics mass/velocity of A
    • Physics mass/velocity of B
    What is the physically-correct way of calculating the impulse that we should apply on bodies A & B using PhysicsVelocity.ApplyImpulse() in order to imitate a dynamic rigidbody collision between the two bodies? I've tried a few things that almost work (calculate the change in momentum at the collision point, using "point velocities" and "effective masses" at that point, and use that as impulse), but the results always seem slightly off compared to an equivalent scenario solved by the physics engine.
    Last edited: Jun 22, 2022
    Occuros likes this.
  2. gtzpower


    Jan 23, 2011
    I don't know the answer to your question, but I do wonder whether you can get the same result as the physics engine when you are (presumably) trying to compute and apply an impulse in a single step while comparing it to what the physics engine does (again presumably) over a number of steps via solver iterations?
  3. PhilSA


    Jul 11, 2013
    I think you're right; it probably won't be exactly the same

    However, I've done some research (talks & papers on physics engines) since yesterday and I now see that my current approach to calculating impulses isn't totally correct, which is the main reason why my results differ. I'll be working on implementing a more physically correct approach and I think the results will be sufficiently similar once that's done
    hippocoder and Occuros like this.
  4. gtzpower


    Jan 23, 2011
    I'd love to hear a TLDR on how you achieved this when you get it (if you don't mind sharing).
    Edy, Occuros and PhilSA like this.
  5. PhilSA


    Jul 11, 2013
    I think I've got it now:

    Code (CSharp):
    2. public static void SolveCollisionImpulses(
    3.     in PhysicsVelocity physicsVelA,
    4.     in PhysicsVelocity physicsVelB,
    5.     in PhysicsMass physicsMassA,
    6.     in PhysicsMass physicsMassB,
    7.     in RigidTransform transformA,
    8.     in RigidTransform transformB,
    9.     float3 collisionPoint,
    10.     float3 collisionNormalBToA,
    11.     out float3 impulseOnA,
    12.     out float3 impulseOnB)
    13. {
    14.     impulseOnA = default;
    15.     impulseOnB = default;
    17.     Translation translationA = new Translation { Value = transformA.pos };
    18.     Translation translationB = new Translation { Value = transformB.pos };
    19.     Rotation rotationA = new Rotation { Value = transformA.rot };
    20.     Rotation rotationB = new Rotation { Value = transformB.rot };
    22.     float3 pointVelocityA = physicsVelA.GetLinearVelocity(physicsMassA, translationA, rotationA, collisionPoint);
    23.     float3 pointVelocityB = physicsVelB.GetLinearVelocity(physicsMassB, translationB, rotationB, collisionPoint);
    25.     float3 centerOfMassA = physicsMassA.GetCenterOfMassWorldSpace(translationA, rotationA);
    26.     float3 centerOfMassB = physicsMassA.GetCenterOfMassWorldSpace(translationB, rotationB);
    27.     float3 centerOfMassAToPoint = collisionPoint - centerOfMassA;
    28.     float3 centerOfMassBToPoint = collisionPoint - centerOfMassB;
    30.     float3 relativeVelocityAToB = pointVelocityB - pointVelocityA;
    31.     float relativeVelocityOnNormal =, collisionNormalBToA);
    33.     float3 crossA = math.cross(centerOfMassAToPoint, collisionNormalBToA);
    34.     float3 crossB = math.cross(collisionNormalBToA, centerOfMassBToPoint);
    35.     float3 angularA = math.mul(new Math.MTransform(transformA).InverseRotation, crossA).xyz;
    36.     float3 angularB = math.mul(new Math.MTransform(transformB).InverseRotation, crossB).xyz;
    37.     float3 temp = angularA * angularA * physicsMassA.InverseInertia + angularB * angularB * physicsMassB.InverseInertia;
    38.     float invEffectiveMass = temp.x + temp.y + temp.z + (physicsMassA.InverseMass + physicsMassB.InverseMass);
    40.     if (invEffectiveMass > 0f)
    41.     {
    42.         float effectiveMass = 1f / invEffectiveMass;
    44.         float impulseScale = -relativeVelocityOnNormal * effectiveMass;
    45.         float3 totalImpulse = collisionNormalBToA * impulseScale;
    47.         impulseOnA = -totalImpulse;
    48.         impulseOnB = totalImpulse;
    49.     }
    50. }
    "SolveCollisionImpulses" takes some PhysicsVelocity and PhysicsMass as parameters, but if one of your objects doesn't have one of these components, you can just create a PhysicsVel/Mass on the fly with all the right properties, and pass them to the function. It'll return the impulses that would resolve the collision. If you need to apply the resulting impulse to an object that doesn't have a PhysicsVelocity, you can just look at what the "ApplyImpulse()" function does in the source code of Unity.Physics, and immitate that for that object

    One limitation to keep in mind with this is that it won't give physically-accurate results when there are more than just 2 bodies involved (ex: you have a seesaw with a block on one side and a character that jumps onto the other side). The impulse calculated by this between the character and the seesaw won't take into account the counter-weight of the block on the other side, so the total effect of the impulse will appear weaker than it should be. The only real way to deal with that kind of scenario is with a complete physics solver (what solves systems of rigidbodies in a typical physics engine)
    Last edited: Jun 30, 2022
    Luxxuor likes this.