PhysicsJointLimits joint,enabled,lower#,upper#
Parameters
|
joint - physics-joint handle from a joint creation command enabled - True switches the limit on, False switches it off lower# - lower bound of the allowed range; must not exceed upper# upper# - upper bound of the allowed range |
Description
|
Enables or disables travel limits on a distance, slider or hinge joint. What the range means depends on the joint. For a distance joint it is a minimum and maximum length in metres (both at least 0) - slack rope behaviour. For a slider it is the travel in metres along the axis, either side of the anchor - how far the drawer opens. For a hinge it is the swing in radians (about 1.571 is 90 degrees) - a door that stops at the wall. Limits are what turn free-moving joints into believable mechanisms, and they pair naturally with PhysicsJointMotor: the motor pushes, the limit says where to stop. Ball and fixed joints do not support limits - calling this on one raises a runtime error, as do reversed bounds. Requires Extended mode. See also: PhysicsJointMotor, CreatePhysicsDistanceJoint, CreatePhysicsHingeJoint, CreatePhysicsSliderJoint, FreePhysicsJoint. |
Example
; PhysicsJointLimits Example ; -------------------------- ; Requires Extended mode. Graphics3D 640,480,0,2 SetBuffer BackBuffer() Const PHYSICS_STATIC=0 Const PHYSICS_DYNAMIC=2 camera=CreateCamera() PositionEntity camera,0,2,-4 light=CreateLight() RotateEntity light,45,-30,0 AmbientLight 50,50,50 world=CreatePhysicsWorld() PhysicsGravity world,0,-9.81,0 ; Static anchor block the pendulum hangs from anchor=CreateCube() ScaleEntity anchor,0.4,0.4,0.4 PositionEntity anchor,0,3,5,True EntityColor anchor,80,160,255 anchor_body=CreatePhysicsBody(world,anchor,PHYSICS_STATIC) anchor_shape=PhysicsBodyBox(anchor_body,0.8,0.8,0.8,0) ; Dynamic pendulum link hanging below the anchor link=CreateCube() ScaleEntity link,0.35,1.5,0.35 PositionEntity link,0,1.5,5,True EntityColor link,255,120,70 link_body=CreatePhysicsBody(world,link,PHYSICS_DYNAMIC) link_shape=PhysicsBodyBox(link_body,0.7,3,0.7,1) ; Hinge the link to the anchor around the Z axis hinge=CreatePhysicsHingeJoint(anchor_body,link_body,0,3,5,0,0,1) ; Start the pendulum swinging PhysicsBodyAngularVelocity link_body,0,0,3 limit#=0.8 limits_on=1 While Not KeyDown(1) ; Space toggles the limits; [ and ] change the allowed range If KeyHit(57) Then limits_on=1-limits_on If KeyDown(26) And limit>0.2 Then limit=limit-0.01 If KeyDown(27) And limit<3 Then limit=limit+0.01 ; Apply the ordered hinge limits (radians either side of rest) PhysicsJointLimits hinge,limits_on,-limit,limit ; K kicks the pendulum swinging again If KeyHit(37) Then PhysicsBodyAngularVelocity link_body,0,0,3 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,"Space: toggle limits [ / ]: range K: kick Arrows: camera Esc: exit" Text 0,20,"PhysicsJointLimits hinge,"+limits_on+","+(-limit)+","+limit Text 0,40,"Pendulum roll: "+EntityRoll(link,True)+" degrees" Flip Wend FreePhysicsWorld world End
Index