Blitz3D+ Command Reference

PhysicsJointSpringTarget joint,target#

Parameters

joint - distance, slider or hinge joint handle

target# - finite rest length in metres (distance), translation in metres (slider), or angle in radians (hinge)

Description

Changes the resting target of a distance, slider or hinge joint without recreating it.

For a distance joint, target is the unloaded distance between the two anchor points and must be positive. Box3D clamps very small lengths to its stable minimum. The initial target comes from CreatePhysicsDistanceJoint, including its automatic initial-distance calculation.

For a slider, target is signed travel along the joint axis in metres, relative to its creation frames. Zero is the initial anchor alignment; a negative value moves in the opposite direction to the axis. For a hinge, target is a signed angle around the hinge axis, relative to its creation frames. Hinge targets must be between -Pi and Pi radians, inclusive; about 1.571 radians is 90 degrees. Slider and hinge targets initially equal zero.

Use PhysicsJointSpring to enable the spring and choose frequency and damping. Changing a target does not enable the spring or alter those settings. On a distance joint with its spring disabled, the new target changes the rigid rod length. On sliders and hinges with their springs disabled, it is remembered for the next time the spring is enabled.

The command wakes both connected bodies; call it when the target changes rather than every frame. The bodies move through physics on subsequent StepPhysics calls, rather than being teleported. Limits and motors remain in force, and gravity or other loads can prevent the body from resting exactly at the target.

Invalid or stale handles, non-finite targets, non-positive distance lengths, out-of-range hinge angles, and ball or fixed joints raise a runtime error.

Requires Extended mode.

See also: PhysicsJointSpring, CreatePhysicsDistanceJoint, CreatePhysicsSliderJoint, CreatePhysicsHingeJoint, PhysicsJointLimits.

Example

; PhysicsJointSpringTarget Example
; --------------------------------
; Requires Extended mode.

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

Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2

camera=CreateCamera()
PositionEntity camera,0,3,-7
RotateEntity camera,20,0,0

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

; An independent Box3D world with standard gravity
world=CreatePhysicsWorld()

; Blue static floor
ground=CreateCube()
ScaleEntity ground,6,0.5,6
PositionEntity ground,0,-0.5,0
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,12,1,12)

; A static post, and a door hinged to it on a vertical axis
post=CreateCube()
ScaleEntity post,0.1,1.5,0.1
PositionEntity post,-1.5,1.5,0
EntityColor post,150,150,160
post_body=CreatePhysicsBody(world,post,PHYSICS_STATIC)
post_shape=PhysicsBodyBox(post_body,0.2,3,0.2)
door=CreateCube()
ScaleEntity door,1.2,1.4,0.05
PositionEntity door,-0.2,1.5,0
EntityColor door,255,120,70
door_body=CreatePhysicsBody(world,door,PHYSICS_DYNAMIC)
door_shape=PhysicsBodyBox(door_body,2.4,2.8,0.1)
PhysicsBodyDamping door_body,0,1
hinge=CreatePhysicsHingeJoint(post_body,door_body,-1.4,1.5,0,0,1,0)

target#=0

; A spring closes the door again, like a self-closing hinge
PhysicsJointSpring hinge,True,0.5,0.3

While Not KeyDown(1)

    ; Space pushes the far edge of the door
    If KeyHit(57)
        TFormPoint 1,0,0,door,0
        PhysicsBodyImpulseAt door_body,0,0,2,TFormedX(),TFormedY(),TFormedZ()
    EndIf

    ; [ and ] move the angle the spring pulls the door towards
    If KeyHit(26) Then target=target-0.25
    If KeyHit(27) Then target=target+0.25
    PhysicsJointSpringTarget hinge,target
    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   [ / ]: target   Esc: exit"
    Text 0,20,"PhysicsJointSpringTarget hinge,"+target
    Text 0,40,"Door angle: "+PhysicsJointAngle(hinge)+" radians"

    Flip

Wend

FreePhysicsWorld world
End

Index