; Physics - ArcBasic extension sample ; ; Rigid bodies: give an entity a body with EntityPhysics and UpdateWorld ; moves it - falling, sliding, tumbling and bouncing off everything else. ; static (1) never moves: the ground, the ramp ; dynamic (2) moved by the simulation: crates, barrels, dominoes, balls ; kinematic (3) moved by the program, pushing what it meets: the sweeper ; Contacts tell you what touched what, and how hard: crates flash when hit. ; ; Keys: Space throw a ball Left/Right/Up/Down move the camera ; S sweeper on/off G moon gravity on/off ; R rebuild Escape quit Graphics3D 800,600,0,2 SetBuffer BackBuffer() Type Ball Field e End Type Global camPivot=CreatePivot() Global camera=CreateCamera(camPivot) PositionEntity camera,0,6,-22 TurnEntity camera,10,0,0 CameraClsColor camera,120,170,230 AmbientLight 110,110,120 light=CreateLight(1) RotateEntity light,55,30,0 LightShadows light ; the static scenery ground=CreatePlane() EntityColor ground,90,150,70 EntityPhysics ground,1 ramp=CreateCube() ScaleEntity ramp,3,.25,7 PositionEntity ramp,-12,2.2,4 RotateEntity ramp,-20,0,0 EntityColor ramp,170,140,100 EntityPhysics ramp,1 ; models used as templates for the crates and barrels Global crateModel=LoadMesh("models\crate.glb") ScaleEntity crateModel,3,3,3 HideEntity crateModel Global barrelModel=LoadMesh("models\barrel.glb") ScaleEntity barrelModel,2,2,2 HideEntity barrelModel Const CRATES=15 Dim crate(CRATES-1), flash#(CRATES-1) Dim barrel(5) Dim domino(13) Global sweeper BuildScene() moon=False : sweeping=True While Not KeyHit(1) ; camera 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(57) Then ThrowBall() If KeyHit(19) Then BuildScene() If KeyHit(31) Then sweeping=Not sweeping If KeyHit(34) moon=Not moon If moon Then PhysicsGravity 0,-1.6,0 Else PhysicsGravity 0,-9.8,0 EndIf ; a kinematic body is moved by the program; physics pushes things out of its way If sweeping Then TurnEntity sweeper,0,1.2,0 UpdateWorld ; contacts: crates flash red when something hits them hard For i=0 To CRATES-1 For c=1 To CountContacts(crate(i)) If ContactImpulse(crate(i),c)>4 Then flash(i)=1 Next If flash(i)>0 flash(i)=flash(i)-.04 EntityColor crate(i),255,255-200*flash(i),255-200*flash(i) EndIf Next RenderWorld Color 0,0,0 Text 10,10,"PHYSICS (ArcBasic extension)" Text 10,30,"Space throw a ball Arrows camera" If sweeping Then s$="on" Else s$="off" If moon Then g$="moon" Else g$="earth" Text 10,45,"S sweeper: "+s$+" G gravity: "+g$ Text 10,60,"R rebuild Esc quit" Flip Wend End Function BuildScene() For b.Ball=Each Ball FreeEntity b\e Delete b Next For i=0 To CRATES-1 If crate(i) Then FreeEntity crate(i) Next For i=0 To 5 If barrel(i) Then FreeEntity barrel(i) Next For i=0 To 13 If domino(i) Then FreeEntity domino(i) Next If sweeper Then FreeEntity sweeper ; a tower of crates: copies of a loaded model, each with a hull shape For i=0 To CRATES-1 crate(i)=CopyEntity(crateModel) PositionEntity crate(i),(i Mod 3)*1.53-1.53,.75+(i/3)*1.51,6 EntityPhysics crate(i),2,2 flash(i)=0 Next ; barrels roll: give them a cylinder shape, turned on their side For i=0 To 5 barrel(i)=CopyEntity(barrelModel) PositionEntity barrel(i),-12.5+(i Mod 2),5.5+(i/2)*1.2,8+(i/2)*1.5 RotateEntity barrel(i),0,0,90 EntityPhysics barrel(i),2,4 EntityShape barrel(i),4 EntityFriction barrel(i),.6 Next ; dominoes in an arc For i=0 To 13 domino(i)=CreateCube() ScaleEntity domino(i),.12,1.2,.5 a#=i*10-40 PositionEntity domino(i),9+Sin(a)*6,1.2,4+Cos(a)*6 RotateEntity domino(i),0,-a,0 EntityColor domino(i),240,240,240-i*12 EntityPhysics domino(i),2,.5 Next ApplyImpulse domino(0),Cos(-40)*.6,0,-Sin(-40)*.6,0,1,0 ; the sweeper: a kinematic bar the program turns sweeper=CreateCube() ScaleEntity sweeper,7,.4,.4 PositionEntity sweeper,0,.4,-6 EntityColor sweeper,200,60,40 EntityPhysics sweeper,3 End Function Function ThrowBall() b.Ball=New Ball b\e=CreateSphere(12) ScaleEntity b\e,.45,.45,.45 EntityColor b\e,Rand(80,255),Rand(80,255),Rand(80,255) PositionEntity b\e,EntityX(camera,True),EntityY(camera,True),EntityZ(camera,True) EntityPhysics b\e,2,3 EntityBounce b\e,.4 TFormVector 0,.1,1,camera,0 SetEntityVelocity b\e,TFormedX()*32,TFormedY()*32,TFormedZ()*32 SetEntitySpin b\e,Rand(-360,360),0,0 ; keep the 25 newest balls n=0 For o.Ball=Each Ball n=n+1 Next If n>25 o.Ball=First Ball FreeEntity o\e Delete o EndIf End Function