ON ERROR IF ERR = 17 CHAIN @lib$+"../examples/tools/touchide" ELSE REPORT : END REM Demonstration of the Box2D (v2.3.1) Physics Engine REM by David Marples, with guidance from some RTR examples REM Now with somewhat fancy graphics, as requested REM This version adapted to run in both BB4W and BBCSDL MODE 21 : OFF Width% = 800 Height% = 600 INSTALL @lib$+"box2dlib" : PROC_b2Init INSTALL @lib$+"box2dgfx" INSTALL @lib$+"box2ddbg" IF INKEY$(-256) = "W" THEN SYS "SetWindowText", @hwnd%, "Trebuchet: Press R for range data, S for slow motion" ELSE SYS "SDL_SetWindowTitle", @hwnd%, "Trebuchet: Press R for range data, S for slow motion", @memhdc% ENDIF ON ERROR PROCcleanup : IF ERR = 17 CHAIN @lib$+"../examples/tools/touchide" ELSE REPORT : END ON CLOSE PROCcleanup : QUIT gravity_x = 0.0 gravity_y = -10 xshift=0 yshift=0 scale1=30 myWorld%% = FN_b2CreateWorld(gravity_x, gravity_y) PROC_gfxInit(gfx{}, Width%, Height%, scale1) PROC_b2DebugInit(myWorld%%, %01011, scale1) gheight=0.2 ground%% = FN_b2StaticBox(myWorld%%, 150, -gheight/2,0,150.0, gheight/2) xoff=5 yoff=2 beamlength=10 hbl=beamlength/2 beamangle=ACS(4/5) pfract=0.7 px=pfract*beamlength*COS(beamangle) py=pfract*beamlength*SIN(beamangle) PROC_gfxLoad(bckdrp{}, @dir$+".trebuchetpics/backdrop.jpg", 24) PROC_gfxLoad(bm{}, @dir$+".trebuchetpics/smallbeam.png", 40) PROC_gfxLoad(wt{}, @dir$+".trebuchetpics/smallweight.png", 40) PROC_gfxLoad(ball{}, @dir$+".trebuchetpics/ball.png", 20) PROC_gfxLoad(cup{}, @dir$+".trebuchetpics/cup.png", 40) PROC_gfxLoad(cap{}, @dir$+".trebuchetpics/cap.png", 40) PROC_gfxLoad(stand{}, @dir$+".trebuchetpics/stand.png", 40) PROC_gfxLoad(tup{}, @dir$+".trebuchetpics/tent_up.png", 40) PROC_gfxLoad(tdown{}, @dir$+".trebuchetpics/tent_down.png", 40) IF INKEY$(-256) = "W" THEN PROC_gfxMultiply(bm{}) PROC_gfxMultiply(wt{}) PROC_gfxMultiply(ball{}) PROC_gfxMultiply(cup{}) PROC_gfxMultiply(cap{}) PROC_gfxMultiply(stand{}) PROC_gfxMultiply(tup{}) PROC_gfxMultiply(tdown{}) ENDIF pivotpt%% = FN_b2StaticBox(myWorld%%, px+xoff, py+yoff, 0.0, 0.1, 0.1) runner%% = FN_b2StaticBox(myWorld%%, px+xoff, yoff-0.8, 0.0, hbl, 0.01) beam%% = FN_b2DynamicBody(myWorld%%, hbl*COS(beamangle)+xoff,hbl*SIN(beamangle) +yoff, beamangle, 0, 0, 0, 0, 0) plank%%=FN_b2BoxFixture(beam%%, 0, 0, 0, hbl, beamlength/100, 0.2, 0, 0.7) marker%%=FN_b2CircleFixture(pivotpt%%, 0, 0, 0.5, 0.3, 0.7, 1.0) wt%%=FN_b2DynamicBody(myWorld%%, beamlength*COS(beamangle)+xoff, beamlength*SIN(beamangle)+yoff-1, 0, 0, 0, 0, 0, 0) weight%%=FN_b2BoxFixture(wt%%, 0,0, 0, 1, 1, 0.2, 0, 5.0) slinglength=8 shotradius=0.4 shotdensity=1.25 cup%%=FN_b2DynamicBody(myWorld%%, slinglength+xoff, 0.2+shotradius+yoff, 0, 0, 0, 0, 0, 0) cbase%%=FN_b2BoxFixture(cup%%, 0.1+shotradius, 0, 0, 0.1, shotradius, 0.2, 0, 0.7) cbottom%%=FN_b2BoxFixture(cup%%, 0, -(0.1+shotradius), 0, shotradius, 0.1, 0.2, 0, 0.7) ctop%%=FN_b2BoxFixture(cup%%, 0, 0.1+shotradius, 0, shotradius, 0.1, 0.2, 0, 0.7) cap%%=FN_b2DynamicBody(myWorld%%, 0+xoff, 0+yoff, beamangle, 0, 0, 0, 0, 0) captop%%=FN_b2BoxFixture(cap%%, 0.1, 0.15, 0, 0.1, 0.05, 0.2, 0, 0.7) capbase%%=FN_b2BoxFixture(cap%%, -0.05, 0, 0, 0.05, 0.2, 0.2, 0, 0.7) capbottom%%=FN_b2BoxFixture(cap%%, 0.1, -0.15, 0, 0.1, 0.05, 0.2, 0, 0.7) shot%%=FN_b2DynamicBody(myWorld%%, slinglength+xoff, 0.2+shotradius+yoff, 0.0, 0, 0, 0, 0, 1.0) shot2%%=FN_b2CircleFixture(shot%%, 0, 0, shotradius, 0.5, 0.2, shotdensity) pivot%%=FN_b2RevoluteJoint(myWorld%%, pivotpt%%, beam%%, px+xoff, py+yoff, 0, 0) pivot2%%=FN_b2RevoluteJoint(myWorld%%, beam%%, wt%%, beamlength*COS(beamangle)+xoff, beamlength*SIN(beamangle)+yoff, 0, 0) rope1%% = FN_b2RopeJoint(myWorld%%, beam%%, cup%%, -hbl, 0, 0.0, 0.1+shotradius, slinglength) rope2%% = FN_b2RopeJoint(myWorld%%, cap%%, cup%%, 0.2, -0.2,0, -(0.1+shotradius), slinglength) REM Let's have a target to aim at DIM tents%%(10) FOR range=100 TO 200 STEP 10 wall1%%=FN_b2DynamicBody(myWorld%%, range-5, 1.5, 0.0, 0, 0, 0, 0, 0) wall1b%%=FN_b2BoxFixture(wall1%%, 0, 0, 0, 0.2, 1.5, 0.2, 0, 0.7) wall2%%=FN_b2DynamicBody(myWorld%%, range, 1.5, 0.0, 0, 0, 0, 0, 0) wall2b%%=FN_b2BoxFixture(wall2%%, 0, 0, 0, 0.2, 1.5, 0.2, 0, 0.7) tents%%((range-100)/10)=FN_b2DynamicBody(myWorld%%, range-2.5, 1.7, 0.0, 0, 0, 0, 0, 0) roof1%%=FN_b2BoxFixture(tents%%((range-100)/10), -1.25, 2, PI/6, 1.3, 0.1, 0.2, 0, 0.7) roof2%%=FN_b2BoxFixture(tents%%((range-100)/10), 1.25, 2, -PI/6, 1.3, 0.1, 0.2, 0, 0.7) NEXT range timeStep = 1.0/60.0 velIterations% = 8 posIterations% = 3 *REFRESH OFF start% = TIME + 100 REPEAT PROC_b2GetBody(shot%%, sx, sy, sa) scale = ABS(500/sy) IF scale>scale1 THEN scale = scale1 xshift = -sx*scale+400 IF xshift > 0 THEN xshift = 0 PROC_gfxMatrix(gfx{}, xshift, yshift, scale) PROC_gfxBlit(gfx{},bckdrp{}, 80, 31, 0) PROC_gfxPlot1(gfx{}, ball{}, sx, sy, sa) PROC_b2GetBody(beam%%, x, y, a) PROC_gfxPlot1(gfx{}, bm{}, x, y, a) PROC_b2GetBody(wt%%, x, y, a) PROC_gfxPlot1(gfx{}, wt{}, x, y, a) PROC_b2GetBody(cup%%, x, y, a) PROC_gfxPlot1(gfx{}, cup{}, x, y, a) PROC_b2GetBody(cap%%, x, y, a) PROC_gfxPlot1(gfx{}, cap{}, x, y, a) PROC_gfxDrawRopeJoint(gfx{}, rope1%%, &FF707040, 3) PROC_gfxDrawRopeJoint(gfx{}, rope2%%, &FF707040, 3) FOR t%=0 TO 10 PROC_b2GetBody(tents%%(t%), x, y, a) IF (x + xshift) < 20 THEN IF y>-0.5 THEN PROC_gfxPlot1(gfx{}, tup{}, x, y+0.7, a) ELSE PROC_gfxPlot1(gfx{}, tdown{}, x, y+1.0, a) ENDIF NEXT t% PROC_gfxPlot1(gfx{}, stand{}, px+xoff, py/2+yoff, 0) IF INKEY-51 PROC_b2DebugMatrix(xshift, yshift, scale) : PROC_b2DebugDraw(myWorld%%) IF INKEY-52 THEN PRINT TAB(0,1);"Height: ";INT(sy):PRINT TAB(0,0);"Range: ";INT(sx) PROC_gfxDisplay IF INKEY-82 WAIT 10 IF TIME > start% PROC_b2WorldStep(myWorld%%, timeStep, velIterations%, posIterations%) UNTIL FALSE PROCcleanup END DEF PROCcleanup *REFRESH ON ON ERROR OFF VDU 23,22,640;500;8,20,16,128 myWorld%% += 0 : IF myWorld%% PROC_b2DestroyWorld(myWorld%%) : myWorld%% = 0 PROC_b2DebugExit PROC_b2Exit PROC_gfxExit ENDPROC