samples/JamesH/Playground
playground.abs
274 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.
; Physics Playground - an ArcBasic sample; Characters, vehicles, soft bodies and ragdolls together in one little world.;; On foot: Arrows walk and turn Space jumps Q shoves a dummy E gets in the car; Driving: Arrows drive and steer Space brakes E gets out F puts the car back on its wheels; Any time: R stands the dummies back up D shows the physics shapesGraphics3D 800,600,0,2SetBuffer BackBuffer()AmbientLight 110,110,125light=CreateLight()RotateEntity light,55,35,0LightShadows lightground=CreatePlane()EntityColor ground,90,150,90EntityPhysics ground,1; ---- the playground ----; stairs up to a platform: each step is low enough to walk upFor i=1 To 5 s=CreateCube() ScaleEntity s,2,i*.14,.6 PositionEntity s,-10,i*.14,3+i*1.2 EntityColor s,170,170,190 EntityPhysics s,1Nextplatform=CreateCube()ScaleEntity platform,2,.7,3PositionEntity platform,-10,.7,12EntityColor platform,170,170,190EntityPhysics platform,1; a ramp to jump the car offramp=CreateCube()ScaleEntity ramp,3,.2,6PositionEntity ramp,12,1.1,10RotateEntity ramp,-12,0,0EntityColor ramp,190,150,100EntityPhysics ramp,1; a row of cones across the car's path and a stack of boxes to knock aboutcone=LoadMesh("car\cone.glb")ScaleEntity cone,3,3,3HideEntity coneFor i=0 To 7 c=CopyEntity(cone) PositionEntity c,9+i*.8,0,1+i*1.6 EntityPhysics c,2,.3Nextbox=LoadMesh("car\box.glb")ScaleEntity box,3,3,3HideEntity boxh#=MeshHeight(box)*3For i=0 To 3 For j=0 To 3-i b=CopyEntity(box) PositionEntity b,-8+j*(h+.05)+i*h/2,i*h+.01,0 EntityPhysics b,2,1 NextNext; a flag of cloth on a pole, blowing in the breezepole=CreateCylinder(8)ScaleEntity pole,.08,3.5,.08PositionEntity pole,-4,3.5,10EntityColor pole,220,220,220EntityPhysics pole,1Const N=12flag=CreateMesh()surf=CreateSurface(flag)For y=0 To N For x=0 To N AddVertex surf,-3.9+x*3.0/N,6.8-y*2.0/N,10,x/Float(N),y/Float(N) NextNextFor y=0 To N-1 For x=0 To N-1 v=y*(N+1)+x AddTriangle surf,v,v+1,v+N+2 AddTriangle surf,v,v+N+2,v+N+1 NextNextUpdateNormals flagEntityColor flag,230,60,80EntityFX flag,16EntitySoftBody flag,1,.9For y=0 To N SoftBodyPin flag,y*(N+1)Next; balloons: soft spheres full of airFor i=0 To 2 balloon=CreateSphere(12) PositionEntity balloon,2+i*2.5,4+i*2,8 EntityColor balloon,Rand(80,255),Rand(80,255),255 EntitySoftBody balloon,.5,.6,3Next; ---- the dummies: characters that go limp when knocked over ----Const DUMMIES=4Dim dummy(DUMMIES), dummyX#(DUMMIES), dummyZ#(DUMMIES)Dim dummyFile$(DUMMIES)dummyFile$(0)="alien" : dummyFile$(1)="robot" : dummyFile$(2)="soldier"dummyFile$(3)="female-a" : dummyFile$(4)="male-b"For i=0 To DUMMIES dummy(i)=LoadAnimMesh("mini\character-"+dummyFile$(i)+".glb") ScaleEntity dummy(i),3,3,3 dummyX(i)=-2+i*2.2 : dummyZ(i)=3 EntityCharacter dummy(i),.4,1.9NextStandDummies(); ---- the player ----Global player=LoadAnimMesh("mini\character-knight.glb")ScaleEntity player,3,3,3PositionEntity player,0,0,-3EntityCharacter player,.4,1.9Global anim$="", shoveTime=0PlayerAnim("idle"); ---- the car: a dynamic body on four wheels, which are the model's own wheel parts ----car=LoadAnimMesh("car\sedan.glb")ScaleEntity car,3,3,3PositionEntity car,6,.5,-1TurnEntity car,0,-30,0EntityPhysics car,2,15AddWheel car,FindChild(car,"wheel-front-left"),True,FalseAddWheel car,FindChild(car,"wheel-front-right"),True,FalseAddWheel car,FindChild(car,"wheel-back-left"),False,TrueAddWheel car,FindChild(car,"wheel-back-right"),False,TrueVehicleEngine car,220,32; ---- the camera follows whoever you are ----camera=CreateCamera()CameraRange camera,.1,500look=CreatePivot()Global camYaw#=0driving=Falsedebug=FalseWhile Not KeyHit(1) If driving VehicleControl car,KeyDown(200)-KeyDown(208),KeyDown(205)-KeyDown(203),KeyDown(57) If KeyHit(33) ; back on its wheels PositionEntity car,EntityX(car),EntityY(car)+1.5,EntityZ(car) RotateEntity car,0,EntityYaw(car),0 SetEntityVelocity car,0,0,0 EndIf If KeyHit(18) ; get out beside the driver's door driving=False VehicleControl car,0,0,1 TFormPoint 2.5,0,0,car,0 PositionEntity player,TFormedX(),EntityY(car)+.5,TFormedZ() RotateEntity player,0,EntityYaw(car),0 ShowEntity player EndIf Else If KeyDown(203) Then TurnEntity player,0,3,0 If KeyDown(205) Then TurnEntity player,0,-3,0 speed#=0 If KeyDown(200) Then speed=5 If KeyDown(208) Then speed=-2.5 CharacterWalk player,0,speed If KeyHit(57) Then CharacterJump player,6 If KeyHit(16) Then Shove() ; getting in hides the player, which takes its body out of the world too If KeyHit(18) And EntityDistance(player,car)<4 driving=True CharacterWalk player,0,0 HideEntity player EndIf If shoveTime>0 shoveTime=shoveTime-1 ; let the shove play out ElseIf Not CharacterOnFloor(player) PlayerAnim("jump") ElseIf speed<>0 PlayerAnim("walk") Else PlayerAnim("idle") EndIf EndIf If KeyHit(19) Then StandDummies() If KeyHit(32) Then debug=Not debug : PhysicsDebug debug ; a light breeze on the flag ApplyForce flag,1,0,.6 ; how the car was moving before this update: a dummy standing in its way ; is as solid as a wall until it has been knocked over vx#=EntityVelocityX(car) : vy#=EntityVelocityY(car) : vz#=EntityVelocityZ(car) UpdateWorld ; the car knocks over the dummies it runs into, and keeps going If vx*vx+vz*vz>4 For c=1 To CountContacts(car) hit=ContactEntity(car,c) For i=0 To DUMMIES If hit=dummy(i) And RagdollParts(dummy(i))=0 EntityRagdoll dummy(i),1,60 SetEntityVelocity dummy(i),vx*1.2,4,vz*1.2 SetEntityVelocity car,vx*.8,vy,vz*.8 EndIf Next Next EndIf If driving Then target=car Else target=player FollowCamera(camera,look,target,driving) RenderWorld Text 0,0,"Physics Playground" If driving Text 0,15,"Arrows: drive and steer Space: brake E: get out F: back on its wheels" Text 0,30,"Speed: "+Int(Abs(VehicleSpeed(car))) Else Text 0,15,"Arrows: walk and turn Space: jump Q: shove a dummy E: get in the car" EndIf Text 0,45,"R: stand the dummies up D: physics shapes" FlipWendEnd; Puts every dummy back on its feet where it started.Function StandDummies() For i=0 To DUMMIES EntityRagdoll dummy(i),False PositionEntity dummy(i),dummyX(i),0,dummyZ(i) RotateEntity dummy(i),0,180,0 Animate dummy(i),1,Rnd(.8,1.2),FindAnimSeq(dummy(i),"idle") NextEnd Function; Shoves the nearest dummy in reach: it goes limp and flies off the way the player faces.Function Shove() best=-1 : bestD#=3 For i=0 To DUMMIES d#=EntityDistance(player,dummy(i)) If d<bestD And RagdollParts(dummy(i))=0 Then best=i : bestD=d Next If best<0 Then Return PlayerAnim("attack-melee-right") shoveTime=30 EntityRagdoll dummy(best),1,60 TFormVector 0,.4,1,player,0 ; push at chest height, so it topples backwards ApplyImpulse dummy(best),TFormedX()*100,TFormedY()*100,TFormedZ()*100,0,1.3,0End FunctionFunction PlayerAnim(name$) If name$=anim$ Then Return anim$=name$ mode=1 If name$="attack-melee-right" Or name$="jump" Then mode=3 Animate player,mode,1,FindAnimSeq(player,name$),6End Function; Swings the camera round behind the target, a little above it.Function FollowCamera(camera,look,target,far) dist#=8 : height#=3.5 If far Then dist=12 : height=5 diff#=EntityYaw(target)-camYaw While diff>180 : diff=diff-360 : Wend While diff<-180 : diff=diff+360 : Wend camYaw=camYaw+diff*.06 PositionEntity look,EntityX(target),EntityY(target)+1.2,EntityZ(target) PositionEntity camera,EntityX(look)+Sin(camYaw)*dist,EntityY(look)+height,EntityZ(look)-Cos(camYaw)*dist PointEntity camera,lookEnd Function
