Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the boids #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions boids/boids.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@
This code simulates the swarming behaviour of bird-like objects ("boids").
"""

from matplotlib import pyplot as plt
from matplotlib import animation
#import random

import random
# boids_x = [random.uniform(-450, 50.0) for x in range(50)]
# boids_y = [random.uniform(300.0, 600.0) for x in range(50)]
# boid_x_velocities = [random.uniform(0, 10.0) for x in range(50)]
# boid_y_velocities = [random.uniform(-20.0, 20.0) for x in range(50)]
# boids = (boids_x, boids_y, boid_x_velocities, boid_y_velocities)

boids_x=[random.uniform(-450,50.0) for x in range(50)]
boids_y=[random.uniform(300.0,600.0) for x in range(50)]
boid_x_velocities=[random.uniform(0,10.0) for x in range(50)]
boid_y_velocities=[random.uniform(-20.0,20.0) for x in range(50)]
boids=(boids_x,boids_y,boid_x_velocities,boid_y_velocities)
boids_x = [-300.0, 20.0, 3.0]
boids_y = [420, 480, 540]
boids_x_velocities = [1.0, 2.0, 7.0]
boids_y_velocities = [-15.0, 10.0, 15.0]
boids = (boids_x, boids_y, boids_x_velocities, boids_y_velocities)

def update_boids(boids):
xs,ys,xvs,yvs=boids
# Fly towards the middle
for i in range(len(xs)):
for j in range(len(xs)):
xvs[i]=xvs[i]+(xs[j]-xs[i])*0.01/len(xs)
for i in range(len(xs)):
for j in range(len(xs)):
yvs[i]=yvs[i]+(ys[j]-ys[i])*0.01/len(xs)
# Fly away from nearby boids
for i in range(len(xs)):
for j in range(len(xs)):
if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 100:
xvs[i]=xvs[i]+(xs[i]-xs[j])
yvs[i]=yvs[i]+(ys[i]-ys[j])
# Try to match speed with nearby boids
for i in range(len(xs)):
for j in range(len(xs)):
if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 10000:
xvs[i]=xvs[i]+(xvs[j]-xvs[i])*0.125/len(xs)
yvs[i]=yvs[i]+(yvs[j]-yvs[i])*0.125/len(xs)
# Move according to velocities
for i in range(len(xs)):
xs[i]=xs[i]+xvs[i]
ys[i]=ys[i]+yvs[i]
x_pos, y_pos, x_vel, y_vel = boids
number_of_boids = len(x_pos)

for i in range(number_of_boids):
for j in range(number_of_boids):
diff_x = x_pos[j] - x_pos[i]
diff_y = y_pos[j] - y_pos[i]
# Fly towards the middle
x_vel[i] = x_vel[i] + (diff_x) * 0.01 / number_of_boids
y_vel[i] = y_vel[i] + (diff_y) * 0.01 / number_of_boids
# Fly away from nearby boids
if (diff_x) ** 2 + (diff_y) ** 2 < 100:
x_vel[i] = x_vel[i] + (diff_x)
y_vel[i] = y_vel[i] + (diff_y)
# Try to match speed with nearby boids
if (diff_x) ** 2 + (diff_y) ** 2 < 10000:
x_vel[i] = x_vel[i] + (x_vel[j] - x_vel[i]) * 0.125 / number_of_boids
y_vel[i] = y_vel[i] + (y_vel[j] - y_vel[i]) * 0.125 / number_of_boids
# Move according to velocities
x_pos[i] = x_pos[i] + x_vel[i]
y_pos[i] = y_pos[i] + y_vel[i]

print(x_pos, y_pos, x_vel, y_vel)

update_boids(boids)
print("hi")