In this tutorial we will go through joint types available in Gideros Box2d world.

If you follow my tutorials you should already be familiar with mouseJoint which is the right way to drag dynamic box2d objects. I'll be using it alot in this tutorial and I'll also include mouseJoint in example, but won't cover how it works here. If you want to know more about it, go to Dragging Box2d object in Gideros Mobile tutorial

So let's start:

First joint is a revolute joint

Revolute joints is used to create an object which rotates around it's own center. It is also possible to set limits on angles and speed, also as apply motor (the force to make it rotate)

Here's an example to create revolute joint



--create empty box2d body for joint
local ground = self.world:createBody({})

--create ball
self.ball1 = self:ball(300, 300)
local jointDef = b2.createRevoluteJointDef(self.ball1.body, ground, 300, 300)
local revoluteJoint = self.world:createJoint(jointDef)
--this will not let ball spin for ever
revoluteJoint:setMaxMotorTorque(1)
revoluteJoint:enableMotor(true)

Next joint is a prismatic joint

Prismatic joint can restrict to moving object only on spcific line. Similar to revolute joints, one could also apply motor and maximum force to use in prismatic joint

Here's an example to create revolute joint



--create empty box2d body for joint
local ground = self.world:createBody({})

--create ball
self.ball1 = self:ball(350, 100)
--axisx, 	axisy	values usually between 0 and 1
--0			0		moves freely
--0			1		moves on y axis
--1			0		moves on x axis
--1			1		moves on diagonal
local jointDef = b2.createPrismaticJointDef(self.ball1.body, ground, 350, 100, 0.3, 1)
local prismaticJoint = self.world:createJoint(jointDef)

Now distance joint

Distance joint keeps constant distance between two objects (atleast tries to keep them at constant distance).By default the distance is the same as distance between objects when joint was created, but you can also modify it's length using setLength method

Here's an example to create distance joint



--create empty box2d body for joint
local ground = self.world:createBody({})

--create balls
self.ball1 = self:ball(100, 100)
self.ball2 = self:ball(200, 100)
--create distance joint
local jointDef = b2.createDistanceJointDef(self.ball1.body, self.ball2.body, 100, 100, 200, 100)
local distanceJoint = self.world:createJoint(jointDef)
--by default length between two bodies is the length they have between them when joint was created
--but it is possilbe to change it using
distanceJoint:setLength(200)

Next one is a pulley joint

Pulley joint is basically a two bodies attached to the same string, which are on two reels, and when you pull one, the other follows.

You can control the ratio of pulling each object and moving other, or making one object heavier than other



--create empty box2d body for joint
local ground = self.world:createBody({})

--create ball
self.ball1 = self:ball(200, 300)
self.ball2 = self:ball(350, 300)
local jointDef = b2.createPulleyJointDef(self.ball1.body, self.ball2.body, 
	200, 100, --coordinates where "imaginary string" is attached in the air for body 1
	350, 100, --coordinates where "imaginary string" is attached in the air for body 2
	200, 300, --coordinates where "imaginary string" is attached to the body 1
	350, 300, --coordinates where "imaginary string" is attached in the body 2
	1) -- ratio (if < 1 -> makes body 2 heavier, > 1 makes body 1 heavier, =1 makes them equal
local pulleyJoint = self.world:createJoint(jointDef)

Another type of joint is a gear joint

Gear joint can connect combination of two revolute or prismatic or mix joints. And when you rotate/pull one, it affects the motor of other body

You can control the ratio of affecting the motor, set limitations, etc



--create empty box2d body for joint
local ground = self.world:createBody({})

--create ball for revolute join
self.ball1 = self:ball(300, 300)
--create revolute joint
--note that ground should be passed as first parameter here
local jointDef = b2.createRevoluteJointDef(ground, self.ball1.body, 300, 300)
local revoluteJoint = self.world:createJoint(jointDef)
--set motor
revoluteJoint:setMaxMotorTorque(1)
revoluteJoint:enableMotor(true)

--create ball for prismatic joint
self.ball2 = self:ball(350, 100)
--axisx, 	axisy	values usually between 0 and 1
--note that ground should be passed as first parameter here
local jointDef = b2.createPrismaticJointDef(ground, self.ball2.body, 350, 100, 0.3, 1)
local prismaticJoint = self.world:createJoint(jointDef)
--set motor
prismaticJoint:setMaxMotorForce(1)
prismaticJoint:enableMotor(true)


--create gear joint using two already created joints
local jointDef = b2.createGearJointDef(self.ball1.body, self.ball2.body, revoluteJoint, prismaticJoint, 1)
local gearJoint = self.world:createJoint(jointDef)

Next one is a wheel joint

If revolute joint is usually used to make body rotate around it's own center, then wheel joint can be used to make body rotate around any other center, plus with configurable radius and translations on axis



--create empty box2d body for joint
local ground = self.world:createBody({})

--create ball
self.ball1 = self:ball(350, 200)
--axisx, 	axisy	values usually between 0 and 1
--0			0		moves freely
--0			1		force pulling back on y axis
--1			0		force pulling back on x axis
--1			1		force pulling back all diretions
local jointDef = b2.createWheelJointDef(self.ball1.body, ground, 350, 200, 1, 1)
local wheelJoint = self.world:createJoint(jointDef)

Then there is also a weld joint

Weld joint simply glues two bodies together. Plain and simple.



--create ball
self.ball1 = self:ball(100, 100)
self.ball2 = self:ball(130, 100)
local jointDef = b2.createWeldJointDef(self.ball1.body, self.ball2.body, 100, 100, 130, 100)
local weldJoint = self.world:createJoint(jointDef)

And the last one is friction joint

Friction joint can limit movement, force or rotation of the body. The effect achieved can be pretty similar to what dampening does to the body as you can restrict forces aplied to the body



--create empty box2d body for joint
local ground = self.world:createBody({})

--create ball
self.ball1 = self:ball(350, 200)
	
--create friction joint
local jointDef = b2.createFrictionJointDef(self.ball1.body, ground, 350, 200)
local frictionJoint = self.world:createJoint(jointDef)

Here is an example video of demo app about joints

And you can also download a demo Gidero project to try all the joints yourself