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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to 'daisy-chain' distance joints to make a High Weight "tow-rope"? [2D gravity game]

Discussion in 'Physics' started by Tset_Tsyung, May 27, 2018.

  1. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    406
    Hey all,

    First off, I probably used those speech and quotes marks incorrectly... my apologies [really should study that...]

    Okay, so I'm making a firing grappling hook to allow players to drag other (damaged) vehicles and crates.

    I've got the firing and spooling of the 'rope' working (DistanceJoint2D and LineRenderers with a BoxCollider2D being aligned in the right direction), and it works pretty well.

    The problem I have is when I try to actually attach to and tow an object. The vehicle has a mass of 1000, Gravity is only -1 (think moon). The rope segments are set to 0.1 mass (to mimic actually rope being spooling off) with a length of 0.5 units each. The hook has 1 mass and same distances.

    NOTE: I have experimented with changing the mass to meet a ratio of 10:1 after reading @MelvMay 's comment on this thread https://forum.unity.com/threads/dis...ance-should-not-vary-yet-it-stretches.239350/, this resolves SOME of the issue... but then I end up with a really heavy rope and hook combo, coupled with immense weight to deal with and a still slightly convulsing rope...

    So, in light of all the above I was wondering about work arounds. Has anyone had some progress with this? As stated in the bold text I have started experimenting with 'distributing' the weight up the line a little and will continue to tweak. However my brain has hit a wall for a Sunday morning. So I'm going AKF for a bit and will tackle this again later.

    Would love to hear what you've all found works.

    (Yes, will provide animated Gifs, script excerpts and screenshots if they are desired XD)

    Mike

    UPDATE:
    I thought I had a eureka moment (literally whilst in the bath... no joke) and came up with an idea to clear the "Connected Rigidbody" field in the distance joint of the hook (which is how I was connecting the hook to the end of the rope) and instead just update the "connected anchor" field each frame, hoping that if it didn't KNOW the weight of the next point in the chain that it would cause an issue...

    No good!

    My latest idea is to reduce the weight of the attached vehicle or crate significantly... however I'm not sure how much this will affect the 'feel' of the game... will test more and update.
     
    Last edited: May 27, 2018
  2. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    406
    Okay, so whilst playing around with other ideas I thought about distributing the weight over the entire rope.

    Basically, once the grappling hook has latched to the other object, divides the mass of the object between the objects itself, the hook and all the rope segments.

    FINALLY! This sorts the issue of the stretching DistanceJoints! Since they are all the same mass there is not a SINGLE segment that's stretching longer than it needs to be.

    Once I 'release' the hook I destroy the hook itself and the rope segments (will pool objects instead once I get out of the tech prototyping stage) and set the object back to it's original mass.

    However this does still have an issue in that once I've distributed the mass along the whole rope it doesn't SEEM to behave quite right, because all of the rope segments are heavier than they should be, so it's still looking like a derpy workaround.

    But, as I stated in the OP, I would LOVE to hear about what solutions any and all of you have come up with. Experimenting on my own only gets me so far ;)

    As always, all the best everyone ;)

    Mike.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,470
    Sounds like you're getting it tuned but one thing you didn't mention is whether you've increased the number of solver iterations. The default iterations in a project are just that, a default and can/should be changed depending on what you're doing. If you give the solver more iterations it might come up with a better solution. Certainly don't be afraid to increase these but obviously you'll need to pay attention to performance, particularly on the target device(s).

    Physics2D.velocityIterations
    Physics2D.positionIterations

    One note about position iterations is that it won't necessarily perform all those iterations if the solver errors are small and will early-out so it's a maximum iterations allowed rather than a fixed number of iterations it'll perform.

    Worth experimenting.
     
    Last edited: May 28, 2018
  4. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    406
    Hi MelvMay,

    I had hoped you'd popped by, but certainly hadn't expected it... Many, many thanks for the reply.

    Hmm, i got scared off of messing with the iterations because someone said that they experienced a change in the speed of the physics... but that was in a forum post... and they probably meant slower due to the performance that you mentioned...

    In other words, no I hadn't messed with the iterations, lol (why couldn't you just say that, Mike?!?!?!?!).

    I will give that a bash and update this thread for anyone else experiencing the same problems that I am.

    Again, I really appreciate your input. As well as the work all who work at Unity Technologies are doing (I mean, some of these new additions you're putting in are so exciting... I just don't have a project to use them on yet, lol)

    Mike
     
  5. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    406
    @MelvMay

    Well changing the iterations does indeed help. However, if I leave the rope at a mass of 1 and the hook at a mass of 3 then I have to ramp up the solver iterations to 160 (position) and 60 (velocity) to get it to behave 'normally'.

    Please Note: Setting rope segments' mass to 2.5 and Hook's to 10 only produces minimal stretching which is visually similar to rope being tugged, and is therefore not TOO much of a problem. However I'm unsure what would happen when I start adding more object to the game and it's physics engine.

    However I then find that 2 things will break the reality of the physics.
    1) With the rope slack, flying at high speed to make it taught causes stretching.
    2) If the rope is long then it has a higher chance of stretching when speed is applied

    (I will state that the speed issue is a new one that I found through experimenting and was prob always there to some extent.)

    With the position and velocity iterations with their default values of 8 and 3 respectively the distributed weight SEEMS to be the best solution so far - but still derpy.

    I believe that by distributing the weight so that the segments are lighter near the players ship and heavier near the object that I will achieve the desired effect.

    Will update thread with results.

    Mike
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,470
    Have you tried adding a single DistanceJoint2D connecting the beginning of the "rope" to the end with that joints' MaxDistanceOnly set to true? This should resist restretching too.

    Not sure if you've seen it but a while back I started creating a GitHub repro here with the aim to cover all components. I never found the time to finish it but hopefully will eventually. Anyway, loading this scene from that project shows the above in action.

    https://gyazo.com/b8f17b2e52ef8a487c7349cd294d1230
     
  7. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    406
    Of course!!!!!!! XD XD XD

    That fixes it! Gah! Such a simple and elegant solution. Have a non visible distance joint handle the actual load bearing, since the players ship and the towed object will be closer in mass, and let the rope segments and their distance joints be purely visual!

    Kicking myself that I didn't see that, but so glad you suggested it! Thank you SO SO SO much. You are an absolute star!

    As for the repo, will check it out as it'll be interesting to see what else I may have missed ;)

    Warmest regards,


    Mike
     
    MelvMay likes this.