Will RopeJoint2D be added in future, or how can i make a new Joint?

I’m doing some experiments to create a rope simulation in unity 2d physics, right now the best selection is HingeJoint2D, but it jitter, hard to make it stable.
I have seen box2d has a RopeJoint2D : https://github.com/erincatto/Box2D/blob/master/Box2D/Box2D/Dynamics/Joints/b2RopeJoint.cpp

So I am wondering will it be added into Unity, or is there any way i can make it, write my own Joint2D?

It’s been in since the early days of 2D. Simply use DistanceJoint2D.maxDistanceOnly set to true.


Thank you MelvMay, here’s my structure picture, the orange circles are the joints. I’m trying to make a rigid chain, with joints connected.

I have tried as you say DistanceJoint2D with maxDistanceOnly = true, see below gifs:

(With DistanceJoint2D, distance=0, maxDistanceOnly=true, you can see its too soft some joints can easily has long distance)


(With HingeJoint2D, no motor, no limits, it seems rigid at beginning, but when the ship accelerate fast, become mess)

I’m wondering, is there something wrong that i can not implemented a rigid chain with joints, maybe i have to make some special config in the Physics2D class? I have no idea, any suggestion will be appreciated.

First, DistanceJoint2D.distance cannot be set to zero, its lowest value is 0.005. Second, Box2Ds b2RopeJoint is designed to be added to bodies at the start and end of the chain/rope to stop the whole hierarchy of joints from stretching i.e. use a single DistanceJoint2D.

Box2D doesn’t like it when bodies have a mass ratio higher than 10:1 so ensure you don’t have crazy differning masses on the bodies or have extremely low masses and high forces being applied.

When using joints, particularly when configuring them into a hierarchy, you need to be extremely careful to not make the whole thing unstable as the solver has a lot of work to do balancing it. The forces you apply should be reasonable, as should be any explicit velocities you are applying. You should absolutely not simply reposition bodies either explicity on them or via the Transform (changing any transform settings causes this).

Also, you are free to increase the solver iterations in Physics2D to make joints more stable (they are after all, just defaults) if you’re pushing things although TBH, I’ve set-up lots and lots of ropes/chains with a single rope (DistanceJoint2D with MaxDistanceOnly set) and they all work fine.

The solver used by Box2D will always produce ‘soft’ joints however increasing iterations and/or using the ‘rope’ joint correctly should solve most use-cases for ropes/chains etc.

Thanks for the details!
I have tried 0.01 instead of zero, same behavior, based on your information, my issue should be mass, the ship main body vs chain node is about 100:1. But if I increase the mass of chain node, it will not be reasonable for the game since it is just a chain.

So the best way to implement a chain or rope is to use just one single DistanceJoint2D(make visual effect between the start and end), right?

Does that mean Box2D is not suit for such case ( joints by joints connected each other), because i hope to make simple models for players that they can connect them freely.

Yes, that’s exactly how it’s supposed to be used yes.

Not at all, why should that be the case? You cannot set-up a complex chain of joints, enter values and expect it all to magically work with defaults. The solver cannot always solve what you’re configuring. If it ‘explodes’ then it’s simply showing that you’re doing something unreasonable that it cannot solve.

You didn’t say whether you added a single DistanceJoint2D from start/end but posted about not being able to do what you’re doing. Have you tried this?

Guess what, that is exactly what I want to provide, to my game players: set-up a complex structure connected by various joints, and drive them.

Well, after experiments and your help, Now I understand that is a really tough work.

I have not tried that. I guess it may works like a soft rope, but I’m currently focusing on joint by joint, just want to discover how difficult to make them stable. Seems i should have a clever way to archive this, the main problem is the mass issue, I can not ensure the body1 connected body2 and they always has low mass ratio.

It’s not tough at all. For the 3rd time, you don’t need a DistanceJoint2D on each joint-node, just try adding a single one to stop the whole thing from being stretched and it’ll work fine!

Hi MelvMay, I’v made a mistake about the title of this thread, you may misunderstand my issue.
It should be : How to build a joint component just like LEGO toy, that user can connect them each by each to become a iron chain?
Just like the gif:

That means I’m trying to provide a component of the chain, not the chain self, the game player can connect them to be a chain, or connect them to be anything other.

So, i can’t not just use one single DistanceJoint2D, rope is not my purpose. The real purpose is to create a node, a node that can be connected each by each, then the whole node perform like a chain, or if you connect them in other forms, they may not perform like a chain, such as circle connected:

So none of this should be a problem.

I can only presume you’ve addressed everything I’ve previously mentioned such as ensuring you’re moving the bodies correctly and not teleporting them from position to position and increasing iterations to give a more stable joint simulation etc?

Ensure any animation isn’t modifying Transforms or causing joints or other physics components to be enabled/disabled or changing the Rigidbody2D properties. When you change stuff like this, it can make the solver’s job much harder.

Also, you could consider reducing the fixed time-step i.e. run more physics updates per/second.

Again, you may not have to do any of this if the problem is being caused by something else. If changing the time-step, increasing iterations etc doesn’t help then you’re doing something else to cause the simulation to explode.

As a final note; feel free to search for Box2D joint stretching as whatever you find there will apply here. Unity doesn’t make it more unstable, in most cases we’re just a light wrapper around Box2D, especially for joints and simulation stuff. You’ll find references to reducing the time-step, increasing iterations, ensuring mass ratio is reasonable etc.

Yes.

I have increased the position iterations from 3 to 30, the simulation becomes very stable, i will try more to find a good number for best performance vs effects.

I would say thank you, helped me understood the physics2d system deeper.

My last question is, how can i extend the Physics2D system? For example, write my own joint class. (Box2d is open source, so if unity provide a way, we may can learn to do that.)

Even adding a joint to Box2D isn’t simple and requires a bunch of changes. Unity does not provide you with the native source therefore this is not possible. Not that writing joints is an easy task in the first place.