Hey guys,
As the title says I made a simple fluid simulation in dbp, I can't credit for it though. It is based on this article I found on the net:
http://local.wasp.uwa.edu.au/~pbourke/modelling_rendering/fluid/
It also is a bit buggy and quite slow (because of the SQRT calls), but I think that the final result is quite cool.
Here is the code:
type fluid
x as float
y as float
oldx as float
oldy as float
xforce as float
yforce as float
mass as float
radius as integer
endtype
#constant ballamount = 50
#constant timeconstant = 1.0
#constant energy = 0.97
#constant scrx = 640
#constant scry = 480
#constant tc = 0.000001
dim ball(ballamount) as fluid
init_fluids()
do
cls
update_fluids()
draw_fluids()
loop
function init_fluids()
for fluid = 1 to ballamount
ball(fluid).x = rnd(scrx)
ball(fluid).y = rnd(scry)
ball(fluid).radius = 14
next fluid
endfunction
function update_fluids()
distance as float
newx as float
newy as float
vxa as float
vya as float
vxb as float
vyb as float
cdx as float
cdy as float
dd as float
m as float
cosam as float
`ga alle fluids langs
for fluida = 1 to ballamount-1
for fluidb = 1 to ballamount
if fluida <> fluidb
distance = ((ball(fluida).x - ball(fluidb).x) * (ball(fluida).x - ball(fluidb).x)) + ((ball(fluida).y - ball(fluidb).y) * (ball(fluida).y - ball(fluidb).y))
if distance < (ball(fluida).radius * ball(fluida).radius * 4)
distance = sqrt(distance)
newx = ball(fluidb).x - ball(fluida).x
newy = ball(fluidb).y - ball(fluida).y
vxa = ball(fluida).xforce
vya = ball(fluida).yforce
vxb = ball(fluidb).xforce
vyb = ball(fluidb).yforce
if abs(distance) <> 0.0001
cdx = newx / distance
cdy = newy / distance
dd = ball(fluida).radius + ball(fluidb).radius - distance
ball(fluida).x = ball(fluida).x - dd * cdx
ball(fluida).y = ball(fluida).y - dd * cdy
ball(fluidb).x = ball(fluidb).x + dd * cdx
ball(fluidb).y = ball(fluidb).y + dd * cdy
`m = sqrt( (ball(fluida).xforce * ball(fluida).xforce) + (ball(fluida).yforce * ball(fluida).yforce))
`if abs(m) > 0.0001
if ball(fluida).xforce <> 0 or ball(fluida).yforce <> 0
cosam = ((cdx * ball(fluida).xforce) + (cdy * ball(fluida).yforce)) * energy
vxa = vxa - cdx * cosam
vya = vya - cdy * cosam
vxb = vxb + cdx * cosam
vyb = vyb + cdy * cosam
endif
`m = sqrt( (ball(fluidb).xforce * ball(fluidb).xforce) + (ball(fluidb).yforce * ball(fluidb).yforce))
`if abs(m) > 0.0001
if ball(fluidb).xforce <> 0 or ball(fluidb).yforce <> 0
cosam = ((cdx * ball(fluidb).xforce) + (cdy * ball(fluidb).yforce)) * energy
vxa = vxa + cdx * cosam
vya = vya + cdy * cosam
vxb = vxb - cdx * cosam
vyb = vyb - cdy * cosam
endif
ball(fluida).xforce = vxa
ball(fluida).yforce = vya
ball(fluidb).xforce = vxb
ball(fluidb).yforce = vyb
endif
endif
for dummy = 1 to ballamount
if ball(dummy).x < 0 + ball(dummy).radius
ball(dummy).x = 0 + ball(dummy).radius
ball(dummy).xforce = -ball(dummy).xforce * energy
endif
if ball(dummy).x > scrx - ball(dummy).radius
ball(dummy).x = scrx - ball(dummy).radius
ball(dummy).xforce = -ball(dummy).xforce * energy
endif
if ball(dummy).y < 0 + ball(dummy).radius
ball(dummy).y = 0 + ball(dummy).radius
ball(dummy).yforce = -ball(dummy).yforce * energy
endif
if ball(dummy).y > scry - ball(dummy).radius
ball(dummy).y = scry - ball(dummy).radius
ball(dummy).yforce = -ball(dummy).yforce * energy
endif
ball(dummy).xforce = ball(dummy).xforce
ball(dummy).yforce = ball(dummy).yforce + 0.1
ball(dummy).x = ball(dummy).x + (ball(dummy).xforce * tc)
ball(dummy).y = ball(dummy).y + (ball(dummy).yforce * tc)
next dummy
endif
next fluidb
next fluida
ball(ballamount).oldx = ball(ballamount).x
ball(ballamount).oldy = ball(ballamount).y
ball(ballamount).x = mousex()
ball(ballamount).y = mousey()
ball(ballamount).xforce = 0.1* (ball(ballamount).x - ball(ballamount).oldx) * tc
ball(ballamount).yforce = 0.1* (ball(ballamount).y - ball(ballamount).oldy) * tc
endfunction
function draw_fluids()
lock pixels
for fluid = 1 to ballamount
circle ball(fluid).x,ball(fluid).y,14
next fluid
unlock pixels
endfunction
Tell me what you think!
I am awesome and always right.