🔗LBJT: Rope Joint 9️⃣.

LibGDX Box2d Joints Tutorials.

Vladislav Shesternin
3 min readJan 2, 2024
LBJT Application: Download for ease of study!
Rope Joint

Text separators are set using chatGPT.

This article is about the Rope Joint, its settings, and how to make it work.

To make it easier to understand what is happening and how to perform practical tasks, start with the article: 🔗LBJT: Сommon in Joints 1️⃣.

And so the… Rope Joint

Rope joint — a joint designed to connect two bodies together usually used to make ropes or suspension bridges.

First, we need to set up the joint. For this, each joint has its own implementation of JointDef. You can read about this in the article: 🔗LBJT: Common in Joints 1️⃣. And now let’s talk about the RopeJointDef.

RopeJointDef settings:

(Mandatory)

  • bodyA — first joint body.
  • bodyB — second joint body.
  • collideConnected — <boolean> Specifies whether bodyA should collide with bodyB.

(Optional)

  • localAnchorA — the local anchor point relative to bodyA origin.
  • localAnchorB — the local anchor point relative to bodyB origin.
  • maxLength — length between anchor points.
RopeJointDef settings

Practice. How to create a Rope Joint?

In order for you to be able to test Joints, you need to interact with bodies, for this you need to implement MouseJoint. How to implement it is described in article 🔗LBJT: Mouse Joint 2️⃣.

Our example will consist of a static circle (blue) and a list of 4 dinamic rectangle (green) connected by RopeJoint.

Rope Joint

1️⃣Create 2 bodies: a static circle and a list of 4 dinamic rectangle:

val staticCircle    = StaticBody
val dynamicRectList = List(4) { DynamicBody }

2️⃣Create RopeJoint:

world.createJoint(RopeJointDef().apply {
bodyA = staticCircle
bodyB = dynamicRectList[0]
collideConnected = true
}

For ease of understanding, first we combine the static circle and the first rectangle from the list, everything will be the same with the rest, but at the end.

That’s it, you can already run the program and the bodies will be connected, but there is one but, they will be connected at their anchor points, where the center of mass is specified, to fix this, you need to configure localAnchorA, localAnchorB about how I explained how to calculate anchor points in the article: 🔗LBJT: Сommon in Joints 1️⃣.

3️⃣Configure: localAnchorA, localAnchorB:

world.createJoint(RopeJointDef().apply {
bodyA = staticCircle
bodyB = dynamicRectList[0]
collideConnected = true

localAnchorA.set(Vector2(2.5f, 0.0f))
localAnchorB.set(Vector2(1.5f, 5.0f))
}

Do not forget that the values are indicated in meters.
About the units of measurement of meters, kilograms, seconds (MKS) is written in the article: 🔗LBJT: Сommon in Joints 1️⃣.

Now everything seems to work but the bodies are shaking, it’s because the maxLength is not configured, let’s fix it.

4️⃣Configure: maxLength:

world.createJoint(RopeJointDef().apply {
...
maxLength = 1.5f
}

5️⃣Configure: list of 4 dinamic rectangle:

In fact, that’s all, now it remains to configure the list of 4 dinamic rectangle in the same way.

Since the first rectangle is already connected, there are 3 left:

repeat(3) { index ->
world.createJoint(RopeJointDef().apply {
bodyA = dynamicRectList[index]
bodyB = dynamicRectList[index + 1]
collideConnected = true

localAnchorA.set(Vector2(1.5f, 0.0f))
localAnchorB.set(Vector2(1.5f, 5.0f))

maxLength = 1.5f
}
}
Rope Joint

That’s how simple we created the rope, if you need the rope to stretch and you can adjust the elasticity, then it’s better to use Distance Joint, about which I also have a article: 🔗LBJT: Distance Joint3️⃣.

It works and it’s great 😎.

PS. Vel_daN: Love what You DO 💚.

--

--