schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 330 forks source link

[Question] How to prevent intersecting bodies shoot each other into space? #329

Closed Fxlr8 closed 5 years ago

Fxlr8 commented 6 years ago

If I spawn one circle body over another, they start to accelerate in opposite directions to prevent the overlap. If the overlap is big, objects accelerate so much, that they fly away from each other. Is there a way to gently push one object out of another so that they come at rest when they stop intersecting?

schteppe commented 6 years ago

Don’t think it’s possible using the current contact solver. The solver will always add velocity to separate objects. Other solver algorithms may work better - solving positions and velocities separately would probably fix it..

If you don’t want to write a new contact solver (totally understandable), then you could separate the objects manually:

  1. Spawn the new body
  2. On every update: 2.1. Disable all equations (contact and friction equations) between the new body and all other bodies 2.2. Get one of the contact normals and move the new body a small distance along this normal direction 2.3. Stop when there are no contacts involving the new body

What do you think?

Fxlr8 commented 6 years ago

Thanks! Looks like a legit workaround for me. I'll give it a try