PhysicsJointSpring joint,enabled,hertz#[,damping_ratio#]

Parameters

joint - distance, slider or hinge joint handle

enabled - True enables the spring; False disables it

hertz# - spring frequency in cycles per second; finite and at least 0

damping_ratio# (optional) - finite, non-negative damping ratio; 0.5 (default)

Description

Configures a spring on a distance, slider or hinge joint.

A distance spring stretches and compresses around its rest length. A slider spring returns along its axis to a target translation. A hinge spring turns around its axis towards a target angle. Use these for elastic connections, suspension, self-closing doors and networks of bodies driving a deformable mesh.

Frequency controls stiffness: higher values recover faster, lower values feel softer. Start around 1 to 5 Hz. At a 60 Hz simulation rate, keep frequencies below about 15 Hz and use a fixed StepPhysics time step with several solver substeps. Box3D solves the springs inside those substeps; no per-frame spring force calculation is needed in Blitz code.

Damping 0 gives no spring damping, values between 0 and 1 oscillate while settling, 1 is critical damping, and values above 1 are overdamped. Contacts, body damping and numerical integration can also remove energy. A frequency of 0 supplies no restoring force or spring damping; it is useful with distance limits and motors.

The initial target is the distance joint's rest length, or zero translation/angle for sliders/hinges relative to their creation frames. Change it with PhysicsJointSpringTarget. Retuning or disabling a spring preserves its target, limits and motor settings. External loads such as gravity can move the equilibrium away from the unloaded target.

Disabling a distance spring restores its rigid rest-length constraint, overriding distance limits and the motor. To use a slack distance range or a motor with no restoring spring, call PhysicsJointSpring joint,True,0. Equal enabled distance limits form a rigid constraint even with the spring enabled. Disabling a slider or hinge spring removes only the spring drive; their limits and motors still work.

The command wakes both connected bodies so changes affect sleeping objects. Apply it when settings change rather than every frame if you want resting bodies to sleep. All numeric arguments are validated even when enabled is False. Ball and fixed joints are not supported and raise a runtime error.

Setting the frequency to zero clears cached spring effort for distance, slider and hinge joints. Ball angular springs use PhysicsBallJointSpring. Fixed-joint channels use PhysicsFixedJointSpring, where zero frequency instead means rigid.


See also: PhysicsJointSpringTarget, CreatePhysicsDistanceJoint, CreatePhysicsSliderJoint, CreatePhysicsHingeJoint, PhysicsJointLimits, PhysicsJointMotor.

Example

; PhysicsJointSpring Example
; --------------------------

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)

spring=True

; 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

    ; S switches the closing spring on or off
    If KeyHit(31)
        spring=Not spring
        PhysicsJointSpring hinge,spring,0.5,0.3
    EndIf
    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   S: spring   Esc: exit"
    Text 0,20,"PhysicsJointSpring hinge,"+spring+",0.5,0.3"
    Text 0,40,"The spring pulls the door back to its target angle of 0"

    Flip

Wend

FreePhysicsWorld world
End

Index