ArcBasicDocs

samples/JamesH/PhysicsToys

physics_toys.abs

232 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.

Download physics_toys.absAll files in this folder

physics_toys.abs
; 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  quitGraphics3D 800,600,0,2SetBuffer BackBuffer()Const TYPE_SOLID=1, TYPE_GHOST=2, TYPE_GLASS=3Type Toy	Field e, lifeEnd TypeGlobal camPivot=CreatePivot()Global camera=CreateCamera(camPivot)PositionEntity camera,0,7,-20TurnEntity camera,15,0,0CameraClsColor camera,30,40,60AmbientLight 100,100,120light=CreateLight(1)RotateEntity light,55,30,0LightShadows lightground=CreatePlane()EntityColor ground,70,110,80EntityPhysics ground,1; glass: ghosts pass through it, everything else bounces offglass=CreateCube()ScaleEntity glass,8,3,.1PositionEntity glass,0,3,8EntityColor glass,160,200,255EntityAlpha glass,.35EntityPhysics glass,1EntityShape glass,1EntityPhysicsType glass,TYPE_GLASSPhysicsCollide TYPE_GHOST,TYPE_GLASS,False; the goal behind the glass: a trigger with a floor drawn under itgoalFloor=CreateCube()ScaleEntity goalFloor,8,.05,3PositionEntity goalFloor,0,.05,11.5EntityColor 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,1Nextgoal=CreatePivot()PositionEntity goal,0,1.5,11.5EntityPhysics goal,4EntityShape goal,1,15.6,3,5.6; a table: a top and four legs drawn as children, one body of five boxestable=CreatePivot()top=CreateCube(table) : ScaleEntity top,2.5,.1,1.2 : PositionEntity top,0,2,0EntityColor top,150,100,60For 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	NextNextPositionEntity table,-6,0,0EntityPhysics table,2,8EntityShape table,1,5,.2,2.4,0,2,0For 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	NextNext; something for the laser and the blasts to knock aboutFor 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,2Next; the laser: a turning post that shows what it hitslaser=CreateCylinder(12)ScaleEntity laser,.2,.6,.2PositionEntity laser,9,.6,-4EntityColor laser,60,60,60EntityPhysics laser,1beam=CreateCube()EntityColor beam,255,40,40EntityFX beam,1EntityGlow beam,2spot=CreateSphere(8)ScaleEntity spot,.15,.15,.15EntityColor spot,255,80,80EntityFX spot,1EntityGlow spot,3laserYaw#=60marker=CreateSphere(12)ScaleEntity marker,.2,.2,.2EntityColor marker,255,255,0EntityFX marker,1debug=FalseWhile 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"	FlipWendEndFunction 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()*28End FunctionFunction 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 FunctionFunction OnOff$(v)	If v Then Return "on"	Return "off"End Function