; Physics Toys - ArcBasic extension sample ; ; More physics: bodies built from several shapes, physics types that pass ; through each other, triggers, line picks, overlap tests and gravity scale. ; - the table is one body made of five boxes, so balls roll underneath ; - blue ghost balls pass through the glass; solid balls bounce off it ; - the goal behind the glass is a trigger that counts what is inside it ; - the laser finds the first body in its way with PhysicsLinePick ; - a blast throws everything near where you look (PhysicsOverlap) ; - balloons float up: their gravity is turned upside down ; ; Keys: Space throw a ball G throw a ghost F let a balloon go ; B blast where you look D show the physics shapes ; Left/Right/Up/Down move the camera Escape quit Graphics3D 800,600,0,2 SetBuffer BackBuffer() Const TYPE_SOLID=1, TYPE_GHOST=2, TYPE_GLASS=3 Type Toy Field e, life End Type Global camPivot=CreatePivot() Global camera=CreateCamera(camPivot) PositionEntity camera,0,7,-20 TurnEntity camera,15,0,0 CameraClsColor camera,30,40,60 AmbientLight 100,100,120 light=CreateLight(1) RotateEntity light,55,30,0 LightShadows light ground=CreatePlane() EntityColor ground,70,110,80 EntityPhysics ground,1 ; glass: ghosts pass through it, everything else bounces off glass=CreateCube() ScaleEntity glass,8,3,.1 PositionEntity glass,0,3,8 EntityColor glass,160,200,255 EntityAlpha glass,.35 EntityPhysics glass,1 EntityShape glass,1 EntityPhysicsType glass,TYPE_GLASS PhysicsCollide TYPE_GHOST,TYPE_GLASS,False ; the goal behind the glass: a trigger with a floor drawn under it goalFloor=CreateCube() ScaleEntity goalFloor,8,.05,3 PositionEntity goalFloor,0,.05,11.5 EntityColor goalFloor,220,190,60 ; low walls keep what gets into the goal there (ghosts too) For i=0 To 2 w=CreateCube() If i=0 Then ScaleEntity w,8,.5,.1 : PositionEntity w,0,.5,14.6 If i>0 Then ScaleEntity w,.1,.5,3.3 : PositionEntity w,(i*2-3)*8.1,.5,11.4 EntityColor w,190,160,50 EntityPhysics w,1 EntityShape w,1 Next goal=CreatePivot() PositionEntity goal,0,1.5,11.5 EntityPhysics goal,4 EntityShape goal,1,15.6,3,5.6 ; a table: a top and four legs drawn as children, one body of five boxes table=CreatePivot() top=CreateCube(table) : ScaleEntity top,2.5,.1,1.2 : PositionEntity top,0,2,0 EntityColor top,150,100,60 For x=-1 To 1 Step 2 For z=-1 To 1 Step 2 leg=CreateCube(table) : ScaleEntity leg,.12,1,.12 : PositionEntity leg,x*2.3,1,z*1 EntityColor leg,120,80,50 Next Next PositionEntity table,-6,0,0 EntityPhysics table,2,8 EntityShape table,1,5,.2,2.4,0,2,0 For x=-1 To 1 Step 2 For z=-1 To 1 Step 2 AddEntityShape table,1,.24,2,.24,x*2.3,1,z*1 Next Next ; something for the laser and the blasts to knock about For i=0 To 11 c=CreateCube() ScaleEntity c,.45,.45,.45 PositionEntity c,4+(i Mod 3)*.95,.45+(i/3)*.92,1 EntityColor c,230,230,230 EntityPhysics c,2 Next ; the laser: a turning post that shows what it hits laser=CreateCylinder(12) ScaleEntity laser,.2,.6,.2 PositionEntity laser,9,.6,-4 EntityColor laser,60,60,60 EntityPhysics laser,1 beam=CreateCube() EntityColor beam,255,40,40 EntityFX beam,1 EntityGlow beam,2 spot=CreateSphere(8) ScaleEntity spot,.15,.15,.15 EntityColor spot,255,80,80 EntityFX spot,1 EntityGlow spot,3 laserYaw#=60 marker=CreateSphere(12) ScaleEntity marker,.2,.2,.2 EntityColor marker,255,255,0 EntityFX marker,1 debug=False 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)<-8 Then MoveEntity camera,0,0,.4 If KeyDown(208) And EntityZ(camera)>-40 Then MoveEntity camera,0,0,-.4 If KeyHit(57) Then Throw(False) If KeyHit(34) Then Throw(True) If KeyHit(33) Then Balloon() If KeyHit(32) debug=Not debug PhysicsDebug debug EndIf ; where the camera looks: the first physics body straight ahead TFormVector 0,0,100,camera,0 aimed=PhysicsLinePick(EntityX(camera,True),EntityY(camera,True),EntityZ(camera,True),TFormedX(),TFormedY(),TFormedZ()) If aimed Then PositionEntity marker,PickedX(),PickedY(),PickedZ() If KeyHit(48) And aimed ; blast: everything within 4 units is thrown away from the middle bx#=PickedX() : by#=PickedY() : bz#=PickedZ() For i=1 To PhysicsOverlap(bx,by,bz,4) e=OverlapEntity(i) If GetEntityPhysics(e)=2 dx#=EntityX(e)-bx : dy#=EntityY(e)-by+1 : dz#=EntityZ(e)-bz d#=Sqr(dx*dx+dy*dy+dz*dz)+.5 ApplyImpulse e,dx/d*6,dy/d*6+3,dz/d*6 EndIf Next EndIf ; the laser sweeps to and fro, nudging what it hits laserYaw=laserYaw+.6 ly#=-60+Abs((laserYaw Mod 240)-120) dx#=-Sin(ly)*30 : dz#=Cos(ly)*30 hit=PhysicsLinePick(9,.9,-4,dx,0,dz) length#=30 If hit length=PickedTime()*30 If GetEntityPhysics(hit)=2 Then ApplyForce hit,dx/30*3,0,dz/30*3 EndIf PositionEntity beam,9,.9,-4 RotateEntity beam,0,ly,0 ScaleEntity beam,.03,.03,length/2 MoveEntity beam,0,0,length/2 PositionEntity spot,9-Sin(ly)*length,.9,-4+Cos(ly)*length UpdateWorld ; old toys go For t.Toy=Each Toy t\life=t\life-1 If t\life<0 Or EntityY(t\e)>40 FreeEntity t\e Delete t EndIf Next ghosts=0 : solids=0 For i=1 To TriggerCount(goal) If GetEntityPhysicsType(TriggerEntity(goal,i))=TYPE_GHOST Then ghosts=ghosts+1 Else solids=solids+1 Next RenderWorld Color 255,255,255 Text 10,10,"PHYSICS TOYS (ArcBasic extension)" Text 10,30,"Space ball G ghost F balloon B blast D shapes: "+OnOff$(debug) Text 10,45,"In the goal: "+ghosts+" ghosts, "+solids+" others" Flip Wend End Function Throw(ghost) t.Toy=New Toy t\life=60*40 t\e=CreateSphere(12) ScaleEntity t\e,.4,.4,.4 PositionEntity t\e,EntityX(camera,True),EntityY(camera,True),EntityZ(camera,True) EntityPhysics t\e,2,2 EntityShape t\e,2 EntityBounce t\e,.4 If ghost EntityPhysicsType t\e,TYPE_GHOST EntityColor t\e,90,150,255 EntityAlpha t\e,.7 Else EntityColor t\e,255,140,60 EndIf TFormVector 0,.12,1,camera,0 SetEntityVelocity t\e,TFormedX()*28,TFormedY()*28,TFormedZ()*28 End Function Function Balloon() t.Toy=New Toy t\life=60*30 t\e=CreateSphere(12) ScaleEntity t\e,.7,.9,.7 PositionEntity t\e,Rnd(-4,4),1,Rnd(-6,-2) EntityColor t\e,Rand(150,255),Rand(60,200),Rand(60,200) EntityPhysics t\e,2,.2 EntityShape t\e,2 EntityGravity t\e,-.15 EntityDamping t\e,.6,.6 ApplyImpulse t\e,Rnd(-.1,.1),0,Rnd(-.1,.1) End Function Function OnOff$(v) If v Then Return "on" Return "off" End Function