samples/JamesH/Joints
joints.abs
201 lines of ArcBasic. Words in colour that are commands link to their help page. To run it, download ArcBasic, open the file and press F5.
; Joints - ArcBasic extension sample;; Physics joints hold bodies together while letting them move:; - a rope bridge of planks, each hinged to the next; - a wrecking ball on a chain of pins, held back by a fixed joint; - a turntable on a hinge, turned by a motor; - a trampoline: a platform on a spring joint; - a puppet whose limbs hang from cone joints;; Keys: W let the wrecking ball go Space throw a ball; M turntable motor on/off D show the physics shapes; Left/Right/Up/Down move the camera Escape quitGraphics3D 800,600,0,2SetBuffer BackBuffer()Global camPivot=CreatePivot()Global camera=CreateCamera(camPivot)PositionEntity camera,0,11,-22TurnEntity camera,24,0,0CameraClsColor camera,110,160,220AmbientLight 110,110,125light=CreateLight(1)RotateEntity light,55,30,0LightShadows lightground=CreatePlane()EntityColor ground,90,140,80EntityPhysics ground,1; ---- rope bridge: two towers and a row of hinged planks ----For side=-1 To 1 Step 2 tower=CreateCube() ScaleEntity tower,.6,2.5,1.2 PositionEntity tower,-8+side*5.3,2.5,6 EntityColor tower,120,110,100 EntityPhysics tower,1NextConst PLANKS=12Dim plank(PLANKS-1)For i=0 To PLANKS-1 plank(i)=CreateCube() ScaleEntity plank(i),.38,.06,1 PositionEntity plank(i),-12.3+i*.8+.4,5,6 EntityColor plank(i),170,120,70 EntityPhysics plank(i),2,.6 x#=-12.3+i*.8 If i=0 Then HingeJoint plank(0),0,x,5,6,0,0,1 Else HingeJoint plank(i),plank(i-1),x,5,6,0,0,1NextHingeJoint plank(PLANKS-1),0,-12.3+PLANKS*.8,5,6,0,0,1; ---- wrecking ball: a chain of pins from a gantry, held back to begin with ----gantry=CreateCube()ScaleEntity gantry,3,.2,.2PositionEntity gantry,4,9,2EntityColor gantry,200,60,40EntityPhysics gantry,1Const LINKS=8Dim chain(LINKS)For i=0 To LINKS ; the chain starts pulled out to the side, ready to swing a#=60 px#=4-Sin(a)*(i+1)*.7 : py#=9-Cos(a)*(i+1)*.7 If i=LINKS chain(i)=CreateSphere(16) ScaleEntity chain(i),.9,.9,.9 EntityColor chain(i),50,50,60 EntityShininess chain(i),.8 EntityPhysics chain(i),2,40 px=4-Sin(a)*(i*.7+1.2) : py=9-Cos(a)*(i*.7+1.2) Else chain(i)=CreateSphere(8) ScaleEntity chain(i),.15,.15,.15 EntityColor chain(i),90,90,100 EntityPhysics chain(i),2,.5 EndIf PositionEntity chain(i),px,py,2 If i=0 PinJoint chain(0),0,4,9,2 Else PinJoint chain(i),chain(i-1),EntityX(chain(i-1)),EntityY(chain(i-1)),2 EndIfNextholdBack=FixedJoint(chain(LINKS),0); crates for it to hitFor i=0 To 9 c=CreateCube() ScaleEntity c,.45,.45,.45 PositionEntity c,5.6+(i Mod 2)*.92,.45+(i/2)*.92,2 EntityColor c,230,200,150 EntityPhysics c,2,1.5Next; ---- turntable: a disc on an upright hinge, turned by a motor ----table=CreateCylinder(24)ScaleEntity table,2.2,.12,2.2PositionEntity table,10,.2,-3EntityColor table,80,80,160EntityPhysics table,2,20EntityShape table,4spin=HingeJoint(table,0,10,.2,-3,0,1,0)JointMotor spin,45,2000For i=0 To 4 c=CreateCube() ScaleEntity c,.3,.3,.3 PositionEntity c,10+Cos(i*72)*1.4,.65,-3+Sin(i*72)*1.4 EntityColor c,255,220,90 EntityPhysics c,2,.5Nextmotor=True; ---- trampoline: a platform on a spring ----frame=CreateCube()ScaleEntity frame,1.6,.3,1.6PositionEntity frame,-3,.3,-4EntityColor frame,60,60,60EntityPhysics frame,1pad=CreateCube()ScaleEntity pad,1.4,.08,1.4PositionEntity pad,-3,1.2,-4EntityColor pad,60,220,140EntityPhysics pad,2,2bounce=SpringJoint(pad,0,0,1,0,260,1)JointLimits bounce,-.6,1; ---- puppet: a body hung from a pin, limbs on cone joints ----torso=CreateCube()ScaleEntity torso,.45,.7,.25PositionEntity torso,-14,6,-2EntityColor torso,240,90,120EntityPhysics torso,2,2PinJoint torso,0,-14,6.9,-2head=CreateSphere(12)ScaleEntity head,.35,.35,.35PositionEntity head,-14,7.1,-2EntityColor head,255,220,190EntityPhysics head,2,.5ConeJoint head,torso,-14,6.75,-2,0,1,0,25,20Dim limb(3)For i=0 To 3 limb(i)=CreateCube() ScaleEntity limb(i),.12,.45,.12 EntityColor limb(i),255,220,190 sx#=-.6+(i Mod 2)*1.2 If i<2 Then sy#=6.55 Else sy#=5.3 If i>=2 Then sx=-.25+(i Mod 2)*.5 PositionEntity limb(i),-14+sx,sy-.45,-2 EntityPhysics limb(i),2,.4 ConeJoint limb(i),torso,-14+sx,sy,-2,0,-1,0,70,20NextType Ball Field e, lifeEnd Typedebug=FalseWhile Not KeyHit(1) If KeyDown(203) Then TurnEntity camPivot,0,1.5,0 If KeyDown(205) Then TurnEntity camPivot,0,-1.5,0 If KeyDown(200) And EntityZ(camera)<-10 Then MoveEntity camera,0,0,.4 If KeyDown(208) And EntityZ(camera)>-45 Then MoveEntity camera,0,0,-.4 If KeyHit(17) And holdBack Then FreeJoint holdBack : holdBack=0 If KeyHit(50) motor=Not motor If motor Then JointMotor spin,45,2000 Else JointMotor spin,0,0 EndIf If KeyHit(32) Then debug=Not debug : PhysicsDebug debug If KeyHit(57) b.Ball=New Ball b\e=CreateSphere(12) b\life=600 ScaleEntity b\e,.35,.35,.35 EntityColor b\e,Rand(100,255),Rand(100,255),Rand(100,255) PositionEntity b\e,EntityX(camera,True),EntityY(camera,True),EntityZ(camera,True) EntityPhysics b\e,2,2 EntityShape b\e,2 EntityBounce b\e,.5 TFormVector 0,.1,1,camera,0 SetEntityVelocity b\e,TFormedX()*28,TFormedY()*28,TFormedZ()*28 EndIf UpdateWorld For b.Ball=Each Ball b\life=b\life-1 If b\life<0 Then FreeEntity b\e : Delete b Next RenderWorld Color 0,0,0 Text 10,10,"JOINTS (ArcBasic extension)" If holdBack Then w$="W let the wrecking ball go" Else w$="the wrecking ball is swinging" Text 10,30,w$+" Space throw a ball" If motor Then m$="on" Else m$="off" Text 10,45,"M turntable motor: "+m$+" ("+Int(JointPosition(spin))+" deg) D shapes" Text 10,60,"Trampoline stretch: "+JointPosition(bounce) FlipWendEnd
