🔗LBJT: PulleyJoint 1️⃣0️⃣.

LibGDX Box2d Joints Tutorials.

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

Text separators are set using chatGPT.

This article is about the Pulley 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… Pulley Joint

Pulley joint — a joint designed to connect two bodies together usually used for a pendulum or something that has a counterweight.

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 PulleyJointDef.

PulleyJointDef settings:

(Mandatory)

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

(Optional)

  • groundAnchorA — anchor point for bodyA relative to the beginning of the world.
  • groundAnchorB — anchor point for bodyB relative to the beginning of the world.
  • localAnchorA — the local anchor point relative to bodyA origin.
  • localAnchorB — the local anchor point relative to bodyB origin.
  • lengthA — length between groundAnchorA and localAnchorA.
  • lengthB — length between groundAnchorB and localAnchorB.
  • ratio — length ratio between lengthA and lengthB.
PulleyJoint settings

Practice. How to create a Pulley 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 2 dynamic circles connected by a Pulley Joint.

Pulley Joint

1️⃣Create 2 dinamic circle bodies:

val bCircleLeft  = DynamicBody
val bCircleRight = DynamicBody

2️⃣Create PulleyJoint:

world.createJoint(PulleyJointDef().apply {
bodyA = bCircleLeft
bodyB = bCircleRight
collideConnected = true
}

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 the world point (x=0.0 | y=0.0), to fix this, you need to configure groundAnchorA, groundAnchorB. (measured in meters).

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️⃣.

3️⃣Configure: groundAnchorA, groundAnchorB:

world.createJoint(PulleyJointDef().apply {
...
groundAnchorA.set(Vector2(9.0f, 9.0f))
groundAnchorB.set(Vector2(11.0f, 9.0f))
}
PulleyJoint groundAnchorA | groundAnchorB

It works but it doesn’t work as a pulley and that’s because you have to configure the length, let’s fix that: (measured in meters).

4️⃣Configure: lengthA, lengthB:

world.createJoint(PulleyJointDef().apply {
...
lengthA = 4f
lengthB = 4f
}
PulleyJoint lengthA | lengthB

5️⃣Configure: localAnchorA, localAnchorB:

It works but the bodies are 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️⃣.

world.createJoint(PulleyJointDef().apply {
...
localAnchorA.set(Vector2(1.0f, 1.0f))
localAnchorB.set(Vector2(1.0f, 1.0f))
}

6️⃣Configure: ratio:

ratio — is the setting of the ratio of lengthA to lengthB (default is 1 x 1).

Аlways assume that the ratio of lengthB = 1 and You configure the ratio for lengthA (default is 1).

PulleyJoint ratio

There are cases when bodies stick for a while to a point groundAnchor this is solved by a simple displacement of the groundAnchor or localAnchor, or the creation of an additional body in the place of adhesion with which a collision will occur.

It works and it’s great 😎.

PS. Vel_daN: Love what You DO 💚.

--

--