CreatePhysicsHingeJoint ( body_a,body_b,x#,y#,z#[,axis_x#][,axis_y#][,axis_z#] )

Parameters

body_a - first physics body

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

x# - world X of the hinge anchor point

y# - world Y of the hinge anchor

z# - world Z of the hinge anchor

axis_x# (optional) - world X component of the hinge axis; 0 (default)

axis_y# (optional) - world Y component of the hinge axis; 0 (default)

axis_z# (optional) - world Z component of the hinge axis; 1 (default)

Description

Connects two bodies with a hinge that rotates about one axis.

The revolute joint: doors on frames, seesaws, wheels, flippers, drawbridges. The two bodies stay pinned together at the world-space anchor and can only rotate relative to each other about the axis. The default axis is world Z (0,0,1); for a door you would use the vertical axis 0,1,0. The axis does not need to be normalised, but a zero-length axis raises a runtime error.

PhysicsJointLimits clamps the swing to a range in radians (about 1.571 is 90 degrees), which turns a free hinge into a door that only opens so far. PhysicsJointMotor spins the hinge at a target speed in radians per second with a torque budget - powered wheels, fans and hatches.

Use PhysicsJointSpring for a spring that returns the hinge towards a resting angle, and PhysicsJointSpringTarget to set that angle in radians.

Anchors are captured at creation time, so position both bodies first. Free with FreePhysicsJoint; freeing either body removes the joint automatically.


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

Example

; CreatePhysicsHingeJoint Example
; -------------------------------

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

Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2

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 pendulum arm hanging below the anchor
arm=CreateCube()
ScaleEntity arm,0.25,1,0.25
PositionEntity arm,0,2.75,0,True
EntityColor arm,255,120,70
arm_body=CreatePhysicsBody(world,arm,PHYSICS_DYNAMIC)
arm_shape=PhysicsBodyBox(arm_body,0.5,2,0.5,1)

; Hinge the arm to the anchor, swinging about the world Z axis
hinge=CreatePhysicsHingeJoint(anchor_body,arm_body,0,3.75,0,0,0,1)

; Start the pendulum swinging
PhysicsBodyImpulse arm_body,1.5,0,0

While Not KeyDown(1)

    ; Space gives the pendulum a fresh push
    If KeyHit(57) Then PhysicsBodyImpulse arm_body,1.5,0,0

    ; 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 pendulum   Esc: exit"
    Text 0,20,"The hinge locks the arm to swing about the Z axis only"

    Flip

Wend

FreePhysicsWorld world
FreeEntity arm
FreeEntity anchor
End

Index