Here is the deal:
Box2D joints are only aware of bodies that are connected to them. So because of that a chain of joints and bodies of different weight will be behaving non pleasurably.
So i've figured out the to solve that i have to consider other joints connected to a body.
And i've created a new type of joint that has pointer to neighbour joints (prev and next) every joint in the chain ( *--*--*--* ) knows about 2 joints. (restriction: only to joints of this type could be connected to body).
In the SolvePositionConstraints() i'm calling recursive function that iterates through the connected joints and decides if a body's position can be manipulated by that joint.
Here's code for chain behind the next joint:
Code: Select all
bool b2RopeJointMassAware::SolvePositionConstraintsNA(const b2SolverData& data,b2Vec2 & dt, float & mass)
b2Vec2 cA = data.positions[m_indexA].c;
b2Vec2 cB = data.positions[m_indexB].c+dt;
b2Vec2 u = cB - cA ;
float32 length = u.Normalize();
float32 C = length - m_maxLength;
C = b2Clamp(C, 0.0f, .2f);
float t_mass=(mass>0.f && m_invMassB>0.f)?(mass+m_invMassB):0;
if ( !(m_invMassA > 0.f))
b2Vec2 tcA = dt.Length() * u;
mass=(mass>0.f && m_invMassB>0.f)?(mass+m_invMassB):0;
that was the best what I could think of.
In the result the joints are not stretching any more, but bodies are shaking unpleasantly. So the code doesn't works.
May by velocities have to be calculated also like this. I'm kinda bad at mechanics.
Has anyone any idea about that?
Next idea i have is to create a special joint that will connect N bodies. But still I have to calculate properly velocities and positions for those bodies. And I cant fire that out.