Rope physics with position constraints question

Please don't post Bullet support questions here, use the above forums instead.
Post Reply
vitaminD
Posts: 3
Joined: Mon Apr 08, 2013 4:50 pm

Rope physics with position constraints question

Post by vitaminD »

I apologize in advance for asking such a beginner question - I know almost nothing about physics but I couldn't find a better place to ask.

I have a simple rope made up of mass-spring system with pretty high spring constant (~5000). I'm using semi-implicit Euler integration with timestep of 0.003 seconds. It looks pretty good, but it feels like the timesteps have to be excessively small because I'm doing something stupid.
To make the 2 ends of the rope constrained to a moving object (the rope is the only thing driven with physics, not the object), I'm simply setting the positions of the 2 ends of the rope to be on the object surface at every timestep. I recently became aware of existence of "constraint solvers" (eg Lagrange multipliers) but it's way over my head and I don't understand what they are or what they do.

So here's my question: is what I'm doing (just forcing 2 end positions to whatever I want) a valid solution? Or is there something better that would allow me to increase timestep? Do I need to look into a "constraint solver", or does that only apply to rigid bodies and not to mass-spring system?
bone
Posts: 231
Joined: Tue Feb 20, 2007 4:56 pm

Re: Rope physics with position constraints question

Post by bone »

If it looks good and the CPU usage isn't causing any problems, I'd say you are done.

That being said, yes this could be solved more efficiently with a lot of work (note that I personally don't even consider a timestep of 0.003 seconds to be particularly small; presumably you'd never even go above the time it takes to render a frame anyway, for example 0.017 seconds on a 60Hz display). Yes, a constraint solver would work perfectly fine for a mass-spring system, one can even design such a system to handle both point masses and rigid bodies. Different solver methods could change the efficiency and how stiff you could make your springs.

What you've done with forcing 2 end positions is actually the start of a slightly different kind of solver - Matthias Muller's position-based dynamics. This is more simple than a full-on velocity-based dynamics solver (which most of the current tech uses), and may be perfect for what you are doing. But some of its drawbacks may not be - IIRC, it can't act "springy".
vitaminD
Posts: 3
Joined: Mon Apr 08, 2013 4:50 pm

Re: Rope physics with position constraints question

Post by vitaminD »

Thank you. That's reassuring. I have a question about using constraint solvers on a mass-spring system though - at high level, what is it that makes it more efficient than forcing end positions? Is the solver trying to figure out velocities for multiple points at once (throughout the whole mass-spring chain, sort of like fully implicit integration), instead of running multiple iterations for the spring forces to "propagate?"
That's the only thing I can think of, but then my imagination is limited due to my lack of knowledge on the subject...
c0der
Posts: 74
Joined: Sun Jul 08, 2012 11:32 am

Re: Rope physics with position constraints question

Post by c0der »

In one of Erin Catto's GDC articles, he says that springs (F = kx) are difficult to tune in practice, so he uses soft constraints instead based on the harmonic oscillator.

For me, the difference is that when you have multiple objects connected, eg. a suspension bridge with several planks (see Box2D), some constraints affect others. Also the constraint solver is generic, you can derive an equation for different constraints (prismatic, ropes, line, hinge etc) and solve them in the same manner, handle numeric instabilities by adding simple stabilisation terms to the equations and so on.

It's not that difficult once you get into it, you just need a bit of knowledge on dynamics and calculus

Here is a derivation for the solver, which can be used for 3D, but there are 2D constraint derivations on the site

http://www.codezealot.org/archives/191
vitaminD
Posts: 3
Joined: Mon Apr 08, 2013 4:50 pm

Re: Rope physics with position constraints question

Post by vitaminD »

Thank you. Your link led me to Erin Catto's GDC slides which are very educational,
however now I'm confused as hell. In his 2009 GDC archive, it contains example code with symplectic (ie semi-implicit) euler integration of spring force, and it's not what I understand as semi-implicit at all!

I thought that semi-implicit is supposed to be exactly the same as implicit, except instead of solving for future spring forces with future position, it approximates the future position with timestep * current velocity. And, same as implicit, the spring force's Jacobian (simply spring constant in 1d zero-rest case) gets multiplied by approximated position and added to total force.

edit: oops, when I say future position, i meant change in position.
edit2: to further clarify, this change in position isn't used to get the new position, but rather to approximate the new force.

Here's my modification to the example code:

Code: Select all

	state2->t = state1->t + h;
	state2->x0 = state1->x;
	float acceleration = -s * state1->x + -s * h * state1->v;
	state2->v = state1->v + h * acceleration;
	state2->x = state1->x + h * state2->v;
And the result (Red line is my change. Green is fully implicit, and Blue is example code's symplectic euler):

Image

As you can see, my code behaves more or less like implicit with low spring constant - which is what I expected.
However with higher spring constant it blows up, just like explicit euler, which makes me wonder why I would bother with semi-implicit at all.

And by now I hope you can see my confusion. First of all, who's right? Is Erin's example mislabeled, or am I mistaken in assuming that semi-implicit is synonymous with symplectic? Or am I not understanding what semi-implicit is? If semi-implicit behaves similarly to implicit but without the stability (which is its primary benefit) what exactly is its use? It seems like a lot of trouble for nothing (those Jacobians are painful!). Please somebody help me understand!


P.S.
Sorry about dumping more text.
Here's a paper that explains my reasoning well:
http://njoubert.com/teaching/cs184_sp09 ... lation.pdf
(end of page 9 and beginning of page 10)
Image

because computing positions via spring force isn't a first-order differential equation, I basically took the equation from the following paper:
http://www.cimat.mx/~cesteves/cursos/an ... lation.pdf
Image
(eq 5 from page 46)

and changed the top equation from
deltaX = h(v0 + deltaV)
to simply
deltaX = h(v0)
So the left side of equation 6 from the paper is reduced to simply deltaV (change of force wrt velocity is nothing because spring forces don't depend on it) and there's no need to do the whole linear solve thing, except there's still the Jacobian on the right side. So basically the same equation as the first paper, really.
Image

I can't understand where I went wrong. Obviously Erin's example code is far superior because it's closer to reference and it's more stable (hard to argue with the result!) but I don't understand how I ended up with a completely different formula.
Post Reply