This is where I will post examples of my 2D Physics engine plugin (a wrapper for
Box2D)
Click
HERE for the main thread
Machines Demo:
Download:
Click
Screeny:
And the code:
sync on : sync rate 60
b2SetUnits 0.1,0.017453292519943295769236907684886
MyWorld = b2CreateWorld(-2000,-2000,2000,2000,0,100,1)
rem FUNNEL
FunnelL = b2CreateBoxBody(MyWorld, 10, 100, 0, 0, 0)
FunnelR = b2CreateBoxBody(MyWorld, 10, 100, 0, 0, 0)
b2PositionBody FunnelL,40,200
b2PositionBody FunnelR,160,200
b2RotateBody FunnelL,135
b2RotateBody FunnelR,45
rem SPINNER
spin = b2CreateBodyTemplate()
b1 = b2CreateBoxShape(300,70)
b2 = b2CreateBoxShape(300,70)
b3 = b2CreateBoxShape(300,70)
b4 = b2CreateBoxShape(300,70)
b2AddShapeToBodyTemplate spin,b1,0,0,0,1,0
b2AddShapeToBodyTemplate spin,b2,0,0,90,1,0
b2AddShapeToBodyTemplate spin,b3,0,0,45,1,0
b2AddShapeToBodyTemplate spin,b4,0,0,135,1,0
spinner = b2CreateBody(MyWorld,spin)
b2PositionBody spinner,100,395
spinnerjoint = b2CreateRevoluteJoint(MyWorld,spinner,FunnelL,100,395)
b2SetJointMotorSpeed spinnerjoint,-10
b2SetJointMotorForce spinnerjoint,100000
rem CONVEYOR BELT
ConL = b2CreateCircleBody(MyWorld, 25, 1, 0, 10)
ConR = b2CreateCircleBody(MyWorld, 25, 1, 0, 10)
ConMov = b2CreateBoxBody(MyWorld, 5, 5, 1, 0, 0)
b2PositionBody ConL,100,700
b2PositionBody ConR,610,700
b2PositionBody ConMov,610,700
b2EnableBodyRotation ConMov,0
b2EnableBodyPosition ConMov,0
ConLjoint = b2CreateRevoluteJoint(MyWorld,ConL,FunnelL,100,700)
ConRjoint = b2CreateRevoluteJoint(MyWorld,ConR,ConMov,610,700)
b2SetJointMotorSpeed ConLjoint,-200
b2SetJointMotorSpeed ConRjoint,-200
b2SetJointMotorForce ConLjoint,100000
b2SetJointMotorForce ConRjoint,100000
oldbody = b2CreateBoxBody(MyWorld,25,5,0.1,0.1,10)
first1 = oldbody
b2PositionBody oldbody,50,670
for i = 1 to 23
curbody = b2CreateBoxBody(MyWorld,25,5,0.1,0.1,10)
b2PositionBody curbody,i*25+50,670
temp = b2CreateRevoluteJoint(MyWorld,oldbody,curbody,i*25+37.5,670)
oldbody = curbody
next i
last1 = oldbody
oldbody = b2CreateBoxBody(MyWorld,25,5,0.1,0.1,10)
first2 = oldbody
b2PositionBody oldbody,50,730
for i = 1 to 23
curbody = b2CreateBoxBody(MyWorld,25,5,0.1,0.1,10)
b2PositionBody curbody,i*25+50,730
temp = b2CreateRevoluteJoint(MyWorld,oldbody,curbody,i*25+37.5,730)
oldbody = curbody
next i
last2 = oldbody
b2PositionBody first1,b2GetBodyX(first2),b2GetBodyY(first2)
b2PositionBody last1,b2GetBodyX(last2)+25,b2GetBodyY(last2)+25
b2PositionBody last2,b2GetBodyX(last2)+25,b2GetBodyY(last2)+25
temp = b2CreateRevoluteJoint(MyWorld,first1,first2,b2GetBodyX(first2)-12.5,b2GetBodyY(first2))
temp = b2CreateRevoluteJoint(MyWorld,last1,last2,b2GetBodyX(last2)+12.5,b2GetBodyY(last2))
rem LIFTER
blocker = b2CreateBoxBody(MyWorld,20,1000,0,0,0)
b2PositionBody blocker,852,600
lifter = b2CreateBoxBody(MyWorld,200,1000,1,0,1)
b2PositionBody lifter,742,1250
lifterjoint = b2CreatePrismaticJoint(MyWorld,FunnelL,lifter,742,1250,0,-10)
b2SetJointLimit lifterjoint,0,650
b2EnableJointLimit lifterjoint,1
b2SetJointMotorForce lifterjoint,1000000
rem PUSHER
roof = b2CreateBoxBody(MyWorld,1000,20,0,0,0)
base = b2CreateBoxBody(MyWorld,400,20,0,0,0)
pusher = b2CreateBoxBody(MyWorld,20,80,1,0,0)
side = b2CreateBoxBody(MyWorld,20,130,0,0,0)
b2PositionBody roof,512,10
b2PositionBody base,440,110
b2PositionBody pusher,852,60
b2PositionBody side,22,90
pusherjoint = b2CreatePrismaticJoint(MyWorld,FunnelL,pusher,852,55,-10,0)
b2SetJointLimit pusherjoint,0,500
b2EnableJointLimit pusherjoint,1
b2SetJointMotorForce pusherjoint,1000000
rem SHAPES
for i = 0 to 50
b2PositionBody b2CreateBoxBody(MyWorld,10,10,0.01,0.2,1),50+rnd(100),rnd(100)+20
next i
for i = 0 to 50
b2PositionBody b2CreateCircleBody(MyWorld,5,0.01,0.2,1),50+rnd(100),rnd(100)+20
next i
do
ang# = wrapvalue(ang#+0.5)
b2SetJointMotorSpeed lifterjoint,sin(ang#)*300
if ang# > 0 and ang# < 100
speed# = 400
else
speed# = 200
endif
b2SetJointMotorSpeed pusherjoint,sin(ang#-90)*speed#
if ang# > 270 or ang# < 5
b2SetJointMotorSpeed ConLjoint,-200
b2SetJointMotorSpeed ConRjoint,-200
else
b2SetJointMotorSpeed ConLjoint,0
b2SetJointMotorSpeed ConRjoint,0
endif
b2Step MyWorld
b2DrawDebugInfo MyWorld,0,0,1,1,1
sync
cls 0
loop
Pyramid Demo:
Download:
HERE
Screeny:
Source (requires an image):
sync on : sync rate 60
backdrop off
load image "SmallCrate.png",1,1
sprite 1,-100,-100,1
hide sprite 1
offset sprite 1,10,10
set sprite 1,0,0
backdrop off
b2SetUnits 0.1,0.017453292519943295769236907684886
MyWorld = b2CreateWorld(-2000,-2000,2000,2000,0,50,1)
floorbody = b2CreateBoxBody(MyWorld,1000,20,0,0,1)
b2PositionBody floorbody,512,700
dim Boxes(-1) as integer
for y = 0 to 20
for x = 0 to y
tempbody = b2CreateBoxBody(MyWorld,20,20,1,0.1,1)
b2PositionBody tempbody,512+(x*20)-(y*10),(y*20)+280
array insert at top Boxes(0)
Boxes(0) = tempbody
next x
next y
do
b2Step MyWorld
c = array count(Boxes(0))
for i = 0 to c
tempbody = Boxes(i)
rotate sprite 1,b2GetBodyAngle(tempbody)
paste sprite 1,b2GetBodyX(tempbody),b2GetBodyY(tempbody)
next i
b2DrawDebugInfo MyWorld,0,0,1,1,1
text 0,0,str$(screen fps())
sync
cls 0
loop
UFO's Marble demo:
Screeny:
Source:
sync on : sync rate 60 : backdrop off
b2SetUnits 0.1,0.017453292519943295769236907684886
myworld = b2CreateWorld(-2000,-2000,2000,2000,0,500,1)
floorbody = b2CreateBoxBody(myworld,800,20,0,0,0.5)
b2PositionBody floorbody,512,700
BodyCount = 1
dim Bodies(-1) as integer
ink rgb(200,200,200),0
for I = 1 to 10
blockbody = b2createboxbody(myworld,100,15,1,0,0.5)
b2positionbody blockbody,rnd(700)+100,500
NExt I
for I = 1 to 5
blockbody = b2createboxbody(myworld,20,20,0.1,0,0.5)
b2positionbody blockbody,rnd(700)+100,500
NExt I
for I = 1 to 5
blockbody = b2createboxbody(myworld,25,25,5,0,0.5)
b2positionbody blockbody,rnd(700)+100,500
NExt I
for I = 1 to 5
blockbody = b2createboxbody(myworld,175,5,1,0,0.5)
b2positionbody blockbody,rnd(700)+100,500
NExt I
do
b2Step myworld
b2DrawDebugInfo myworld,0,0,1,1,1
If mouseclick() = 2 and rnd(2) = 1
circlebody = b2createcirclebody(myworld,5,0.1,0,0.5)
b2positionbody circlebody,mousex(),mousey()
endif
text 0,0,str$(screen fps())
text 0,20,"Body count: "+str$(BodyCount)
sync
cls 0
loop
UFO's Building demo:
Screeny:
Source:
sync on : sync rate 60 : backdrop off
`Not sure what this does. You should probably have it before any program with the plugin, tho. Its weird without it.
b2SetUnits 0.1,0.017453292519943295769236907684886
`This creates the world.
myworld = b2CreateWorld(-2000,-2000,2000,2000,0,500,1)
`This makes the floor to build on. It has a density of 0, so it will stay in place.
floorbody = b2CreateBoxBody(myworld,800,20,0,0,0.5)
b2PositionBody floorbody,512,700
ink rgb(200,200,200),0
`These are variables that will be explained later
click = 1
oldmousex = 0
oldmousey = 0
lastmousex = 0
lastmousey = 0
lastshape = 0
Posx = 0
Posy = 0
density# = 0.5
clickdelay = -1
do
CLS
`not sure what these do yet
b2Step myworld
b2DrawDebugInfo myworld,0,0,1,1,1
`This is used to make a delay between clicks so the game doesn't register one click as a trillion
If clickdelay > -1 then clickdelay = clickdelay - 1
`This puts it into build mode (If click = 2 then it is in build mode). It also records the mouse coordinates for building.
If mouseclick() = 2 and click = 1 and clickdelay = -1
click = 2
oldmousex = mousex()
oldmousey = mousey()
clickdelay = 10
endif
`This cancels the build mode and puts it back into drag mode
If mouseclick() = 2 and click = 2 and clickdelay = -1 then click = 1:clickdelay = 10
`Makes the last shape made again
If mouseclick() = 4 and click = 1 and clickdelay = -1 and lastshape <> 0
clickdelay = 10
If lastshape = 1 then body = b2createboxbody(myworld,abs(lastmousex-oldmousex),abs(lastmousey-oldmousey),density#,0,0.5):b2positionbody body,mousex(),mousey()
If lastshape = 2 then body = b2createcirclebody(myworld,Get_Distance(oldmousex,oldmousey,lastmousex,lastmousey)/2,density#,0,0.5):b2positionbody body,mousex(),mousey()
clickdelay = 10
endif
`This makes a box. It could probably have been done better, so there's no point commenting on my crappy code here
If mouseclick() = 1 and click = 2 and mousex() <> oldmousex and mousey() <> oldmousey and clickdelay = -1
click = 1
If mousex() > oldmousex then posx = oldmousex+(0.5*(mousex()-oldmousex))
If mousey() > oldmousey then posy = oldmousey+(0.5*(mousey()-oldmousey))
If mousex() < oldmousex then posx = mousex()+(0.5*(oldmousex-mousex()))
If mousey() < oldmousey then posy = mousey()+(0.5*(oldmousey-mousey()))
blockbody = b2createboxbody(myworld,abs(mousex()-oldmousex),abs(mousey()-oldmousey),density#,0,0.5)
b2positionbody blockbody,posx,posy
clickdelay = 10
lastshape = 1
lastmousex = mousex()
lastmousey = mousey()
endif
`This makes a circle
If mouseclick() = 4 and click = 2 and mousex() <> oldmousex and mousey() <> oldmousey and clickdelay = -1
`These two commands make and position the circle at the current mouse position
circlebody = b2createcirclebody(myworld,Get_Distance(oldmousex,oldmousey,mousex(),mousey())/2,density#,0,0.5)
b2positionbody circlebody,oldmousex,oldmousey
click = 1
clickdelay = 10
lastshape = 2
lastmousex = mousex()
lastmousey = mousey()
endif
If click = 2 then linebox(oldmousex,oldmousey,mousex(),mousey()):circle oldmousex,oldmousey,get_distance(oldmousex,oldmousey,mousex(),mousey())/2
`If you press a number button, record it as the density
If scancode() >= 2 and scancode() <= 10 then density# = scancode() - 1
If scancode() = 11 then density# = 0
`Debug and help info
If click = 1 then text 0,0,"Drag mode"
If click = 2 then text 0,0,"Build mode"
text 0,20,"Density of block: "+str$(density#)
text 0,40,"LMB to drag shapes"
Text 0,60,"RMB button for build mode"
Text 0,80,"Followed by:"
Text 0,100,"--LMB for box"
Text 0,120,"--MMB for circle"
Text 0,140,"--RMB to leave build mode"
Text 0,160,"MMB to make last shape"
If clickdelay > -1 then Text 0,180,"Click delay"
IF click = 2 then Text 0,200,"Dis X: "+str$(mousex()-oldmousex)+", Dis Y: "+str$(mousey()-oldmousey)
sync
cls 0
loop
`This gets the distance between 2 coordinates
Function get_Distance(x1,y1,x2,y2)
result# = sqrt((x2-x1)^2+(y2-y1)^2)
result# = abs(result#)
Endfunction result#
Function linebox(x1,y1,x2,y2)
line x1,y1,x2,y1
line x1,y1,x1,y2
line x2,y1,x2,y2
line x1,y2,x2,y2
Endfunction
Roxas's water demo:
Screeny:
Source:
sync on
b2SetUnits 0.1,0.017453292519943295769236907684886
rem Water Size
rem Try diffrent sizes
#constant WATERSIZE = 1
rem Max water amount
rem how much ripples you can draw until engine starts to remove old ripples
#constant MAXWATER = 400
Rem Water Shape
Rem 1 = Box 2 = Circle
#constant WATERSHAPE = 1
global wat
MyWorld = b2CreateWorld(-2000,-2000,2000,2000,0,50,1)
MyFloor = b2CreateBoxBody(MyWorld, screen width(), 50, 0, 0.3, 5)
b2PositionBody MyFloor,screen width()/2,screen height()-50
CreateWater(MyWorld,500,1)
do
if mouseclick() then CreateWater(MyWorld,mousex(),mousey())
b2Step MyWorld
b2DrawDebugInfo MyWorld,0,0,1,1,0
fastsync
cls 0
loop
Function CreateWater(MyWorld,x,y)
if wat<MAXWATER then inc wat,1:global dim water(wat,2) as integer
if wat=MAXWATER then del=Longest():b2DeleteBody MyWorld,water(del,0):water(del,2)=0
if wat<MAXWATER then del=wat
if WATERSHAPE = 1 then water(del,0) = b2CreateBoxBody(MyWorld,WATERSIZE,WATERSIZE,2,0,0)
if WATERSHAPE = 2 then water(del,0) = b2CreateCircleBody(MyWorld,WATERSIZE,2,0,0)
water(del,1) = timer()
water(del,2) = 1
b2PositionBody water(del,0),x,y
Endfunction
Function Longest()
for x=1 to wat
if water(x,2)=1
if timer()-water(x,1)>longes then longes=timer()-water(x,1):long=x
endif
next x
Endfunction long