CreatePhysicsDistanceJoint ( body_a,body_b,anchor_ax#,anchor_ay#,anchor_az#,anchor_bx#,anchor_by#,anchor_bz#[,length#] )

Parameters

body_a - first physics body

body_b - second physics body; must be a different body in the same world

anchor_ax# - world X of the attachment point on body A

anchor_ay# - world Y of the attachment point on body A

anchor_az# - world Z of the attachment point on body A

anchor_bx# - world X of the attachment point on body B

anchor_by# - world Y of the attachment point on body B

anchor_bz# - world Z of the attachment point on body B

length# (optional) - rest length in metres; when omitted or not positive, the starting distance between the anchors is used (default)

Description

Connects two bodies so their anchor points keep a set distance apart.

Out of the box it behaves like a rigid rod between the two anchors: pendulums, hanging signs, tow bars, balance scales. Both anchors are given in world space at creation time, so position the bodies first and the joint remembers where it grabs each one.

Use PhysicsJointSpring to turn the rod into a spring with a chosen frequency and damping ratio. PhysicsJointSpringTarget changes its rest length.

Distance limits and motors require spring mode. Call PhysicsJointSpring joint,True,0 for a slack range or motor without a restoring force, then configure PhysicsJointLimits or PhysicsJointMotor. With spring mode disabled, the fixed rest length overrides both. Equal enabled limits also form a rigid constraint.

Both bodies must be current handles from the same world, and a body cannot be jointed to itself. Free the joint with FreePhysicsJoint; freeing either body removes it automatically.


See also: CreatePhysicsBallJoint, CreatePhysicsHingeJoint, CreatePhysicsSliderJoint, PhysicsJointLimits, PhysicsJointMotor.

Example

; CreatePhysicsDistanceJoint Example
; ----------------------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2

SeedRnd MilliSecs()

camera=CreateCamera()
PositionEntity camera,0,3,-8

light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 50,50,50

; Independent Box3D world with Earth-like gravity
world=CreatePhysicsWorld()
PhysicsGravity world,0,-9.81,0

; Blue static anchor block near the top of the scene
anchor=CreateCube()
ScaleEntity anchor,0.25,0.25,0.25
PositionEntity anchor,0,4,0,True
EntityColor anchor,80,160,255
anchor_body=CreatePhysicsBody(world,anchor,PHYSICS_STATIC)
anchor_shape=PhysicsBodyBox(anchor_body,0.5,0.5,0.5,0)

; Orange bob that will hang on an invisible rope
bob=CreateSphere()
ScaleEntity bob,0.5,0.5,0.5
PositionEntity bob,0,1.5,0,True
EntityColor bob,255,120,70
bob_body=CreatePhysicsBody(world,bob,PHYSICS_DYNAMIC)
bob_shape=PhysicsBodySphere(bob_body,1,1)

; Keep the bob exactly 2.5 metres below the anchor point
rope=CreatePhysicsDistanceJoint(anchor_body,bob_body,0,4,0,0,1.5,0,2.5)

; Start the bob swinging
PhysicsBodyImpulse bob_body,1,0,0.6

While Not KeyDown(1)

    ; Space pushes the bob so it swings on its fixed-length constraint
    If KeyHit(57) Then PhysicsBodyImpulse bob_body,Rnd(-1.5,1.5),0,Rnd(-1.5,1.5)

    ; Advance the Box3D simulation by one 60 Hz step
    StepPhysics world,1.0/60.0,4

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    Text 0,0,"Arrow keys: move camera   Space: push the bob   Esc: exit"
    Text 0,20,"The bob stays 2.5 m from the anchor, like a rigid rope"

    Flip

Wend

FreePhysicsWorld world
FreeEntity bob
FreeEntity anchor
End

Index