I seriously apologize if this doesn't work in DarkBASIC Classic, I only have DBPro.
I know it works in DBPro though, and I'm sure with a little work you can adapt the general concept.
type particles
x# as float ` Distance on x-axis from origin (0,0)
y# as float ` Distance on y-axis from orogin (0,0)
m# as float ` Mass
u# as float ` Velocity on the x-axis
v# as float ` Velocity on the y-axis
endtype
Rem ***** Main Source File *****
sync on
set display mode 1024, 786, 32
#constant GRAVITYCONST 0.00000667300
global dim d(0) as particles
MakeAlphaImage( 1, screen width(), screen Height(), 0, 0, 0, 5 )
ink rgb( 255,0,0 ), 0
do
`cls
if mouseclick()=1
px1# = mousex()
py1# = mousey()
clicked = 1
endif
px2# = mousex()
py2# = mousey()
if mouseclick()=0 and clicked = 1
MakeParticle( px1#, py1#, rnd(19)+1, (-px1#+px2#)/2.0, (-py1#+py2#)/2.0 )
clicked = 0
endif
Render()
if spacekey() then StopAll()
paste image 1, 0, 0, 1
text 20, 20, "Click the mouse to place a particle"
text 20, 35, "Hold Spacebar to give all the particles 0.0 velocity"
sync
loop
function MakeAlphaImage( ImgNum, Width, Height, Rv, Gv, Bv, Alpha )
`http://forum.thegamecreators.com/?m=forum_view&t=49923&b=6
if Width > 0 and Height > 0
Depth = 32
make memblock 255, Width*Height*4+12
write memblock dword 255, 0, Width
write memblock dword 255, 4, Height
write memblock dword 255, 8, Depth
for i=12 to (Width*Height*4+11) step 4
write memblock byte 255, i+2, Rv :`Blue
write memblock byte 255, i+1, Gv :`Green
write memblock byte 255, i, Bv :`Red
write memblock byte 255, i+3, Alpha :`Alpha
next i
make image from memblock ImgNum, 255
delete memblock 255
else
text mousex(), mousey(), "Image Dimensions are illegal"
endif
endfunction
function StopAll()
ac = array count( d(0) )
for o1 = 0 to ac
d(o1).u# = 0.0
d(o1).v# = 0.0
next o1
endfunction
function Render()
ac = array count( d(0) )
for o1 = 1 to ac
gx# = 0.0 : gy# = 0.0
` For every other particle
for o2 = 1 to ac
if o1 <> o2
`Force of gravitational attraction=Gravitational Constant*Mass Object 1*Mass Object 2/the distance between them squared
x# = d(o2).x#
y# = d(o2).y#
dist# = Dist2d(x#, y#, d(o1).x#, d(o1).y# )
` Yes I know that this is sqauring the square root of the differance
accel# = (GRAVITYCONST * d(o1).m# * d(o2).m#) / dist#*dist#
theta# = angle( d(o1).x#, d(o1).y#, x#, y#, 1 )
gx# = gx# + (accel# * cos(theta#))
gy# = gy# + (accel# * sin(theta#))
endif
next o2
if mouseclick()=2
x# = mousex()
y# = mousey()
dist# = Dist2d(x#, y#, d(o1).x#, d(o1).y# )
` Yes I know that this is sqauring the square root of the differance
accel# = (GRAVITYCONST * d(o1).m# * 1000.0) / dist#*dist#
theta# = angle( d(o1).x#, d(o1).y#, x#, y#, 1 )
gx# = gx# + (accel# * cos(theta#))
gy# = gy# + (accel# * sin(theta#))
ink rgb( 0, 255, 0 ), 0
circle x#, y#, 5
endif
d(o1).u# = d(o1).u# + gx#
d(o1).v# = d(o1).v# + gy#
` Update the positions of the Particles
`if mouseclick()=2
d(o1).x# = d(o1).x# + ( d(o1).u#)
d(o1).y# = d(o1).y# + ( d(o1).v#)
if d(o1).x# > screen width() then d(o1).x# = 0 else if d(o1).x# < 0 then d(o1).x# = screen width()
if d(o1).y# > screen height() then d(o1).y# = 0 else if d(o1).y# < 0 then d(o1).y# = screen height()
`endif
` Draw a particle here.
ink rgb( 255, 0, 0 ), 0
circle d(o1).x#, d(o1).y#, d(o1).m# / 2.0
line d(o1).x#, d(o1).y#, d(o1).x#-d(o1).u#, d(o1).y#-d(o1).v#
next o1
endfunction
function Dist2d( x1#, y1#, x2#, y2# )
dist# = sqrt( (x2#-x1#)*(x2#-x1#) + (y2#-y1#)*(y2#-y1#) )
endfunction dist#
`this function calculates the angle between the line created by the points
` and the chosen axis
function angle( FROMx#, FROMy#, TOx#, TOy#, AXIS )
#constant X_AXIS = 1
#constant Y_AXIS = 2
vx# = (TOx# - FROMx#)
vy# = (TOy# - FROMy#)
if AXIS = X_AXIS
if (TOy# - FROMy#) > 0
ang# = acos( vx#/sqrt( vx#*vx# + vy#*vy# ) )
else
ang# = acos( -vx#/sqrt( vx#*vx# + vy#*vy# ) ) + 180
endif
else
if (TOx# - FROMx#) > 0
ang# = acos( vy#/(sqrt( vx#*vx# + vy#*vy# )) )
else
ang# = acos( -vy#/(sqrt( vx#*vx# + vy#*vy# )) ) + 180
endif
endif
endfunction ang#
function SetPixel( x as integer, y as integer, color as integer )
lock pixels
if x > screen width() then x = screen width()
if x < 0 then x = 0
if y > screen height() then y = screen height()
if y < 0 then y = 0
ptr = get pixels pointer()
pitch = get pixels pitch()
bpp = screen depth() / 8
pixel = x*bpp + y*pitch
ptr = ptr + pixel
*ptr = color
unlock pixels
endfunction pixel
function GetPixel( x as integer, y as integer )
if x > screen width() then x = screen width()
if x < 0 then x = 0
if y > screen height() then y = screen height()
if y < 0 then y = 0
ptr = get pixels pointer()
pitch = get pixels pitch()
bpp = screen depth() / 8
pixel = x*bpp + y*pitch
ptr = ptr + pixel
color = *ptr
endfunction color
function MakeParticle( x# as float, y# as float, m# as float, u# as float, v# as float )
array insert at bottom d(0)
ac = array count( d(0) )
d(ac).x# = x#
d(ac).y# = y#
d(ac).m# = m#
d(ac).u# = u#
d(ac).v# = v#
endfunction