views:

161

answers:

1

I'm writing a physics engine that uses Verlet integration, and i can't get some constraints to work right. Some (like a bond/weld constraint) are extra "soggy" and aren't stiff enough, while others (like an area constraint) are extra bouncy and send atoms flying. The code for my update ethod in my physics simulator is like this:

    ProcessRemovedItems();
    ProcessAddedItems();
    _colliderManager.Update(timestepSize);
    foreach (Atom atom in _atomList)
    {
        atom.Update(timestepSize);
    }
    for (int i = 0; i < _iterations; i++)
    {
        foreach (IConstraint constraint in _constraintList)
        {
            constraint.Update();
        }
    }

I've tried all idfferent combinations of update orders and none help. I have a vague idea of something about using iterations but have no clue what else would help. Any suggestions?

+3  A: 

You are correct in your assumptions of iterations.

By running the verlet integrator part multiple times in a frame, the stiffer and better the simulation works).

This is due to the following: say you have 5 atoms next to each other, and when the last atom gets integrated, it is moved a bit. unfortunately it is moved i to the atom next to it. It takes a complete frame, before the integration is done again and this is fixed (due to the constraints). How ever, chances are, the atoms again are placed inside the others.

so to counter this effect, the more iterations you do (so the more times per frame you call the integrator), the better the simulation will work and conform to your constraints,

however, you'll notice that the more you run the integrator per frame, the more processor power it will use. So there is a sweet spot somewhere which you have to find manually

Toad
if i call each atom's update method 5 times with 1/5th the timestep each frame then it will feel much stiffer?
RCIX
I've updated my code but now it didnt really help and blows up after a couple of seconds. No clue why...
RCIX
Everywhere there's an update method (constraints, the collider manager) i've added the timestep as a factor which i multiply the force by before applying any forces to atoms, and i get the problem above. :confused:
RCIX
you are right....my bad. I checked my sources, and it is ONLY the constraints which have to be done with multiple iterations. Running the integrator multiple times would make the simulation go way too fast of course.
Toad
The idea is still the same as I described.... you run the constraints multiple times, since resolving one constraint might cause another to go wrong... so you have to recheck again.
Toad
One word of advice with the constraints. Make sure you move the proper elements in the proper direction (inrelation to the mass they have). So given that 2 atoms of similar mass bump, then resolve the constraint by moving them in opposite directions equally until they don't bump anymore.
Toad
Thanks, ill see if all of that helps, get back to you soon!
RCIX
Hmm. It's odd, what happens is the simulation runs kind of slowly, and the constraint's still don't work. See my updated code. I want to thank you for all of your help so far though!
RCIX
why would your constraints code need a timestep? The constraints work always the same. e.g: atom is in the wall, then move the atom as much pixels out of the wall needed so the constraint is satisfied. In case of atoms intercolliding, use the line from the center of atom to the center of atom2 as the direction you move the atoms out of eachother,
Toad
ohhh..... I think i see. I'll work on my code some then, i think i know what needs to be fixed.
RCIX