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:
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.
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?
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
“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)