This is a simple gas physics engine that's supposed to simulate an 'ideal' gas. It looks ok, but the physics don't quite match reality in certain places. Still, not too bad for just over 150 lines...
I've added comments in case you want to use it in say, a virtual wind tunnel or some other simulator.
But for now its just fun throwing random particles at each other to see what happens
Left mouseclick to heat particles under mouse
Right mouseclick to cool particles under mouse
Spacekey+Mouseclick to grab particles and move them around
Returnkey to resize box to the mouse pointer
Mouse Wheel to change the size of the mouse pointer
Ctrl key displays particles according to velocity
Shift key displays particles according to temperature
(Advanced2D Plugin used. If you don't have it, get it here: http://forum.thegamecreators.com/?m=forum_view&t=179096&b=5)
sync on
set display mode display width(),display height(),32
part=100000 `Number of gas particles. If you go much above 100000 then you're likely to struggle
msize=100 `Size of Mouse
sw=screen width()
sh=screen height()
asw=sw
ash=sh
Dim Px#(part)
Dim Py#(part)
Dim PP(sw,sh)
dim xvel#(part)
dim yvel#(part)
dim temp#(part)
dim shft(0)
dim ctrl(0)
for p=0 to part `randomly seeds the particles
x=rnd(sw)
y=rnd(sh)
px#(p)=x
py#(p)=y
pp(x,y)=p
temp#(p)=0.027
yvel#(p)=1
next p
ink rgb(255,100,100),0
a2startdotbatch part
do
cls 0
mx=mousex()
my=mousey()
mmx=mousemovex()
mmy=mousemovey()
avtemp#=0
avvel#=0
If returnkey()=1 `resizes screen to mouse position
sw=mousex()
sh=mousey()
endif
if sw<80 then sw=80
if sh<80 then sh=80
a2box 0,0,sw,sh,rgb(0,0,255)
inc msize,mousemovez()/60
if msize<1 then msize=1
`pa(0)=0
mc=mouseclick()
spc=spacekey()
shft(0)=shiftkey()
ctrl(0)=controlkey()
for p=1 to part
Updateparticle(p,sw,sh)
inc avtemp#,temp#(p)
inc avvel#,(fabs(yvel#(p))+fabs(xvel#(p)))/2
if mc>0 or spc>0
if sqrt((mx-px#(p))^2+(my-py#(p))^2)<msize
if mc=1 and spc=0 then inc temp#(p),0.1 `heats particles
if mc=2 then dec temp#(p),0.01 `cools particles
if mc=1 and spc=1 then yvel#(p)=mmy:xvel#(p)=mmx `grabs particles
if temp#(p)<0 then temp#(p)=0
endif
endif
next p
center text asw/2,ash-80,"Left mouseclick to heat particles"
center text asw/2,ash-70,"Right mouseclick to cool particles"
center text asw/2,ash-60,"Spacekey+Mouseclick to grab particles"
center text asw/2,ash-50,"Returnkey to resize box"
if shft(0)=1 then center text asw/2,ash-40,"Tempertature: "+str$(int((avtemp#/part)*10000))+" K"
if ctrl(0)=1 then center text asw/2,ash-40,"Velocity "+str$(avvel#/part)+" Units"
if ctrl(0)=0 and shft(0)=0
center text asw/2,ash-40,"Hold Shift for avg. Temp"
center text asw/2,ash-30,"Hold Ctrl for avg. Vel"
endif
center text asw/2,ash-20,"FPS: "+str$(screen fps())
a2circle mx,my,msize,rgb(100,100,100)
sync
loop
function Updateparticle(p,sw,sh)
PP(int(px#(p)),int(py#(p)))=0
repeat
inc ch
xc#=rnd(100)-50 `Causes particles to travel in random directions
yc#=rnd(100)-50
xvel#(p)=(xc#*temp#(p))+xvel#(p) `Causes particles to travel in random directions based on temperature
yvel#(p)=(yc#*temp#(p))+yvel#(p)
inc px#(p),xvel#(p)
inc py#(p),yvel#(p)
if px#(p)>sw then px#(p)=sw:xvel#(p)=-xvel#(p) `sorts collisions with edge of box
if px#(p)<0 then px#(p)=0:xvel#(p)=-xvel#(p)
if py#(p)>sh then py#(p)=sh:yvel#(p)=-yvel#(p)
if py#(p)<0 then py#(p)=0:yvel#(p)=-yvel#(p)
px=int(px#(p))
py=int(py#(p))
tp2=PP(px,py)
if tp2>0 `checks if the new location is already occupied
ayvel#=(yvel#(p)+yvel#(tp2))/2 `takes average velocity of both particles
axvel#=(xvel#(p)+xvel#(tp2))/2
ntemp#=(temp#(p)+temp#(tp2))/2 `takes average temperature of both particles
yvel#(p)=ayvel# `Applies the averages to both particles
xvel#(p)=axvel#
yvel#(tp2)=ayvel#
xvel#(tp2)=axvel#
temp#(p)=ntemp#
temp#(tp2)=ntemp#
endif
until tp2=0 or ch>10`If there is a collision then update the particle again so that they arn't in the same place
if shft(0)=1
col=temp#(p)*10000
endif
if ctrl(0)=1
col=int(fabs(yvel#(p))+fabs(xvel#(p))*30)
endif
if ctrl(0)=0 and shft(0)=0
col=255
endif
if col>255 then col=255
a2dot px,py,rgb(col,col,col)
PP(px,py)=p
endfunction
function fabs(x) `fast abs
if x<0 then x=-x
endfunction x
EDIT: The code's been tidied up so it runs about 5 times faster. I get about 5 FPS when simulating 100000 particles