Torsion spring for revolution joints

I'd like to simulate a torsion spring for revolution joints. I inserted this code just prior to the "// Solve motor constraint." portion of the class void b2RevoluteJoint::SolveVelocityConstraints(const b2TimeStep& step) {...} in b2RevoluteJoint.cpp.


if (m_enableTorsionSpring)
{
float32 jointAngle = b2->m_sweep.a - b1->m_sweep.a - m_referenceAngle;

float32 vimpulse = jointAngle * m_kSpring;

w1 += i1 * vimpulse;
w2 -= i2 * vimpulse;
}


I tested this with a chain of o2dBox segments connected with the modified joint. It seems to work pretty well when the adjoined segments are "large enough" (on the order of a meter) or the spring constant is small enough. However, when the segments become small or the spring constant too large, the joint becomes unstable very quickly, and the segments start flying across the screen wildly.

Has anyone implemented something similar? Is there a better way to do this?

Thanks!
Here are the formulas we used in our game (first 2 snippets) : viewtopic.php?f=3&t=1007&hilit=spring+joint

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