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

Boids: Renamed variables for readability #5

Open
wants to merge 3 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
38 changes: 16 additions & 22 deletions boids/boids.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,22 @@
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)

def update_boids(boids):
xs,ys,xvs,yvs=boids
def update_boids(boids, middle_param=0.01, nearby_away = 100, nearby_match_speed = 10000):
x_positions,y_positions,x_velocities,y_velocities=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])
for i in x_positions:
for j in x_positions:
x_velocities[i]=x_velocities[i]+(x_positions[j]-x_positions[i])*middle_param/len(x_positions)
y_velocities[i]=y_velocities[i]+(y_positions[j]-y_positions[i])*middle_param/len(x_positions)
# Fly away from nearby boids
if (x_positions[j]-x_positions[i])**2 + (y_positions[j]-y_positions[i])**2 < nearby_away:
x_velocities[i]=x_velocities[i]+(x_positions[i]-x_positions[j])
y_velocities[i]=y_velocities[i]+(y_positions[i]-y_positions[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)
if (x_positions[j]-x_positions[i])**2 + (y_positions[j]-y_positions[i])**2 < nearby_match_speed:
x_velocities[i]=x_velocities[i]+(x_velocities[j]-x_velocities[i])*0.125/len(x_positions)
y_velocities[i]=y_velocities[i]+(y_velocities[j]-y_velocities[i])*0.125/len(x_positions)
# Move according to velocities
for i in range(len(xs)):
xs[i]=xs[i]+xvs[i]
ys[i]=ys[i]+yvs[i]
x_positions[i]=x_positions[i]+x_velocities[i]
y_positions[i]=y_positions[i]+y_velocities[i]