basic questions about spring and joints in general

I’m trying to connect two rigid bodies with a spring joint in between them and have some question. Mind you, even though I’m trying specifically with a spring at the moment, some of these questions are really about joints in Unity in general:

  1. Is it possible to model a spring
    with two ends and one body on each
    end and have the 2 ends behave the same with respect to each other?

    For instance, in Apple’s SpriteKit,
    which under the hood uses box2d, a
    spring joint connects one body with
    another (it has physicsBodyA and
    physicsBodyB properties and
    anchorA and anchorB properties).
    That is, both bodies and anchor
    points are at the same hierarchical
    level. In Unity however, I’m
    confused about the hierarchy and the
    ConnectedBody vs Anchor
    implication. It seems as if at some
    point, a joint is a one-way
    relationship: the body you add the
    spring to becomes the “free” end of
    the spring and the ConnectedBody
    property, even if referencing
    another body, does not allow this
    other body to move in the same way
    the parent of the joint component
    moves.

    The best example I can think of is
    this (assume A and B are rigid
    bodies with say, box colliders).

    • A has SpringJoint component
    • SpringJoint.ConnectedBody = B

    Rotating A around B will cause B to
    rotate about its center as if
    “watching” B, which I’d expect.
    However, attempting to rotate B
    around A does not make A to rotate
    in place but instead to start
    strafing towards B until it ill end
    (for an attempt at rotating 90 degs
    for instance) almost horizontally
    aligned with B. Maybe this drawing
    helps:

    I wonder if it might have something to do with Unity’s definition of a spring having a “fixed” end? But if so, would I need to coordinate two spring joints (from A to B and B to A) to get the effect of a single spring with two “free” ends?

  2. Is it possible to have bodies connected thru a joint to still
    collide and repel each other? If
    not, is there a sensible way to fake
    this?

    I’ve done some initial testing and
    it doesn’t seem like it is. For
    example, if A has a joint and that
    joint’s ConnectedBody is B, from
    that point onwards A and B can
    intersect each other, which kind of
    sucks… cause I’m trying to create
    a sort of physics-affected graph:
    nodes connected by edges (which
    would behave like springs for
    instance) and connected nodes should
    still not intersect each other on
    colliding.

  3. What determines the distance between the two bodies joined by a
    joint?
    First I thought that their
    distance at the time of creating the
    joint was what would get set as the
    joint length if you will (though
    this might only make sense if I
    think about the joint itself as an
    elastic rod or spring?). Then I
    thought the min/max distances of the
    spring joint or the linear limits in
    the configurable joint would set
    this. But I get strange results. Say
    I want A to be joined to B by a
    spring and the distance between A
    and B (the spring length if you
    will) be 10 units. How do I
    establish the 10 unit distance?

  4. Is it possible to change at runtime the length of the joint /
    distance between two joined
    bodies?
    Following on the previous
    example, say at runtime I want to
    change the distance between A and B
    from 10 to 5. How would I go about
    it?

These are just guessing from playing with joints. I assume they may have been imported from Phys-X, so a better explanation is there:

  1. Check the connected position. The ConnectedRigidbody always counts as having the spring connected to the center, so pulling gives smooth rotations. The object with the spring has an adjustable position – sometimes it defaults to one end of the object. Lots of joint oddness is fixed by adjusting the position.

  2. Springs need rigidbodies, but not colliders. Never tried this, but the collision layer matrix might allow springs to work, but allow colliders to pass-through (but would need to be clever with the layers.) Or it might say “diff layers, no interaction, no spring.”

3,4) Best as I can tell, “spring length” is determined by initial placement and the settings. Unattaching the joint, moving the object, then reattaching does work in some (possibly all) cases.