`2D Physics sync on : sync rate 60 set display mode 1024,768,32 : `hide mouse `types type vec x as float y as float m as float endtype type force f1 as vec f2 as vec fw as vec fd as vec f as vec endtype type tscene grav as float density as float endtype scene as tscene type tball pos as vec acc as vec vel as vec mom as vec ang as float frc as force mass as float weight as float size as float drag as float collision as boolean bmdist as float endtype `setup scene scene.grav=0.0 scene.density=0.0 `create balls balls=5 dim ball(balls) as tball for b=1 to balls ball(b).pos.x=rnd(screen width()) ball(b).pos.y=rnd(screen height()) ball(b).size=rnd(20)+20 ball(b).mass=rnd(200)+200 ball(b).weight=ball(b).mass*scene.grav ball(b).drag=0.47 next b `main loop do `control balls for b=1 to balls ball(b).bmdist=sqrt((mousex()-ball(b).pos.x)^2+(mousey()-ball(b).pos.y)^2) ball(b).frc.f1.x=sin(ball(b).ang)*(ball(b).bmdist/12) ball(b).frc.f1.y=cos(ball(b).ang)*(ball(b).bmdist/12) ball(b).ang=atanfull(mousex()-ball(b).pos.x,mousey()-ball(b).pos.y) ball(b).mom.x=ball(b).mass*ball(b).vel.x : ball(b).mom.y=ball(b).mass*ball(b).vel.y ball(b).frc.fd.x=-(ball(b).drag/2)*(ball(b).size*2)*scene.density*((ball(b).vel.x^2)*sign(ball(b).vel.x)) ball(b).frc.fd.y=-(ball(b).drag/2)*(ball(b).size*2)*scene.density*((ball(b).vel.y^2)*sign(ball(b).vel.y)) ball(b).frc.fw.y=ball(b).weight if ball(b).pos.x<=0 or ball(b).pos.y<=0 or ball(b).pos.x>screen width() or ball(b).pos.y>screen height() ball(b).collision=1 else ball(b).collision=0 endif if ball(b).collision=1 ball(b).frc.f2.x=(ball(b).frc.f1.x+ball(b).frc.fd.x+ball(b).frc.fw.x) ball(b).frc.f2.y=(ball(b).frc.f1.y+ball(b).frc.fd.y+ball(b).frc.fw.y) else ball(b).frc.f2.x=0 ball(b).frc.f2.y=0 endif ball(b).frc.f.x=ball(b).frc.f1.x+ball(b).frc.f2.x+ball(b).frc.fd.x+ball(b).frc.fw.x ball(b).frc.f.y=ball(b).frc.f1.y+ball(b).frc.f2.y+ball(b).frc.fd.y+ball(b).frc.fw.y ball(b).acc.x=ball(b).frc.f.x/ball(b).mass ball(b).acc.y=ball(b).frc.f.y/ball(b).mass ball(b).vel.x=ball(b).vel.x+ball(b).acc.x ball(b).vel.y=ball(b).vel.y+ball(b).acc.y ball(b).vel.m=sqrt(ball(b).vel.x^2+ball(b).vel.y^2) ball(b).pos.x=ball(b).pos.x+ball(b).vel.x ball(b).pos.y=ball(b).pos.y+ball(b).vel.y `draw circle ink rgb(255,255,255),0 circle ball(b).pos.x,ball(b).pos.y,ball(b).size `line ball(b).pos.x,ball(b).pos.y,ball(b).pos.x+(sin(ball(b).ang)*ball(b).size),ball(b).pos.y+(cos(ball(b).ang)*ball(b).size) box ball(b).pos.x+((sin(ball(b).ang)*ball(b).size)-2),ball(b).pos.y+((cos(ball(b).ang)*ball(b).size)-2),ball(b).pos.x+((sin(ball(b).ang)*ball(b).size)+2),ball(b).pos.y+((cos(ball(b).ang)*ball(b).size)+2) `draw force vectors amp#=1 ink rgb(255,0,0),0 if spacekey() and shold=0 then shold=1 : showvec=abs(showvec-1) if spacekey()=0 then shold=0 if showvec=1 line ball(b).pos.x,ball(b).pos.y,ball(b).pos.x+(ball(b).frc.f1.x*amp#),ball(b).pos.y+(ball(b).frc.f1.y*amp#) line ball(b).pos.x,ball(b).pos.y,ball(b).pos.x+(ball(b).frc.f2.x*amp#),ball(b).pos.y+(ball(b).frc.f2.y*amp#) line ball(b).pos.x,ball(b).pos.y,ball(b).pos.x+(ball(b).frc.fd.x*amp#),ball(b).pos.y+(ball(b).frc.fd.y*amp#) line ball(b).pos.x,ball(b).pos.y,ball(b).pos.x+(ball(b).frc.fw.x*amp#),ball(b).pos.y+(ball(b).frc.fw.y*amp#) ink rgb(0,255,0),0 line ball(b).pos.x,ball(b).pos.y,ball(b).pos.x+(ball(b).frc.f.x*amp#),ball(b).pos.y+(ball(b).frc.f.y*amp#) endif next b `show stuff ink rgb(255,255,255),0 text 10,10,"Press Space to toggle Force Vectors" sync cls loop function sign(n as float) if n>=0 then exitfunction 1 endfunction -1