Post by bplus on Mar 10, 2022 13:29:34 GMT -5
OK here is a step or two further down from complete restart:
Enough code to see flocking action of dots from Random Start:

' Boids Restart b+ 2022-03-10
' >>>>>>>>>>>>>>>>>>>>> Escape Key will now call it Quits!
global H$, xmax, ymax, pi, deg, rad, nb, speed, done
H$ = "gr"
xmax = 1200 '<== actual drawing space desired
ymax = 700 '<=== actual drawing space desired
pi = acs(-1)
deg = 180 / pi ' radian 2 degree mult
rad = pi / 180 ' degree 2 radian mult
nb = 100 ' number of birds
speed = 5
done = 0
dim bx(nb), by(nb), ba(nb), oldx(nb), oldy(nb)
for i = 1 to nb
bx(i) = rand(100, xmax - 100) ' start random screen x, y away from borders
by(i) = rand(100, ymax - 100)
ba(i) = 2 * pi * rnd(0) ' random headings
next
headMode = 1 ' on / off
sway = pi/6 'just turn neighbor towards neighbor
hf = .05 'heading factor how strong an influence 0 to 1
centerMode = 1 ' on / off
cf = .001 'centering factor how strong a pull from 0 to 1 .01 is week .1 pretty strong!
nomainwin
WindowWidth = xmax + 8
WindowHeight = ymax + 32
UpperLeftX = 100
UpperLeftY = 30
open "Boids Restart" for graphics_nsb_nf as #gr '<======================= title
#gr "setfocus"
#gr "trapclose quit"
#gr "when leftButtonUp lButtonUp"
#gr "when characterInput charIn"
#gr "down"
while done = 0
scan
for i = 1 to nb 'draw then update positions
'erase old
#gr "color white"
#gr "backcolor white"
#gr "place ";oldx(i);" ";oldy(i);"; circlefilled ";2
' draw current
#gr "color black"
#gr "backcolor black"
#gr "place ";bx(i);" ";by(i);"; circlefilled ";2
oldx(i) = bx(i) : oldy(i) = by(i)
[test]
dx = speed * cos(ba(i)) : dy = speed * sin(ba(i))
testx = bx(i) + dx : testy = by(i) + dy
' fix ba() for comparing
if ba(i) < 0 then ba(i) = ba(i) + 2 * pi
if ba(i) >= 2 * pi then ba(i) = ba(i) - 2 * pi
'inbounds?
if testx <= 0 then 'they seem to graviatate to left edge
bx(i) = xmax + testx - rand(0, 15) : by(i) = rand(100, ymax - 100): ba(i) = pi + rnd(0) * pi/2 * rMinus()
else
if testx > 0 and testx < xmax and testy > 0 and testy < ymax then ' OK
bx(i) = testx : by(i) = testy
else
ba(i) = ba(i) + rnd(0) * 2 * pi - pi : goto [test]
end if
end if
for j = i + 1 to nb
scan
dist = distance(bx(i), by(i), bx(j), by(j))
if dist < 50 then ' birds are close enough to influence each other by visual
'sway the neighbors headings towards each other
if headMode then
if ba(i) > ba(j) then ' this is congregating all the birds to the left edge and bottom
ba(i) = ba(i) - sway * hf
ba(j) = ba(j) + sway * hf
else
ba(i) = ba(i) + sway * hf
ba(j) = ba(j) - sway * hf
end if
if ba(i) < 0 then ba(i) = ba(i) + 2 * pi
if ba(i) >= 2 * pi then ba(i) = ba(i) - 2 * pi
if ba(j) < 0 then ba(j) = ba(j) + 2 * pi
if ba(j) >= 2 * pi then ba(j) = ba(j) - 2 * pi
end if
end if
if dist > 20 and dist < 100 then
'stickiness stay close to neighbors, close distance between
if centerMode then
if bx(i) > bx(j) then
bx(i) = bx(i) - cf * (bx(i) - bx(j))
bx(j) = bx(j) + cf * (bx(i) - bx(j))
else
bx(i) = bx(i) + cf * (bx(j) - bx(i))
bx(j) = bx(j) - cf * (bx(j) - bx(i))
end if
if by(i) > by(j) then
by(i) = by(i) - cf * (by(i) - by(j))
by(j) = by(j) + cf * (by(i) - by(j))
else
by(i) = by(i) + cf * (by(j) - by(i))
by(j) = by(j) - cf * (by(j) - by(i))
end if
end if
end if
next 'j
next ' i
' call pause 200 EDIT: This is not needed for this code
wend
wait
sub lButtonUp H$, mx, my 'must have handle and mouse x,y
end sub
sub charIn H$, c$
if asc(c$) = 27 then call quit H$
end sub
'Need line: #gr "trapclose quit"
sub quit H$
close #H$ '<=== this needs Global H$ = "gr"
end 'Thanks Facundo, close graphic wo error
end sub
sub pause mil 'tsh version has scan built-in
t0 = time$("ms")
while time$("ms") < t0 + mil : scan : wend
end sub
Function Atan2(y,x)
'Atan2 is a function which determines the angle between points
'x1, y1 and x2, y2. The angle returned is in radians
'The angle returned is always in the range of
'-pi to pi radians (-180 to 180 degrees)
'==============================================================
'NOTE the position of Y and X arguments
'This keeps Atan2 function same as other language versions
'==============================================================
If x = 0 Then
If y < 0 Then
Atan2 = -1.5707963267948967
Else
Atan2 = 1.5707963267948967
End If
Else
chk = atn(y/x)
If x < 0 Then
If y < 0 Then
chk = chk - 3.1415926535897932
Else
chk = chk + 3.1415926535897932
End If
End If
Atan2 = chk
End If
'thanks Andy Amaya
End Function
function rand(lo, hi)
rand = int((hi - lo + 1) * rnd(0)) + lo
end function
function rMinus() ' plus or minus 1 multiplier
if rnd(0) < .5 then rMinus = -1 else rMinus = 1
end function
function distance(x1, y1, x2, y2)
distance = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5
end function
Enough code to see flocking action of dots from Random Start:
