🔗LBJT: Distance Joint3️⃣.

LibGDX Box2d Joints Tutorials.

Vladislav Shesternin
4 min readDec 17, 2023
LBJT Application: Download for ease of study!
Distance Joint

Text separators are set using chatGPT.

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

Distance joint — a joint designed to connect two bodies together like a spring with the ability to configure the elasticity and how quickly the oscillations disappear.

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

DistanceJointDef 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.
  • length — length between anchor points.
  • frequencyHz — affects the elasticity of the joint between bodyA and bodyB.

Value < 1: With values less than 1, such as 0.5 or 0.7, the joint will be strongly stretched, resembling a soft spring.

Value > 1: With values greater than 1, such as 2, 5, or 10, the joint will stretch very little or not at all, similar to a stiff spring.

  • dampingRatio — suppresses vibrations and takes values from 0 to 1.

0: does not suppress.

1: almost absolute suppression.

DistanceJointDef settings

Practice. How to create a Distance 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 platform and a dynamic ball connected by DistanceJoint.

Distance Joint
platform and ball

1️⃣Create 2 bodies: a static platform and a dynamic ball:

val platform = StaticBody
val ball = DynamicBody

2️⃣Create DistanceJoint:

world.createJoint(DistanceJointDef().apply {
bodyA = platform
bodyB = ball
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 their anchor points, where the center of mass is specified and with a length of 1 meter, to fix this, you need to configure localAnchorA, localAnchorB, length I talked about how to calculate anchor points in the article: 🔗LBJT: Сommon in Joints 1️⃣.

3️⃣Configure: localAnchorA, localAnchorB, length:

world.createJoint(DistanceJointDef().apply {
bodyA = platform
bodyB = ball
collideConnected = true

localAnchorA.set(Vector2(5f, 1f))
localAnchorB.set(Vector2(5f, 4f))
length = 6f
}

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

4️⃣Configure: frequencyHz (elasticity):

world.createJoint(DistanceJointDef().apply {
...
frequencyHz = 0.5f
}

5️⃣Configure: dampingRatio:

dampingRatio — it extinguishes the stretching of the joint and not the oscillations to the sides, so it can be said that it works together with frequencyHz.

world.createJoint(DistanceJointDef().apply {
...
frequencyHz = 0.5f
dampingRatio = 5.0f
}

The body returns to its original position very smoothly and slowly, as if someone is pulling it towards you a little at a time, a good effect, the greater the value of dampingRatio, the more your body will be pulled back and the slower it will return to its original position (don’t forget that it only works together with frequencyHz).

It works and it’s great 😎.

PS. Vel_daN: Love what You DO 💚.

--

--

Vladislav Shesternin
Vladislav Shesternin

Written by Vladislav Shesternin

LibGDX | Android Developer | Enthusiast.

No responses yet