Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

DistanceJoint2D claims that distance should not vary yet it stretches?

Discussion in '2D' started by hippocoder, Apr 9, 2014.

  1. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    CharlieSamways likes this.
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Solved by bumping up Position Iterations (thanks Trent)
     
  3. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    This is actually a known bug that will be fixed in the next major release.
     
  4. pengo

    pengo

    Joined:
    Jun 3, 2013
    Posts:
    5
    It wasn't fixed in the recent Unity 4.5 release. I'm having the same issue still after upgrading.

    I've worked around the issue by reducing the weight of all my objects (e.g. to 0.00001) so the strain of gravity is reduced, and also by increasing the Position Iterations.

    I originally had my game in ActionScript 3 using a port of Box2D and never had the issue of joints stretching under gravity, so I don't know if Box2D has changed over the years since then or if it's the Unity port, but it still seems to have this issue.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,417
    To start, the DistanceJoint2D doesn't claim that the distance should not vary, it only "claims" that it will try to constrain to that distance. Whatever simulation you get, it's Box2D and not Unity. Most of Box2Ds joints are "soft" in that you can break the constraint temporarily.

    The DistanceJoint2D component uses one of two Box2D joints. When not in "Max Distance Only" mode, it uses Box2Ds distance-joint with a frequency of zero and damping of one so it's pretty rigid but Box2D doesn't guarantee that it will always be at that distance, just that it will try (given the number of iterations and forces applied) to meet that constraint.

    It may be useful to know here that if this "rigid" set-up of the distance-joint doesn't suit you then you can always use the SpringJoint2D joint with your own configuration as that uses Box2Ds distance-joint but allows you to specify the freq/damping.

    When you set the DistanceJoint2D component to be "Max Distance Only", it instead uses Box2Ds rope-joint which ensures that the distance never exceeds a certain distance. This is a much "harder" joint and tends to stay within the constraint under fairly extreme conditions. Typically it is users to set the overall length of a chain of joints to prevent "stretching". This is the only change from 4.3 to 4.5 for this joint component.

    It's not difficult to break constraints of joints (stretching as you put it) in Box2D though. We essentially offer a wrapper to Box2D, we don't actually perform the simulation so If you take Box2D as it current is, you'll get similar results.

    The Box2D manual also provides a caution about joints in general saying:

    Chains of bodies connected by joints may stretch if a lighter body is supporting a heavier body. For example, a wrecking ball connect to a chain of light weight bodies may not be stable. Stability degrades as the mass ratio passes 10:1.

    Hope this helps.
     
    pengo likes this.