🔗LBJT: Gear Joint 1️⃣1️⃣.

LibGDX Box2d Joints Tutorials.

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

Text separators are set using chatGPT.

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

Gear joint — a joint designed to connect two joints together but there is 1 quirk to create it, two existing revolute or prismatic joints are required (any combination will do). The connections provided should attach the dynamic body to the static body.

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

GearJointDef settings:

(Mandatory)

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

(Optional)

  • joint1 — first joint (RevoluteJoint or PrismaticJoint).
  • joint2 — second joint (RevoluteJoint or PrismaticJoint).
  • ratio — the ratio between the joint1 and joint2 (default = 1)
GearJointDef settings

Practice. How to create a Gear 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 RevoluteJoint and PrismaticJoint connected by GearJoint.

Gear Joint

1️⃣Create 2 joints: Revolute and Prismatic:

I described how to create RevoluteJoint in the article: 🔗LBJT: Revolute Joint 4️⃣.
I described how to create PrismaticJoint in the article: 🔗LBJT: Prismatic Joint 5️⃣.

For ease of understanding, place the joints as in the video above.

val jointRevolute  = RevoluteJoint
val jointPrismatic = PrismaticJoint

2️⃣Create GearJoint:

world.createJoint(GearJointDef().apply {
bodyA = jointRevolute.DynamicBody
bodyB = jointPrismatic.DynamicBody
collideConnected = true
}

bodyA and bodyB — in them we install a dynamic body with RevoluteJoint and PrismaticJoint (those bodies that will directly rotate or move).

3️⃣Configure: joint1 | joint2:

world.createJoint(GearJointDef().apply {
...
joint1 = jointRevolute
joint2 = jointPrismatic
}

That’s it, you can already run the program and the bodies will be connected.

4️⃣Configure: ratio:

ratio — the ratio between the joint1 and joint2 (default = 1)

When you set the value for ratio, always think that this value is for joint1, and for joint2 ratio = 1.

RevoluteJoint with RevoluteJoint:
If you have joint1 = RevoluteJoint and joint2 = RevoluteJoint.

  • If ratio = 1, it means that when joint1 makes 1 revolution, then joint2 makes 1 revolutions.
  • If ratio=0.5, it means that when joint1 makes 1 revolution, then joint2 makes 2 times more, i.e. 2 revolutions.
  • If ratio=2, it means that when joint1 makes 1 revolution, then joint2 makes 2 times less, i.e. 0.5 revolutions.

PrismaticJoint with PrismaticJoint:
It works in the same way as RevoluteJoint with RevoluteJoint, only here not revolutions, but meters.

RevoluteJoint with PrismaticJoint:
This is the same as in our example, but how to calculate how many meters PrismaticJoint will pass if RevoluteJoint makes 1 revolution?

Everything is very simple, by how many radians will RevoluteJoint turn, so much in meters and PrismaticJoint will pass, and vice versa how many meters will PrismaticJoint pass by so many radians and RevoluteJoint will return.

If you need to change the direction of rotation or movement, simply change the sign of ratio = (-1 negative | +1 positive).

Do not forget that all angles in Box2d are measured in radians.
Do not forget
that the values of the distance are indicated in meters.

About radians and units of measurement of meters, kilograms, seconds (MKS) is written in the article: 🔗LBJT: Сommon in Joints 1️⃣.

GearJoint ratio

For better understanding as you can see in the example I added the meters as black and white cubes, drew the degrees around RevoluteJoint and output the RADIANS and METERS values in separate labels (you can output it in logs).

The main thing is not to forget that in Box2d everything rotates in radians and not degrees, because for the first time I output degrees and was very confused about how it works, and then I remembered that everything is in radians and everything became obvious.

It works and it’s great 😎.

PS. Vel_daN: Love what You DO 💚.

--

--