Joints

class Joints(source)

Joints between rigid bodies, the way machines connect them: hinge, slider, weld and distance, solved the XPBD way, as in Müller et al. 2020.

A joint's first end is a RigidBody. Its second, a JointEnd, is another body, the world (JointEnd.WORLD: a fixed point in it), or a Particle. Each holds a point of each end, its anchor, where it was when the joint was made. A particle can't turn, so a joint to one holds its position only.

Every pass, a joint pulls its anchors where they should be, split by how hard each end is to move there, as for a contact, and turns the two ends to the angle they should have. Its compliance says how soft that is: 0 is rigid. A hinge and a slider can have limits (setLimits), past which they're pushed back, and a motor (setMotor), which drives them at a speed with at most a torque or a force. A weld breaks, and is removed, when the force holding it passes its breakForce.

Two bodies joined to each other don't collide with each other: a wheel can sit inside its axle box.

Each joint is a Joint handle; a removed one's slot is reused. Removing a body or particle removes every joint that holds it.

toString gives how many joints there are, for example Joints(size=12).

Samples

val world = PhysicsWorld()
val door = world.rigidBody(Shape.box(40f, 4f), x = 20f, y = 0f)
// Hinged to the world at its left end: it swings down about that point.
val hinge = world.joints.hinge(door, JointEnd.WORLD, x = 0f, y = 0f)
repeat(60) { world.step(1.seconds / 60) }
assertTrue(world.rigidBodies.angleOf(door) > 0.5f)
assertPrints(world.joints.kindOf(hinge), "HINGE")

Properties

Link copied to clipboard
val size: Int

Number of joints.

Functions

Link copied to clipboard
fun angleOf(joint: Joint): Float

Returns how far hinge joint's second end has turned against its first since it was made, in radians, clockwise on screen.

Link copied to clipboard
fun axisXOf(joint: Joint): Float

Returns the direction slider joint slides along in the world now, across: it turns with its first end.

Link copied to clipboard
fun axisYOf(joint: Joint): Float

Returns the direction slider joint slides along in the world now, down.

Link copied to clipboard
fun clearLimits(joint: Joint)

Takes away joint's limits, so it turns or slides as far as it likes.

Link copied to clipboard
fun distance(a: RigidBody, b: JointEnd, ax: Float, ay: Float, bx: Float, by: Float, compliance: Float = 0.0f): Joint

Holds the world point (ax, ay) of a and (bx, by) of b as far apart as they are now, as softly as compliance, and returns the joint's handle. Pass the points by name.

Link copied to clipboard

Returns where joint holds its first end in the world now, across.

Link copied to clipboard

Returns where joint holds its first end in the world now, down.

Link copied to clipboard

Returns joint's first end.

Link copied to clipboard
fun forceOf(joint: Joint): Float

Returns the force joint held its anchors together with over the last substep: 0 while its bodies sleep.

Link copied to clipboard
inline fun forEach(action: (Joint) -> Unit)

Calls action with every joint, in slot order. action may remove the joint it is given.

Link copied to clipboard
fun hasLimits(joint: Joint): Boolean

Returns true if hinge or slider joint has limits: see setLimits.

Link copied to clipboard
fun hasMotor(joint: Joint): Boolean

Returns true if hinge or slider joint has a motor on: see setMotor.

Link copied to clipboard
fun hinge(a: RigidBody, b: JointEnd, x: Float, y: Float): Joint

Holds a and b together at the world point (x, y), free to turn about it, and returns the joint's handle.

Link copied to clipboard
fun isAlive(joint: Joint): Boolean

Returns true if joint is still there: not removed and not broken.

Link copied to clipboard
fun kindOf(joint: Joint): JointKind

Returns what kind of joint joint is.

Link copied to clipboard
fun lowerLimitOf(joint: Joint): Float

Returns joint's lower limit: an angle in radians for a hinge, a distance for a slider.

Link copied to clipboard
fun motorSpeedOf(joint: Joint): Float

Returns how fast joint's motor drives it: radians a second for a hinge, units a second for a slider.

Link copied to clipboard
fun positionOf(joint: Joint): Float

Returns how far slider joint's second end has slid along its axis since it was made, in units.

Link copied to clipboard
fun remove(joint: Joint)

Removes joint; its two ends wake, as what held them is gone. Removing one that is gone does nothing.

Link copied to clipboard
fun restAngleOf(joint: Joint): Float

Returns the angle hinge joint's two ends had when it was made: its second end's less its first's.

Link copied to clipboard

Returns where joint holds its second end in the world now, across.

Link copied to clipboard

Returns where joint holds its second end in the world now, down.

Link copied to clipboard

Returns joint's second end: a body, JointEnd.WORLD, or a particle.

Link copied to clipboard
fun setLimits(joint: Joint, lower: Float, upper: Float)

Keeps hinge joint's angle (how far its second end has turned against its first since it was made, in radians), or slider joint's position (how far its second end has slid along the axis, in units), from lower to upper.

Link copied to clipboard
fun setMotor(joint: Joint, speed: Float, maxTorque: Float)

Drives hinge joint at speed radians a second, with at most maxTorque, or slider joint at speed units a second, with at most maxTorque as a force. A maxTorque of 0 turns the motor off. The speed is the second end's against the first, clockwise on screen, as angleOf counts: a wheel hinged to JointEnd.WORLD turns at -speed, since the world turns against the wheel.

Link copied to clipboard
fun slider(a: RigidBody, b: JointEnd, x: Float, y: Float, axisX: Float, axisY: Float): Joint

Lets b slide against a only along the axis (axisX, axisY) through the world point (x, y), as the two lie now, and not turn against it, and returns the joint's handle: a bead on a wire. Pass the arguments by name.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun upperLimitOf(joint: Joint): Float

Returns joint's upper limit: an angle in radians for a hinge, a distance for a slider.

Link copied to clipboard
fun weld(a: RigidBody, b: JointEnd, x: Float, y: Float, compliance: Float = 0.0f, breakForce: Float = Float.POSITIVE_INFINITY): Joint

Holds a and b together at the world point (x, y), and at the angle they have now, as softly as compliance, and returns the joint's handle. Pulled harder than breakForce, it breaks: it is removed, and Events.brokenJointAt reports it.