Search Unity

Configurable Joint 2D Equivalent? Possible "RopeJoint?"

Discussion in '2D' started by Jesse_Pixelsmith, Dec 9, 2013.

  1. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    This is also on Unity Answers: http://answers.unity3d.com/questions/593111/configurable-joint-2d-equivalent.html
    Just realized that there was a dedicated 2D section on the forums so I felt it worth posting here.


    I'm doing a game with similar mechanics to "Cut the Rope" - I actually have a system that works quite well - I use a Configurable Joint on each Rope Anchor, and connect it to a "Player Object", and set its linear limit to the length of the rope. I then use a Verlet method to generate a rope mesh which sags etc (but the physics are all controlled by the Config Joint).

    However, it's a 2D game, using the 3D physics (rigidbodies etc) with axis restrictions- and 4.3 dropped, I'm trying to convert my system over, so I can use 2d collides etc, and make things more efficient.

    I'm running into an issue with my rope system in that I'm not sure how to replace the configurable joint I use with a 2D equivalent.

    My first thought was to use a Slider Joint 2D, to handle the variable length, but that didn't produce good results.

    I then tried to use it in conjunction with a Hinge Joint 2D (with the hinge as the parent), which worked a bit better, and I could get a pendulum motion going, but would still have issues when the "Player Object" would fall from above, with the Hinge Joint at some angle - the Slider Joint wouldn't seem to contract, but instead the force would make the Hinge Joint rotate faster - which kind of makes physical sense if I think of a Slider which can only slide on one axis.

    The Configurable Joint actually has its angular motion locked, and just moves along X and Y (I don't completely understand this...but it works).

    Is there a 2D Joint or some combination that would allow movement along X and Y similar to a config joint, or some other method I could use to make this work?


    http://www.box2d.org/manual.html#_Toc258082974 - seems like a Box2D RopeJoint (8.13) would do the trick, but that didn't seem to get brought in?

    Here is my code to initialize the Config Joint:

    Code (csharp):
    1.  //Adding a configurable joint to the rope anchor
    2.         cj = anchorObject.AddComponent<ConfigurableJoint>();
    3.  
    4.         cj.anchor = Vector3.zero;
    5.         cj.axis = Vector3.zero;
    6.         cj.autoConfigureConnectedAnchor = false;
    7.         cj.connectedAnchor = Vector3.zero;
    8.         cj.secondaryAxis = Vector3.zero;
    9.  
    10.         cj.linearLimit = new SoftJointLimit() { limit = targetLength, bounciness = 0, damper = 0, spring = 0 };
    11.  
    12.        //We want to limit motion along the X and Y axis, to a maximum distance of "targetLength", and of course lock motion on the Z axis
    13.         cj.xMotion = ConfigurableJointMotion.Limited;
    14.         cj.yMotion = ConfigurableJointMotion.Limited;
    15.         cj.zMotion = ConfigurableJointMotion.Locked;
    16.  
    17.        //The configurable joint doesn't need to rotate, but makes no difference if it's set to Locked or Free (don't really understand why at the moment)
    18.        cj.angularXMotion = ConfigurableJointMotion.Locked;
    19.        cj.angularYMotion = ConfigurableJointMotion.Locked;
    20.        cj.angularZMotion = ConfigurableJointMotion.Locked;
    21.  
    22.        //Attach the configurable joint to the player's rigidbody
    23.        cj.connectedBody = connectedObject.rigidbody;
     
  2. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Looks like Chipmunk has this too. http://chipmunk-physics.net/release/ChipmunkLatest-API-Reference/interface_chipmunk_slide_joint.html "Slide joints hold the distance between points on two bodies between a minimum and a maximum."

    They have a unity integration but I'd rather not abandon all my colliders and unity physics etc...

    Hopefully can get something that works with Unity Box2d within 4.x. As of right now I'm forced to use expensive mesh colliders for my terrain on my mobile game etc. I guess it's just the same as before 4.3, but now with all these 2D googies I want to take part!
     
  3. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Lonely bump... sigh :)
     
  4. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Well its just a matter of waiting until the Unity guys implement all of the features of Box2D. Right now its a partial Box2D implementation in Unity.
     
  5. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Well that's what I hope, but is this on a roadmap somewhere? This is a core functionality of my game, so knowing if this feature will get brought into Unity2D or not will help me greatly in planning for it.

    I'd rather not switch all my physics completely over to Chipmunk or try and use some 3rd party Box2d port. Right now it's working with 3d PhysX so I'm thinking "if it ain't broke" but obviously 2d will be a huge performance boost on mobile. If it's on the roadmap for 4.x (and not on the same roadmap as UnityGUI...) then I'd be comfortable waiting because I won't be launching until late 2014 is my guess.

    Would just like some official Unity confirmation if possible.
     
  6. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  7. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Good catch, it's here: http://www.youtube.com/watch?v=CFT_abC4oq4#t=2508

    I heard: "No Weld joint, pully joint ,mouse joint, gear joint, wheel joint" in yet - but might be coming later on... nothing on Rope Joint :) Going to PM Tomas to see if he might be able to chime in.
     
  8. lilymontoute

    lilymontoute

    Joined:
    Feb 8, 2011
    Posts:
    1,181
  9. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    @Thinksquirrel
    Thanks. That is an awesome demo in the Web Player LOL! Question... when will you finish implementing Pully Joints?

    @Jesse
    I've recently discovered that the Unity 2D DistanceJoint actually acts like a RopeJoint... you should try it ;)
     
  10. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Does the distance joint have a min/max? I thought it was just a fixed distance?
     
  11. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    A RopeJoint only has a max. So does the DistanceJoint. A WeldJoint has a min and max. Unity has not implemented this yet.
     
  12. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Er sorry I didnt mean min/max, I meant that the distance joint keeps things a fixed distance apart, (while the Rope Joint allows movement from 0 to max distance.)

    http://docs.unity3d.com/Documentation/Components/class-DistanceJoint2D.html
    "Distance Joint 2D
    The Distance Joint 2D component allows two Sprite objects controlled by rigidbody physics to be attached together to keep them a fixed distance apart. Note that unlike the Spring Joint 2D, the Distance Joint is rigid and does not allow the distance between the objects to vary."

    Are you saying this is not the case?
     
  13. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I'm saying this is not the case. Perhaps it's a bug, but it seems like the DistanceJoint is only enforcing a max distance. Try it out and tell me what you experience.
     
  14. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Well I'll be...

    Giving it a quick test and it looks like you're right. I can't believe it but I guess I must not have tried this, just from looking at the docs, (tried the slider and hinge combination).

    I'll need to sit down and implement this into my game and see the results ... if it is a bug, I hope if they "fix" it that they will keep this code and make it a Rope Joint.

    Anyways, thank you!
     
  15. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    The current Distance Joint 2D indeed works exactly as I need. Big ups to Kurius for pointing this out! - But I imagine something will change in the future as it doesn't match the description ( which says it's always a fixed length). I imagine a toggle on the joint or a completely new joint.

    At some point I'll need a joint that does have a fixed length, I've let UT know about the issue, but for now, I can move forward.
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Yes, that is a bug which has been fixed internally. For the future, the DistanceJoint2D is indeed a fixed distance constraint. Also, a RopeJoint2D will be coming in a near future release.
     
  17. Jesse_Pixelsmith

    Jesse_Pixelsmith

    Joined:
    Nov 22, 2009
    Posts:
    296
    Sweet - well guess I won't update until I'm sure the Rope Joint is in :) Thanks!
     
  18. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    @MelvMAy.... PLEASE DO NOT release the fix for the DistanceJoint2D until you release the RopJoint2D :(
    I game needs a rope joint and right now the distance joint is serving this purpose very well.

    Honestly how hard would it be for you guys to just rename the current implementation of the DistanceJoint2D to instead be called RopeJoint2D?!

    Thanks
     
  19. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,546
    I'm guessing the RopeJoint2D they are implementing will have a bit more Rope specific features than what the DistanceJoint2D has. If the RopeJoint doesn't come the same time as the fix, I'd say to just not update then, if things are already working how you need them to.
     
  20. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Don't worry, you'll get both distance and max-distance functionality in the same final release.
     
  22. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  23. Tialis

    Tialis

    Joined:
    Nov 9, 2013
    Posts:
    4
    Hey guys, one quick question on this topic. How did you manage to use Distance & Hinge joints to create an elastic rope? (cut the rope like). Just some hints would help. I tested distance + hinge joints but the results were really weird...

    Also, do these ropes work propperly with different riggidbodies masses? Every rope system I used didn't work well with different masses...

    Some help would be greatly appreciated! ^^
     
  24. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Are you making sure you use several nodes in your rope? As a starting point, make 5 small circle bodies. Let's call them A, B, C, D, E. Attach A to the ceiling or to some static fixture via a HingeJoint. Attach B to A via a HingeJoint and DistanceJoint. Attach C to B via a HingeJoint and DistanceJoint. Attach D to C via a HingeJoint and DistanceJoint. Attach E to D via a HingeJoint and DistanceJoint. Now test it out... its probably a pretty short rope, but its elastic because of all the bodies involved.

    Remember, Unity 2D uses Box2D Physics, so all of the Box2D Physics examples on the internet should be easily adaptable to your Unity project.
     
  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    I would recommend additionally connecting the first and last joint 'node' with a distance joint set to 'Max Distance Only'. This limits the maximum overall distance the 'rope' can stretch to. When you set a distance joint to max-distance-only, it uses the b2RopeJoint instead of the b2DistanceJoint.
     
  26. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    Is the "near future release" here soon? :)

    I can't wait! :D
     
  27. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Yes, it came in the 4.5 release a fair while ago and you can use it as I describe in my previous post. :)
     
    TruffelsAndOranges likes this.
  28. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    Oh! Thanks! I'll try that out.

    It would be cool if you added it as a normal component along with some nice documentation. :)