For example paul doe used the metaball code but how does the metaBall code actually work?

viewtopic.php?f=15&t=26050

Here it is explained in steps.

https://www.youtube.com/watch?v=ccYLb7cLB1I

This video example actually uses functions not part of the FreeBASIC standard set such as dist() so you have to write them yourself.

So I start with the first step:

Code: Select all

`const SCRW = 640`

const SCRH = 480

screenres 640,480,32 'set up window

sub drawIt()

dim as single dx,dy,d

for x as integer = 0 to SCRW-1

for y as integer = 0 to SCRH-1

dx = abs(x - SCRW/2)

dy = abs(y - SCRH/2)

d = sqr(dx^2 + dy^2)

if d > 255 then d = 255

pset(x,y), rgb(d,d,d) 'each color a function of x and y

next y

next x

end sub

drawIt()

sleep

I will skip a couple of interations to this one,

Code: Select all

`'https://www.youtube.com/watch?v=ccYLb7cLB1I`

const SCRW = 640

const SCRH = 480

screenres 640,480,32 'set up window

type BLOB

as single x

as single y

as single vx

as single vy

as single r

as ulong c

end type

dim shared as BLOB b(1 to 2)

'intialize blobs

b(1).x = 100

b(1).y = 100

b(1).r = 40

b(1).c = rgb(255,0,0)

b(1).vx = int(rnd(1)*3)-1 '-1 to 1

b(1).vy = int(rnd(1)*3)-1

while b(1).vx = 0 and b(1).vy = 0 'don't want zero movement

b(1).vx = int(rnd(1)*3)-1 '-1 to 1

b(1).vy = int(rnd(1)*3)-1

wend

b(2).x = 200

b(2).y = 200

b(2).r = 40

b(2).c = rgb(255,0,0)

b(2).vx = int(rnd(1)*3)-1 '-1 to 1

b(2).vy = int(rnd(1)*3)-1

while b(2).vx = 0 and b(2).vy = 0 'don't want zero movement

b(2).vx = int(rnd(1)*3)-1 '-1 to 1

b(2).vy = int(rnd(1)*3)-1

wend

function dist(x1 as single,y1 as single,x2 as single, y2 as single) as single

dim as single dx,dy,d

dx = abs(x1-x2)

dy = abs(y1-y2)

d = sqr(dx^2+dy^2)

if d > 255 then d = 255

return d

end function

sub drawIt()

dim as single dx,dy,d

dim as ulong col

for x as integer = 0 to SCRW-1

for y as integer = 0 to SCRH-1

d = dist(x,y,b(1).x,b(1).y)

if d > 0 then

col = b(1).r*255 \ d

end if

if col > 255 then col = 255

pset(x,y), rgb(col,0,0) 'each color a function of x and y

next y

next x

end sub

sub drawBlob(b as BLOB)

circle (b.x,b.y),b.r,rgb(255,255,255)

end sub

sub moveBlob(b as BLOB)

b.x = b.x + b.vx

if b.x < 0 or b.x > SCRW then b.vx = -b.vx

b.y = b.y + b.vy

if b.y < 0 or b.y > SCRH then b.vy = -b.vy

end sub

do

screenlock

drawIt() 'draw faded colors

drawBlob(b(1)) 'draw circular blob

drawBlob(b(2))

screenunlock

moveBlob(b(1))

moveBlob(b(2))

sleep 2

loop until multikey(&H01)

sleep

And this is the stage I got to tonight which actually has a visual bug in it which never the less shows the blending of blobs so I will leave it as is until tomorrow night when I will continue watching the tutorial and translating the code examples.

Code: Select all

`'https://www.youtube.com/watch?v=ccYLb7cLB1I`

const SCRW = 640

const SCRH = 480

screenres 640,480,32 'set up window

type BLOB

as single x

as single y

as single vx

as single vy

as single r

as ulong c

end type

const BlobCount = 4

dim shared as BLOB b(1 to BlobCount)

'intialize blobs

for i as integer = 1 to BlobCount

b(i).x = int(rnd(1)*SCRW)

b(i).y = int(rnd(1)*SCRH)

b(i).r = 40

b(i).c = rgb(255,0,0)

b(i).vx = int(rnd(1)*3)-1 '-1 to 1

b(i).vy = int(rnd(1)*3)-1

while b(i).vx = 0 and b(i).vy = 0 'don't want zero movement

b(i).vx = int(rnd(i)*3)-1 '-1 to 1

b(i).vy = int(rnd(i)*3)-1

wend

'speed them up

b(i).vx = b(i).vx * 8

b(i).vy = b(i).vy * 8

next i

function dist(x1 as single,y1 as single,x2 as single, y2 as single) as single

dim as single dx,dy,d

dx = abs(x1-x2)

dy = abs(y1-y2)

d = sqr(dx^2+dy^2)

if d > 255 then d = 255

return d

end function

sub drawIt()

dim as single dx,dy,d,sum

dim as ulong col

for x as integer = 0 to SCRW-1

for y as integer = 0 to SCRH-1

sum = 0

for i as integer = 1 to BlobCount

d = dist(x,y,b(i).x,b(i).y)

if d > 0 then

sum = sum + b(i).r*180 \ d

end if

next i

'if sum > 255 then sum = 255

pset(x,y), rgb(sum,0,0) 'each color a function of x and y

next y

next x

end sub

sub drawBlob(b as BLOB)

circle (b.x,b.y),b.r,rgb(255,255,255)

end sub

sub moveBlob(b as BLOB)

b.x = b.x + b.vx

if b.x < 0 or b.x > SCRW then b.vx = -b.vx

b.y = b.y + b.vy

if b.y < 0 or b.y > SCRH then b.vy = -b.vy

end sub

do

screenlock

drawIt() 'draw faded colors

for i as integer = 1 to BlobCount

drawBlob(b(i)) 'draw circular blob

next i

screenunlock

for i as integer = 1 to BlobCount

moveBlob(b(i))

next i

sleep 2

loop until multikey(&H01)

sleep