Categories
Development Game Creation

Hinge joints chain movement (2D)

I have some problems with hinge joint chain movement calculations…

Here is my algorithm for segments location/rotation adjustments after a previous segment update (C#):

void AdjustLocation()
{
    float halfWidth = size.Width / 2;

    // Translations
    Vector parentBack = PrevSegment.Center - (halfWidth * new Vector(Cos(PrevSegment.Rotation), Sin(PrevSegment.Rotation))); // Green on the pic
    Vector front = this.Center + (halfWidth * new Vector(Cos(this.Rotation), Sin(this.Rotation))); // Blue on the pic

    // Const spacing between segments
    float spacing = *some_value*;

    Vector dv = parentBack - front;
    float k = spacing / dv.Length;

    // New front location
    front = parentBack - (dv * k);
    // New rotation
    dv = front - this.Center;
    this.Rotation = Atan2(dv.Y, dv.X);

    // Translate back to the center
    this.Center = front - (halfWidth * new Vector(Cos(this.Rotation), Sin(this.Rotation)));

    NextSegment?.AdjustLocation();
}

It’s working (partially), but it seems I’m missing something as it doesn’t behave like a chain. Segments always move one towards another although they should not always do this. Also overlapped sections of a long chain form a “knot” going all the way to the end of the chain before its “untangling”.
What am I missing?

Btw: everything screws up with negative spacing values.

P.S. Tried to visualize the algorithm on the picture:

Leave a Reply

Your email address will not be published. Required fields are marked *