Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ConfigurableJoint Spring Dampening Parameter Broken

Discussion in 'Physics' started by Tinus, Mar 9, 2015.

  1. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    After upgrading to Unity 5, Volo Airsport plays like this:



    Which is funny, but not what we're going for. :)

    Our character's body is completely animated using ConfigurableJoints, and we expected some things to break after upgrading. The Unity 5 upgrade guide mentions we'd likely need to tweak spring and dampening parameters, so we did.

    Problem: As it stands though, we cannot get the dampening parameters on the motors to do anything. We can use both XYZ and Slerp drive modes to exert a spring force, but the associated dampening settings don't seem to be applied at all.

    Repro: Make a scene with two rigidbodies in it, link them with a single ConfigurableJoint. Enable either XYZ or Slerp motors and try changing the dampening settings.

    Other joints (such as HingeJoint) do not have this problem.

    Has anyone else experienced this?
     
    TahmimMiah and Zergling103 like this.
  2. Matkins

    Matkins

    Joined:
    Aug 24, 2009
    Posts:
    152
    I'm also using ConfigurableJoints target angular velocity in XYZ mode, and I can't get it to move at all regardless of what values I use. I want to know if this is a bug or if i'm missing something.

    Elsewhere I'm also using ConfigurableJoints with target rotation, whilst these do move they over shoot the target wildly. Perhaps these ones are fixable though, I wasn't aware of an upgrade guide until now.
     
    TahmimMiah likes this.
  3. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    @Matkins: Oh, that might be related to another issue I ran into: Rigidbodies are not awakened by motor force. If the body is sleeping, no amount of motor activity will wake it up, and it will remain motionless.

    Calling Rigidbody.WakeUp() every frame fixed all the non-responsive-motor issues I was having today.

    Try that and let me know whether that works. Really curious to hear if you can get the motors to work, including dampening.
     
    Last edited: Mar 9, 2015
    TahmimMiah likes this.
  4. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    I tried writing my own dampening logic with plain euler integration. It works, but the higher the dampening force and the larger the physics timestep, the more unwanted oscillation the system exhibits. Here's the relevant code snippets, in case anyone wants to try something similar:

    Code (csharp):
    1. private void ApplyAngularDampening() {
    2.         Quaternion localRotation = Quaternion.Inverse(_joint.connectedBody.rotation) * _transform.rotation;
    3.         Quaternion rotationDelta = Quaternion.Inverse(_lastLocalRotation)*localRotation;
    4.         _lastLocalRotation = localRotation;
    5.  
    6.         Vector3 axis;
    7.         float angle;
    8.         rotationDelta.ToAngleAxis(out angle, out axis);
    9.  
    10.         Vector3 angularVelocity = axis*(angle*Mathf.Deg2Rad/Time.fixedDeltaTime);
    11.  
    12.         _jointBody.AddTorqueAtPosition(
    13.             _transform.TransformDirection(angularVelocity * -_joint.slerpDrive.positionDamper),
    14.             _transform.TransformPoint(_joint.anchor), ForceMode.Acceleration);
    15.     }
    16.  
    17. public static void AddTorqueAtPosition(this Rigidbody me, Vector3 torque, Vector3 position, ForceMode forceMode) {
    18.         Vector3 torqueAxis = torque.normalized;
    19.         Vector3 hinge = Vector3.right;
    20.         if ((torqueAxis - hinge).sqrMagnitude < float.Epsilon) { // Chosen hinge cannot be the same as torque axis
    21.             hinge = Vector3.up;
    22.         }
    23.         Vector3.OrthoNormalize(ref torqueAxis, ref hinge); // Make hinge orthogonal to torque axis
    24.         AddTorqueAtPosition(me, torque, hinge, position, forceMode);
    25.     }
    26.  
    27. public static void AddTorqueAtPosition(this Rigidbody me, Vector3 torque, Vector3 hinge, Vector3 position, ForceMode forceMode) {
    28.         Vector3 force = Vector3.Cross(0.5f * torque, hinge);
    29.         me.AddForceAtPosition(force, position + hinge, forceMode);
    30.         me.AddForceAtPosition(-force, position - hinge, forceMode);
    31.     }
    32.  
    So next I'll try to achieve more stable dampening by using joint.targetAngularVelocity.
     
    TahmimMiah likes this.
  5. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Oh hey, it appears the dampening parameters work, but only when I use the PositionAndVelocity drive mode. I never used that before, but it looks like I can get it to work.

    Now to calculate appropriate target velocities that try to reach their target positions.

    Edit: I now have working ConfigurableJoints with XYZ drive that use both the spring force and spring dampening parameters.

    The thing is, I have no clue what caused it to work! Over the last couple of days I have changed and/or replaced the behavior of pretty much all parameters on the joints, but nothing seemed to work.

    I am now back where I started with the configurations, but now I suddenly have working dampening.

    Perhaps ConfigurableJoint has some hidden internal state that got broken during the U5 upgrade, but has now been overridden by some magic combination of my changes.
     
    Last edited: Mar 10, 2015
    TahmimMiah likes this.
  6. Kavorka

    Kavorka

    Joined:
    Sep 15, 2012
    Posts:
    247
    To bad you do not know what you did to fix it. I also see that the configurable joints spring damper is defect, I have no joint damping. I use only slerp position drive. Did you say it works in slerp position and velocity drive?
     
    TahmimMiah likes this.
  7. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Setting the drive to use PositionAndVelocity was definitely part of getting dampening to work, yeah!
     
    TahmimMiah likes this.
  8. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    I also saw this issue when trying to move my game, Krashlander, to Unity5. I use the Configurable joint extensively to control the skier in my game. With Unity4.x everything was rock-solid. After upgrading to Unity5, I could not get things to stabilize no matter how much I tuned things.

    Guess I will need to try again and see if I can happen upon whatever you did to fix this. Hope this gets fixed in a patch release.

    Also tried recreating everything using Physics2D but that just doesn't seem to be as robust as PhysX.

    -Jeff
     
    TahmimMiah likes this.
  9. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    @Tinus or anyone else, did you ever figure out what you did to get the damping to work.

    Did you ever get it to work using the Position mode of the motor (as apposed to the position and velocity)?

    Anyone from Unity, is this bug currently being tracked? If not, let me know and I'll log it.

    Again, the bug is:

    For the ConfigurableJoint, when using the rotation motors with mode set to "Position" only, the Dampning parameter has no affect.

    -Jeff
     
    TahmimMiah likes this.
  10. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    Anybody know anything about this? I tried using "Position and Velocity". Dampning works when using Position and Velocity, but for my purposes it's not as stable as Position only which is what I was using in 4.6.x

    -Jeff
     
    TahmimMiah likes this.
  11. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
  12. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Oh, sorry for the lack of response in this thread! I didn't get any email notifications for some reason.

    At first I thought I had to set a target velocity, like you say. This might actually still be handy, but after a while I figured out I could actually leave the target velocity parameter set to (0,0,0) and just have it work for me. I'm still not completely sure this is the way I want it to work, as the target velocity parameter might be resisting some motion now.

    What I haven't yet tried is to drive the target velocity parameter based on the difference between the current orientation vs. the target orientation. I've already written code that figures out the joint's angular velocity (to apply my own dampening effect), and that could be used to determine a velocity needed to reach any target orientation:

    Code (csharp):
    1.  
    2. // This snippet calculates the difference between last frame's orientation and the current orientation
    3. // By plugging in current orientation and target orientation instead you can find a good target velocity to set
    4.  
    5. Quaternion localRotation = Quaternion.Inverse(_joint.connectedBody.rotation) * _transform.rotation;
    6. Quaternion rotationDelta = Quaternion.Inverse(_lastLocalRotation)*localRotation;
    7. _lastLocalRotation = localRotation; // update member of the class this code is used in
    8.  
    9. Vector3 axis;
    10. float angle;
    11. rotationDelta.ToAngleAxis(out angle, out axis);
    12.  
    13. Vector3 angularVelocity = axis*(angle*Mathf.Deg2Rad/Time.fixedDeltaTime);
    This would need to be modified to compare the targetRotation and currentRotation in the same coordinate space. I'll try it later this week. :)

    But again, you should be able to get your joints to work well by using the PositionAndVelocity drive mode and leaving targetVelocity at 0.

    Let me know if that helps!
     
    Last edited: Mar 23, 2015
  13. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    Could someone from Unity please chime in on this issue? This is a complete show stopper for me. I've been trying to work around it for over a week. I really don't want to go back to Unity 4.6 since I already paid for and love everything (except this bug) about Unity 5, but right now that seems to be the only option.

    -Jeff
     
    TahmimMiah likes this.
  14. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    An udpate:

    I submitted a demo to Unity QA that shows the dampening issue. They were able to confirm the bug and they passed it on to the devs.... Hopefully this makes it through semi-quickly.

    -Jeff
     
    TahmimMiah likes this.
  15. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Fantastic news! *crosses fingers for a quick p3 release*
     
  16. LSPressWorks

    LSPressWorks

    Joined:
    Jun 16, 2014
    Posts:
    25
    I wonder if I could/should submit what I am witnessing. NOthing has seemed to work to prevent teh model from flying off into the NaN dimension in U5. Works perfectly in 4.62....
     
  17. samAsQ

    samAsQ

    Joined:
    Jun 16, 2015
    Posts:
    7
    Did anyone get any further news on this? Both this thread and the bug report above just tail off. Did you guys find solutions or workarounds? Does dampening still not work on joints when using position mode? Thanks for an update
     
  18. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    Been a while since I toyed with the U5 configurable joint, but my take away from the last time I played with it is that some fundamental changes were made to how the dampening works. I was never able to make it as stable as what I had working in Unity 4.x for my game Krashlander.

    -Jeff