Request: Prioritized (Contact) Solver sub-step

I think there are many situations where it would be beneficial to have access to a contact impulse before completing all solver iterations.

In particular, implementing breakage is hindered by this situation: the breakage must occur before the velocity solver iterations, but the calculation of contact impulse is dependent on the result of those solver iterations.

The only thing we can do at present is to let the simulation step complete to get the impulse value, and retroactively apply changes in velocity, position, and geometry due to breakage.

This means that in order to get correct behavior it may be necessary to run two entire simulation steps. If the first one yields impulses that trigger events, the events are applied, the world is rewinded and stepped again, this time producing the desired results. This is bad because it requires the storing and resetting of the state of all bodies.

To address this I am proposing a lightweight solver sub-step which is designed to provide an estimate of impulse data for the purposes of determining breakage and similar effects. Bodies which have been previously flagged will then receive "pre-solve" impulses. The benefit of having this is that the entire rest of the simulation step (broadphase, collisions) is not wasted. Basically what I'm looking for is some method to obtain the impulses without incurring the full cost of a step.
I would love to allow destruction to happen in one time step and get correct follow through.

Here's what we want:
- car hits static wall
- some code breaks the wall into dynamic pieces
- car continues through pushing the broken pieces
- awesome!

Unfortunately what really happens is:
- car hits static wall and stops
- some code breaks the wall into dynamic pieces
- car is still stopped
- yawn 🙁

This is a difficult problem! There are many open issues:
- does the solver know how to break bodies or does that go to client code?
- what are good criteria for breaking?
- how would the engine communicate the new/destroyed bodies to the client?
- is this fully recursive, allowing multiple fractures? solve-break-solve-break-...?

dc443 wrote:
To address this I am proposing a lightweight solver sub-step which is designed to provide an estimate of impulse data for the purposes of determining breakage and similar effects. Bodies which have been previously flagged will then receive "pre-solve" impulses. The benefit of having this is that the entire rest of the simulation step (broadphase, collisions) is not wasted. Basically what I'm looking for is some method to obtain the impulses without incurring the full cost of a step.


For impulses to be transmitted correctly we need to consider the whole constraint island. So I don't see a way to make this light-weight. Handling destruction correctly seems like a very heavy-weight problem that would require extensive changes to the engine. I'd like to do this, but I would consider this to be a research project, not something with a clear solution.

Many games just cheat to solve this problem. So for the car, the game would just store the car's velocity. If the car hits something that breaks, the car's velocity is reset to the previous value. You will see a small hitch in movement at the time of collision, but usually this is acceptable.
Currently I'm pretty sure that it's possible to get a completely correct response if two entire steps are performed, by using the method I described before of using the first step for determining impulse values, and if breakage occurs the scene is reset and the step is repeated with the new geometry. This way, an extra Box2D simulation step will be able to correctly resolve the issues and eliminate the "hitch".

This really isn't too bad, I just wish there was an interface for avoiding doing this.

You are certainly correct that determining the actual breakage geometry is a highly involved problem. I have been working on this over the last several weeks. I came up with a linear finite element solver which determines an internal stress distribution. My plan is to use this to generate breakage geometry to pass to Box2D before the impulses are applied:

Box2d Step produces impulse value (input to breakage solver)
breakage solver produces either a value of false ("box2d: carry on as usual") or breakage geometry ("Here are the resulting broken pieces, box2d")
The problem is that at this point box2d has already updated the rest of the world to a state which assumes that the broken object remained rigid.

So at least in my case this is the nature of the situation. I would say that calculating the breakage is not a task appropriate for Box2D to handle, there is just too much stuff to be dealt with (for example I need to be constructing a finite element mesh and solve a n=2*nodes linear system of equations)...

You do bring up something important that I haven't spent a lot of time thinking about yet with the recursiveness: I am pretty sure that limiting to one breakage event per step is capable of producing good looking results so may be a good first step. But it's true that I'm going to also want Time of Impact impulses if a bullet body hits a breakable body.

It does seem like there's no way around solving the islands. Are there other steps that occur before that step that would be redundant? I'm pretty confident that my method of performing two steps should work okay because objects do not move apart very far, so hopefully resetting positions will not incur too much thrashing in the broadphase.
How about if PostSolve was called before the collision response was actually applied, and its b2ContactImpulse parameter had read-write impulse values for each body. For example in the car scenario above you could set the impulses for the car body to nothing (or maybe just scale them down if the impact should actually cause the car to slow a little), and the impulses for the wall body to nothing and mark it for breaking after the world step finishes. Then in the next time step the still moving car could continue to push through the pieces. It would be similar to the way a contact can be disabled in PreSolve, except you would have more information to use in decision-making and it would not be such an all-or-nothing adjustment.

Even if the collision doesn't break anything, there are other cases where this could be useful. A while ago I tried to take the values from PostSolve and use them to nullify one side of a collision response, specifically I wanted to check if an arrow hit a hanging target with enough energy to stick in, and I wanted the impulse to be applied to the target so that it swung, but not to the arrow because I didn't want it to bounce back out.

I agree that the details of the breaking procedure itself should be left to the user.

Edit: ah, I forgot we were talking about TOI sub-steps... I guess this doesn't help with that.
The problem with not rewinding after obtaining impulses is that the passage of time is now different for the broken object while everything else continues. The broken object is "frozen" in space for an entire timestep. It would be necessary to integrate the positions of the newly broken pieces by a timestep before placing them back into the world.

Depending on the application, that momentary freeze could be completely fine. It would be quite awesome to see an object frozen in its tracks as it progressively decomposes on an impact, similar to the way many fighting games freeze characters on purpose when they get hit, before they start moving back (I'm thinking of super smash bros. The duration of the freeze seems to provide a bit of feedback to convey the magnitude of damage), but the goal for our physics engine is always to make the most convincing and physically realistic simulation possible.

I thought about it some more and I might be wrong about things being "correct" even with re-doing an entire simulation step, because I neglected to consider TOI events. The correct behavior to handle this is that the breakage must occur prior to encountering the next TOI event. By using a step rewind method, all TOI events that occur over a timestep have already completed by the time I get access to the impulses. Breaking the object on the initial impact could potentially change the sequence of events from that point forward, which could potentially invalidate all the remaining impulse values! It would be nearly impossible to deal with this without a way to modify the world in the middle of a step.

This is a stronger argument for the need to have an interface to "step in" to modify geometry for breakage when impulses are generated. i'll need Erin or somebody more knowledgeable than me of how the engine works to show me how to modify it so that we can modify the world in the middle of a step (immediately after a CCD event). My understanding is that the regular (non CCD) contact impulses are generated at the very end of the step, after contact constraints have been solved, so this can be treated as just another potential impact event.

I think my per-step impulse method could still produce good results for low timesteps so long as I accumulate all the impulses, but if velocities are on the scale that many CCD bouncings can occur within a timestep then it will be unable to take the sequential order of those events into account.

Original thread: https://box2d.org/forum/viewtopic.php?t=8034