; 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 quit Graphics3D 800,600,0,2 SetBuffer BackBuffer() Global camPivot=CreatePivot() Global camera=CreateCamera(camPivot) PositionEntity camera,0,11,-22 TurnEntity camera,24,0,0 CameraClsColor camera,110,160,220 AmbientLight 110,110,125 light=CreateLight(1) RotateEntity light,55,30,0 LightShadows light ground=CreatePlane() EntityColor ground,90,140,80 EntityPhysics 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,1 Next Const PLANKS=12 Dim 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,1 Next HingeJoint 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,.2 PositionEntity gantry,4,9,2 EntityColor gantry,200,60,40 EntityPhysics gantry,1 Const LINKS=8 Dim 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 EndIf Next holdBack=FixedJoint(chain(LINKS),0) ; crates for it to hit For 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.5 Next ; ---- turntable: a disc on an upright hinge, turned by a motor ---- table=CreateCylinder(24) ScaleEntity table,2.2,.12,2.2 PositionEntity table,10,.2,-3 EntityColor table,80,80,160 EntityPhysics table,2,20 EntityShape table,4 spin=HingeJoint(table,0,10,.2,-3,0,1,0) JointMotor spin,45,2000 For 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,.5 Next motor=True ; ---- trampoline: a platform on a spring ---- frame=CreateCube() ScaleEntity frame,1.6,.3,1.6 PositionEntity frame,-3,.3,-4 EntityColor frame,60,60,60 EntityPhysics frame,1 pad=CreateCube() ScaleEntity pad,1.4,.08,1.4 PositionEntity pad,-3,1.2,-4 EntityColor pad,60,220,140 EntityPhysics pad,2,2 bounce=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,.25 PositionEntity torso,-14,6,-2 EntityColor torso,240,90,120 EntityPhysics torso,2,2 PinJoint torso,0,-14,6.9,-2 head=CreateSphere(12) ScaleEntity head,.35,.35,.35 PositionEntity head,-14,7.1,-2 EntityColor head,255,220,190 EntityPhysics head,2,.5 ConeJoint head,torso,-14,6.75,-2,0,1,0,25,20 Dim 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,20 Next Type Ball Field e, life End Type debug=False While 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) Flip Wend End