; 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 shapes Graphics3D 800,600,0,2 SetBuffer BackBuffer() AmbientLight 110,110,125 light=CreateLight() RotateEntity light,55,35,0 LightShadows light ground=CreatePlane() EntityColor ground,90,150,90 EntityPhysics ground,1 ; ---- the playground ---- ; stairs up to a platform: each step is low enough to walk up For 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,1 Next platform=CreateCube() ScaleEntity platform,2,.7,3 PositionEntity platform,-10,.7,12 EntityColor platform,170,170,190 EntityPhysics platform,1 ; a ramp to jump the car off ramp=CreateCube() ScaleEntity ramp,3,.2,6 PositionEntity ramp,12,1.1,10 RotateEntity ramp,-12,0,0 EntityColor ramp,190,150,100 EntityPhysics ramp,1 ; a row of cones across the car's path and a stack of boxes to knock about cone=LoadMesh("car\cone.glb") ScaleEntity cone,3,3,3 HideEntity cone For i=0 To 7 c=CopyEntity(cone) PositionEntity c,9+i*.8,0,1+i*1.6 EntityPhysics c,2,.3 Next box=LoadMesh("car\box.glb") ScaleEntity box,3,3,3 HideEntity box h#=MeshHeight(box)*3 For 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 Next Next ; a flag of cloth on a pole, blowing in the breeze pole=CreateCylinder(8) ScaleEntity pole,.08,3.5,.08 PositionEntity pole,-4,3.5,10 EntityColor pole,220,220,220 EntityPhysics pole,1 Const N=12 flag=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) Next Next For 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 Next Next UpdateNormals flag EntityColor flag,230,60,80 EntityFX flag,16 EntitySoftBody flag,1,.9 For y=0 To N SoftBodyPin flag,y*(N+1) Next ; balloons: soft spheres full of air For 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,3 Next ; ---- the dummies: characters that go limp when knocked over ---- Const DUMMIES=4 Dim 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.9 Next StandDummies() ; ---- the player ---- Global player=LoadAnimMesh("mini\character-knight.glb") ScaleEntity player,3,3,3 PositionEntity player,0,0,-3 EntityCharacter player,.4,1.9 Global anim$="", shoveTime=0 PlayerAnim("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,3 PositionEntity car,6,.5,-1 TurnEntity car,0,-30,0 EntityPhysics car,2,15 AddWheel car,FindChild(car,"wheel-front-left"),True,False AddWheel car,FindChild(car,"wheel-front-right"),True,False AddWheel car,FindChild(car,"wheel-back-left"),False,True AddWheel car,FindChild(car,"wheel-back-right"),False,True VehicleEngine car,220,32 ; ---- the camera follows whoever you are ---- camera=CreateCamera() CameraRange camera,.1,500 look=CreatePivot() Global camYaw#=0 driving=False debug=False While 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" Flip Wend End ; 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") Next End 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 d180 : 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,look End Function