Post by tenochtitlanuk on Mar 16, 2022 15:48:01 GMT -5
I'm coming into this a bit late, with little spare time, and as always I'm more interested in the appearance and flocking-rules than in speed. Can always save a series of images and play as an animated GIF...
This image shows how it displays- each boid as a circle and velocity-vector arrow. Now to implement the usual boid-rules..
The Boids Rules
Rule 1: Boids try to fly towards the centre of mass of neighbouring boids.
Rule 2: Boids try to keep a small distance away from other objects (including other boids).
Rule 3: Boids try to match velocity with near boids.
Lots of others to add- edge behaviour ( bounce or wrap). goal-setting, obstacles.
EDIT - present code- velocities not yet being changed, but looks good!
This image shows how it displays- each boid as a circle and velocity-vector arrow. Now to implement the usual boid-rules..
The Boids Rules
Rule 1: Boids try to fly towards the centre of mass of neighbouring boids.
Rule 2: Boids try to keep a small distance away from other objects (including other boids).
Rule 3: Boids try to match velocity with near boids.
Lots of others to add- edge behaviour ( bounce or wrap). goal-setting, obstacles.
EDIT - present code- velocities not yet being changed, but looks good!
'********************************************
'** **
'** tenochBoids2.bas 16/03/21 **
'** **
'** create a field of flocking boids **
'** **
'********************************************
nomainwin
WindowWidth =620
WindowHeight =620
'Each boid represented in a csv string of x, y, vx, vy
'x/y onscreen if in o...600. Velocity of 1 represents one pixel per cycle.
N =100
dim boid$( N)
open "Tenochtitlanuk Boids" for graphics_nsb as #wg
#wg "trapclose quit"
#wg "down ; size 10 ; fill 80 80 255"
gosub [createBoidField]
for H = 1 to N
gosub [drawBoidField]
gosub [updateBoidField]
next H
wait
' __________________________________________________________
[createBoidField]
for i =0 to N -1 ' create a random flock, all on-screen
x =600 *rnd( 1)
y =600 *rnd( 1)
vx = 10 *rnd( 1) -5
vy = 10 *rnd( 1) -5
boid$( i) =str$( x) +"," +str$( y) +"," +str$( vx) +"," +str$( vy)
next i
return
[drawBoidField]
#wg "cls ; fill 80 80 255"
for i =0 to N -1 ' draw current state of flock,
call drawBoid boid$( i)
scan
next i
return
[updateBoidField]
for K =0 to N -1 ' create a random flock, all on-screen
x =val( word$( boid$( K), 1,","))
y =val( word$( boid$( K), 2,","))
vx =val( word$( boid$( K), 3,","))
vy =val( word$( boid$( K), 4,","))
if x <=1 or x >=600 then vx =0 -vx
if y <=1 or y >=600 then vy =0 -vy
x =x +vx
y =y +vy
boid$( K) =str$( x) +"," +str$( y) +"," +str$( vx) +"," +str$( vy)
scan
next K
return
sub drawBoid i$ ' draw an individual boid
#wg "color darkblue"
#wg "size 10"
x =val( word$( i$, 1,","))
y =val( word$( i$, 2,","))
#wg "set "; int( x); " "; int( y)
vx =val( word$( i$, 3,","))
vy =val( word$( i$, 4,","))
magn =( vx^2 +vy^2)^0.5
dirn =Atan2( vy, vx)
nx =x +vx
ny =y +vy
#wg "color white"
#wg "size 1"
#wg "goto "; x -4 *vx; " "; y -4 *vy
end sub
Function Atan2( y, x)
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
sub quit h$
close #h$
end
end sub