This guide discusses how to configure the various physical Box2D joints available in Corona.
Joints can be used to assemble complex physical objects from multiple rigid bodies. For example, joints can be used to join the limbs of a ragdoll figure, attach the wheels of a vehicle to its body, create a moving elevator platform, and more.
Most joints are used to connect two rigid physical bodies, so you must create these bodies first and then submit them as arguments to the physics.newJoint() constructor function, along with other properties that vary depending on the joint type (see details below).
physics.newJoint( jointType, ... )
If a joint is no longer needed, you can destroy it by calling either object:removeSelf() or display.remove():
joint:removeSelf() --OR: display.remove( joint )
Note that if one of the joined bodies is destroyed, the associated joint will be automatically destroyed. However, remember to properly remove and nil
any references to a joint which has been destroyed.
The joint:getAnchorA() and joint:getAnchorB() functions return the x and y coordinates of either anchor point in content space, where A
and B
are the two joined bodies.
local ax, ay = myJoint:getAnchorA() local bx, by = myJoint:getAnchorB()
The joint:getLocalAnchorA() and joint:getLocalAnchorB() functions return the x and y coordinates of either anchor point, relative to object A
or B
which are the two joined bodies. Does not apply to touch or pulley joints.
local ax, ay = myJoint:getLocalAnchorA() local bx, by = myJoint:getLocalAnchorB()
This function returns the reaction force in Newtons at the joint anchor in the second body.
local reactionForceX, reactionForceY = myJoint:getReactionForce()
This read-only boolean value indicates whether a joint is active or inactive.
This read-only boolean value specifies if the joined objects follow the standard collision rules (true
) or not (false
).
This read-only value indicates the reaction torque in N*m at the joint anchor in the second body.
local reactionTorque = myJoint.reactionTorque
A pivot joint, known as a revolute joint in Box2D terms, joins two bodies at an overlapping point, like two boards joined by a rotating peg. The initial arguments are bodies A
and B
to join, followed by the x and y coordinates for the anchor point, declared in content space coordinates.
local pivotJoint = physics.newJoint( "pivot", bodyA, bodyB, anchor_x, anchor_y )
If you're building a complex assembly with pivot joints, like a ragdoll, and you're experiencing instability issues, consider setting physics.setContinuous() to true
.
Optionally, object B
joined with a pivot joint to object A
can be driven (rotated) with a joint motor. This rotation can be either clockwise or motorSpeed
value. Note that joint motors have a fairly weak maximum torque by default, so setting motorSpeed
may appear to have little visible effect. Therefore, you should generally set maxMotorTorque
to a high value such as 100000
if you're trying to move any significant mass, such as rotating a wheel to power a car.
Boolean. This must be set to true
to enable any motor action on a pivot joint.
Number. Value indicating the intended rotational motor speed.
Number. The maximum allowed torque for the joint motor.
Number. Read-only value indicating the current motor torque.
Pivot joints can optionally be constrained in their range of rotation. For example, if constructing a ragdoll figure, the head/neck joint would likely have a limited range of angular motion in either the clockwise or
Boolean. Set this to true
to constrain the pivot joint rotation limits.
Function. Accepts two values, in degrees, defining the negative and positive limits of rotation. For example, if you want to limit the rotation somewhat tightly in relation to the upward axis (0
), these values may be -10
and 10
respectively. Note that these values remain in relation to the rotation of body A
, meaning that if body A
rotates after the joint is declared, these rotation limits will remain in rotational synchronization with that body.
pivotJoint.isLimitEnabled = true pivotJoint:setRotationLimits( -20, 20 )
Function. This function returns the current negative and positive rotation limits.
local negLimit, posLimit = pivotJoint:getRotationLimits() print( negLimit, posLimit )
Number. Read-only value indicating the current angle of a pivot joint, in degrees.
Number. Read-only value indicating the current rotation speed of a pivot joint.
Number. Read-only value indicating the joint angle between the bodies at time of creation.
A distance joint attaches two bodies at a fixed distance. The initial arguments are bodies A
and B
to join, followed by an x and y anchor point coordinate for each body, declared in content space coordinates.
local distanceJoint = physics.newJoint( "distance", bodyA, bodyB, anchorA_x, anchorA_y, anchorB_x, anchorB_y )
Optionally, the distance joint can be made soft, like a
Number. This value can range from 0
(no damping) to 1
(critical damping). With critical damping, all oscillations should vanish.
Number. Specifies the
Number. Defines the distance between the anchor points, which should not be zero or very short. If you position the bodies before applying a distance joint to them, this length will automatically be set as the distance between the anchor points, so it's usually not necessary to set this parameter.
A piston joint, known as a prismatic joint in Box2D terms, joins two bodies along a single, restricted axis of motion, like engine pistons or an elevator platform that is restricted to movement along a vertical axis. The initial arguments are bodies A
and B
to join, followed by an x and y coordinate for an anchor point on either body, followed by values which define the axis along which linear motion is allowed.
local pistonJoint = physics.newJoint( "piston", bodyA, bodyB, anchor_x, anchor_y, axisX, axisY )
As stated, piston joints have a restricted axis of linear motion, defined by axisX
and axisY
. This axis can be considered a directional vector indicating which way the object will move when driven by a positive pistonJoint.motorSpeed value.
The following chart provides examples of the relationship between axisX
, axisY
, and a positive motor speed. However, both axisX
and axisY
values can be decimal values ranging between -1.0
and 1.0
— you are not restricted to these eight directions.
axisX | axisY | |
---|---|---|
0 |
-1 |
up / 0° |
1 |
-1 |
right-up / 45° |
1 |
0 |
right / 90° |
1 |
1 |
right-down / 135° |
0 |
1 |
down / 180° |
-1 |
1 |
left-down / 225° |
-1 |
0 |
left / 270° |
-1 |
-1 |
left-up / 315° |
To limit motion distance along the linear axis in either direction, use the pistonJoint:setLimits() function as illustrated below.
The speed of motion is not controlled by the axis values, but rather by the pistonJoint.motorSpeed property as explained in the next section.
A negative motor speed will drive the object in the opposite direction of its defined axis vector.
Optionally, the piston joint can be driven with a joint motor. Unlike the rotational motor of the pivot joint, this motion will be linear, restricted along the defined axis. For example, a piston joint motor can be used to move an elevator platform up and down along its axis.
Boolean. This must be set to true
to enable any motor action on a piston joint.
Number. Value indicating the intended linear motor speed. A positive motor speed will drive the object in the direction of its defined axis vector, while a negative motor speed will drive it in the opposite direction.
Number. The maximum allowed force for the joint motor.
Number. Read-only value indicating the current motor force.
Piston joints can optionally be constrained in their range of linear motion. The limits take the form of a lower and upper limit which define the range of motion (distance) along the axis defined by axisX
and axisY
.
Boolean. Set this to true
to limit the piston joint motion range.
Function. Accepts two values which define the negative and positive range of motion. The second value should always be greater than or equal to the first value, since they define a range of motion (distance) along the axis.
pistonJoint.isLimitEnabled = true pistonJoint:setLimits( -40, 120 )
Function. This function returns the current negative and positive motion limits.
local negLimit, posLimit = pistonJoint:getLimits() print( negLimit, posLimit )
Function. This function returns the current coordinates of the anchor point along the defined axis, in content space.
Number. Read-only value indicating the current translation of a piston joint, meaning, the distance of movement that has occurred along the axis.
Number. Read-only value indicating the current linear movement speed of a piston joint.
Number. Read-only value indicating the joint angle between the bodies at time of creation.
The friction joint is used to simulate A
and B
to join, followed by an x and y coordinate for the anchor point.
local frictionJoint = physics.newJoint( "friction", bodyA, bodyB, anchor_x, anchor_y )
When creating a friction joint, simulated friction can be applied via maximum force and torque values:
Number. The maximum directional friction which may be applied to the joined body. A higher value simulates higher friction.
Number. The maximum rotational friction which may be applied to the joined body. A higher value simulates higher friction.
The weld joint rigidly attaches two bodies at a specified anchor point in content space coordinates.
local weldJoint = physics.newJoint( "weld", bodyA, bodyB, anchor_x, anchor_y )
Due to mathematical approximation, this joint may appear slightly soft during simulation, even if dampingRatio
and frequency
are set to prevent this behavior. If you want to assemble multiple shapes without any risk of flexing, consider creating a
Number. This value can range from 0
(no damping) to 1
(critical damping). With critical damping, all oscillations should vanish.
Number. This value specifies the
Notice in the diagram that the two bodies don't necessarily need to overlap, and the joint anchor doesn't necessarily need to reside within one of the bodies. In fact, the two bodies can be completely separated and the weld joint will still join the bodies together as if a rigid, invisible link exists between them.
Number. Read-only value indicating the joint angle between the bodies at time of creation.
A wheel joint, known as a line joint in Box2D terms, is similar to a piston joint, except that the attached body can rotate freely like a wheel mounted to the shock absorber of a car. Most of its properties are inherited from the standard piston joint, but you can specify a A
and B
to join, followed by an x and y coordinate for an anchor point on either body, followed by values which define the axis along which linear motion is allowed.
local wheelJoint = physics.newJoint( "wheel", bodyA, bodyB, anchor_x, anchor_y, axisX, axisY )
Like the piston joint, wheel joints have a restricted axis of linear motion, defined by axisX
and axisY
. However, unlike piston joints, wheel joints aren't driven by a motor, so you can't send the wheel B
in the diagram)0,1
or 0,-1
will restrict motion to a vertical axis, but not restrict it to either upward or downward along that axis. Similarly, values of either 1,0
or -1,0
will restrict linear motion to either left or right along a horizontal axis.
Essentially, the wheel — or rather the object that represents the wheel — is prone to whatever forces might be upon it, for instance gravity. The object will attempt to stay upon the defined axis, but other forces may pull/push the wheel off of that axis in unpredictable ways.
Optionally, the wheel joint can be made soft, like a
Number. This value can range from 0
(no damping) to 1
(critical damping). With critical damping, all oscillations should vanish.
Number. This value specifies the 0
.
Function. This function returns the current coordinates of the anchor point along the defined axis, in content space.
Number. Read-only value indicating the current translation of a wheel joint, meaning, the distance of movement that has occurred along the axis.
Number. Read-only value indicating the current linear movement speed of a wheel joint.
A pulley joint attaches two bodies with an imaginary rope whose length remains constant. If one body is pulled or pushed by some force, the other body will compensate according to the simulated rope. The initial arguments are bodies A
and B
to join, followed by two coordinate pairs which represent the stationary anchor points from where each side of the rope hangs. The next four arguments are the x and y anchor point coordinates ratio
value which can be adjusted to simulate a block and tackle setup.
local pulleyJoint = physics.newJoint( "pulley", bodyA, bodyB, statA_x, statA_y, statB_x, statB_y, bodyA_x, bodyA_y, bodyB_x, bodyB_y, 1.0 )
The pulley joint features a ratio
property as the final parameter in the constructor. This ratio can be adjusted to simulate a block and tackle arrangement. In such a scenario, one side of the pulley rope moves faster than the other. The default ratio is 1.0
which simulates a simple pulley.
Number. Read-only value indicating the current ratio for the pulley joint.
Function. Returns the x and y coordinates of the joint's first ground anchor in content coordinates.
Function. Returns the x and y coordinates of the joint's second ground anchor in content coordinates.
Number. Read-only value that, upon instantiation, indicates the distance in pixels between the first joint anchor point and its stationary pulley anchor point.
Number. Read-only value that, upon instantiation, indicates the distance in pixels between the second joint anchor point and its stationary pulley anchor point.
A touch joint, known as a mouse joint in Box2D, connects a single object to an A
to connect followed by the x and y anchor point coordinates in content space.
local touchJoint = physics.newJoint( "touch", body, anchor_x, anchor_y )
For touch joints, the anchor point can be considered the point at which to "connect" the body. For example, to connect a body by its center point, specify body.x
and body.y
as the last two arguments. Alternatively, to drag an object by the point it was touched on, use the event.x
and event.y
values returned by the touch event. This anchor point is commonly configured as a point within the bounds of the body, although it can reside outside the bounds if desired.
Note that the body connected by the touch joint remains under simulation, so it will interact fully with the other bodies in the world. Also, the body will rotate realistically under gravity when connected by a
The target-following behavior of a touch joint is set by passing x and y coordinates to the touchJoint:setTarget() function:
Function. Sets the current target (follow) point of the touch joint as represented by targetX
and targetY
in content space coordinates. This can be any specific content point, the location of the user's touch, the coordinates of some other object to follow, successive points along a path, etc.
touchJoint:setTarget( targetX, targetY )
Number. This value can range from 0
(no damping) to 1
(critical damping). With critical damping, all oscillations should vanish.
Number. This value specifies the
Number. The maximum allowed force for the touch joint. By default, this attribute is 1000 × the mass of the body, allowing for fairly rapid target following.
Function. Returns the current target coordinates of the touch joint as specified by touchJoint:setTarget().
A rope joint connects two objects by a theoretical A
and B
to join, followed by optional localized offset points which can be used to set
local ropeJoint = physics.newJoint( "rope", bodyA, bodyB, offsetA_x, offsetA_y, offsetB_x, offsetB_y )
When configuring a rope joint, the offset values are optional and default to 0
unless set otherwise. Unlike other joints, these x and y values should not be specified in content space. Instead, they should be considered as localized offset values related to either body. For example, if you specify ( ..., 0, 0, 0, 0 )
, the rope ends will be attached to the center of each body. However, if you specify ( ..., -20, -20, 10, 10 )
, the rope end attached to body A
will be offset 20 pixels left and upward of its center point, while the rope end attached to body B
will be offset 10 pixels right and downward of its center point.
The maximum length of the rope can be set via the ropeJoint.maxLength property. Unlike a distance joint which can be used to keep two bodies at a fixed distance apart, the theoretical rope can bend/flex like an actual rope, but the maximum distance will always be constrained to this value.
Number. The maximum separation length of the two bodies (rope length), in pixels.
String. Read-only value that indicates the current limit state of the rope joint: "inactive"
, "lower"
, "upper"
, or "equal"
.
To simulate sophisticated mechanical contraptions involving gears, consider using gear joints. This joint can link two bodies — or multiple bodies in sequence — in a
local gearJoint = physics.newJoint( "gear", bodyA, bodyB, joint1, joint2, ratio )
The most important distinction of the gear joint is that it operates directly upon two other joints. These two joints must be created before instantiating the gear joint, and they must be either pivot or piston joints. Once declared, one of pivot/piston joints can be powered by its motor and impact the other pivot/piston joint attached to the parent gear joint. The same principle applies to successive joints in a more complex chain configuration, assuming they are properly chained together by other gear joints.
Always destroy gear joints before destroying the associated pivot/piston joints or their bodies. Otherwise, Box2D is liable to crash due to the orphaned gear joint pointers.
Another important aspect of the gear joint is the ratio
property. This indicates the ratio at which the
Number. The gear ratio, either positive or negative. Can be either set or read. Note that setting the correct ratio can be challenging when setting up a B
and wheel C
are 1.0
since wheel B
directly drives C
. However, you must calculate the ratio for the gear joint which links wheel C
to the vertical bar (D
), and because wheel C
has a larger circumference than wheel B
, this ratio would not be 1.0
.
Userdata. Read-only reference to the first pivot or piston joint associated with the gear joint.
Userdata. Read-only reference to the second pivot or piston joint associated with the gear joint.