Skip to content
Open
Show file tree
Hide file tree
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
Binary file modified .DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,24 @@ ENV/

# mypy
.mypy_cache/
<<<<<<< HEAD
<<<<<<< HEAD

src/Render.py

src/render.py

src/render.py
=======
=======
>>>>>>> master
.vscode/
.idea/boids.iml
.idea/boids.iml
.idea/misc.xml
*.iml
.idea/workspace.xml
<<<<<<< HEAD
>>>>>>> master
=======
>>>>>>> master
11 changes: 0 additions & 11 deletions .idea/boids.iml

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

553 changes: 0 additions & 553 deletions .idea/workspace.xml

This file was deleted.

Binary file modified src/.DS_Store
Binary file not shown.
47 changes: 47 additions & 0 deletions src/Behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@


def behavior(boid):
<<<<<<< HEAD
<<<<<<< HEAD
max_acc = 1
r = 10
nby = [other for other in boid.flock.boids if boid.id > other.id and boid.flock.distance_matrix[boid.id][other.id] <
r or other.id > boid.id and boid.flock.distance_matrix[other.id][boid.id] < r]

# collision avoidance
threshold = 3
avoid = [other for other in nby if boid.id > other.id and boid.flock.distance_matrix[boid.id][other.id] < threshold
or other.id > boid.id and boid.flock.distance_matrix[other.id][boid.id] < threshold]
v1 = P3.P3(0, 0, 0)
for other in avoid:
v1 += other.position.vectorTo(boid.position) * (1 / boid.position.distance(other.position))
=======
=======
>>>>>>> master
max_acc = .1
max_vel = 25
r = 200
Expand All @@ -14,20 +31,46 @@ def behavior(boid):
v1 = P3.P3(0, 0, 0)
for other in avoid:
v1 += other.position - boid.position * (1 / boid.position.distance(other.position)**2)
<<<<<<< HEAD
>>>>>>> master
=======
>>>>>>> master

# velocity matching
v2 = P3.P3(0, 0, 0)
for b in nby:
<<<<<<< HEAD
<<<<<<< HEAD
v2 += b.vel
if len(nby) > 1:
v2 *= 1/len(nby)
=======
=======
>>>>>>> master
v2 += b.velocity
if len(nby) > 1:
v2 *= 1/len(nby)
v2 -= boid.velocity
<<<<<<< HEAD
>>>>>>> master
=======
>>>>>>> master

# flock centering
v3 = P3.P3(0, 0, 0)
for b in nby:
v3 += b.position
if len(nby) > 1:
<<<<<<< HEAD
<<<<<<< HEAD
v3 *= 1 / len(nby) # might need parenthesis around division

behavior_vel = 10*v1 + 1*v2 + 1*v3
delta_vel = behavior_vel + -1 * boid.vel
return P3.P3.normalize(delta_vel)
=======
=======
>>>>>>> master
v3 *= 1 / len(nby)
v3 -= boid.position

Expand All @@ -42,3 +85,7 @@ def behavior(boid):
acc = P3.P3.normalize(delta_vel) * max_acc

return acc if acc.distance() < delta_vel.distance() else delta_vel
<<<<<<< HEAD
>>>>>>> master
=======
>>>>>>> master
34 changes: 33 additions & 1 deletion src/Boid.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
import P3 # imports 3-D vector class
import P3 # imports 3-D vector class
import Behavior

class Boid:

<<<<<<< HEAD
<<<<<<< HEAD
def __init__(self, flock, id, position, vel, behavior):

self.id = id # holding a unique identifying int
self.position = position # P3 holding the position or (x, y, z) of the boid
self.vel = vel # P3 holding the velosities of the boid
self.behavior = behavior # Float holding the acceleration
=======
def __init__(self, flock, id, position, velocity, behavior):

self.id = id # holding a unique identifying int
self.position = position # P3 holding the position or (x, y, z) of the boid
self.velocity = velocity # P3 holding the velosities of the boid
self.behavior = behavior # Float holding the behavior function
>>>>>>> master
=======
def __init__(self, flock, id, position, velocity, behavior):

self.id = id # holding a unique identifying int
self.position = position # P3 holding the position or (x, y, z) of the boid
self.velocity = velocity # P3 holding the velosities of the boid
self.behavior = behavior # Float holding the behavior function
>>>>>>> master
self.flock = flock # Current flock holding boid object

def move_Boid(self, tick): # Called to change the velocity and position of a particuliar boid

<<<<<<< HEAD
<<<<<<< HEAD
self.vel = self.vel + self.behavior(self) * tick # Update velocity by adding the acceleration and multiplying by the tick

self.position = self.position + self.vel * tick # Update position by adding the velocity and multiplying by the tick

=======
self.velocity = self.velocity + self.behavior(self) * tick # Update velocity by calling behavior function and multiplying by the tick

self.position = self.position + self.velocity * tick # Update position by adding the velocity and multiplying by the tick
>>>>>>> master
=======
self.velocity = self.velocity + self.behavior(self) * tick # Update velocity by calling behavior function and multiplying by the tick

self.position = self.position + self.velocity * tick # Update position by adding the velocity and multiplying by the tick
>>>>>>> master
11 changes: 11 additions & 0 deletions src/Flock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import P3
import math
import random
import Behavior


class Flock:
Expand All @@ -19,11 +20,21 @@ def __init__(self, num_boids, center=P3.P3(0, 0, 0), radius=20): #possibly also
theta = 2 * math.pi * random.random()
x = r * math.cos(theta)
y = r * math.sin(theta)
<<<<<<< HEAD
<<<<<<< HEAD
self.boids.append(Boid.Boid(self, i, P3.P3(x, y, 50) + center, P3.P3(5, 0, 0), Behavior.behavior))
=======
=======
>>>>>>> master
# eventually velocity is taken from config file
v = P3.P3(1*random.random()*random.randrange(-1, 2, 2)+5, 1*random.random()*random.randrange(-1, 2, 2)+5, 0)
self.boids.append(Boid.Boid(self, i, P3.P3(x, y, 0) + center + P3.P3(0, 0, 2*random.randrange(-5, 6, 10)),
v, Behavior.behavior))
self.update_dist_matrix()
<<<<<<< HEAD
>>>>>>> master
=======
>>>>>>> master

def update(self, tick):
for b in self.boids:
Expand Down
7 changes: 7 additions & 0 deletions src/P3.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ def normalize(self):
magnitude = self.distance() if self.distance() != 0 else 1
return P3(self.x/magnitude, self.y/magnitude, self.z/magnitude)

def vectorTo(self, other):
return P3(other.x-self.x, other.y-self.y, other.z-self.z)

def __str__(self):
return '(' + str(self.x) + ', ' + str(self.y) + ', ' + str(self.z) + ')'


if __name__ == "__main__":
p1 = P3(1, 1, 1)
p2 = P3(2, 5, 10)



print(2 * p1)
print(p1 * 2)
print(p1 * p2)
Expand All @@ -49,3 +55,4 @@ def __str__(self):
print(p3 - p1)


print(P3.normalize(p2))
55 changes: 24 additions & 31 deletions src/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import Flock
import Boid
import P3
import os


x = 800
y = 600
z = 1000



Expand Down Expand Up @@ -46,7 +45,8 @@
)

#call to create display
def start(width, hieght, depth):
def start(x, y, width, hieght, depth):
os.environ['SDL_VIDEO_WINDOW_POS'] = str(x) + "," + str(y)
pygame.init()
display = (width, hieght)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
Expand Down Expand Up @@ -107,20 +107,13 @@ def draw_bird(vertices):
glEnd()


glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glColor3fv((1, 1, 1))
glVertex3fv(vertices[vertex])
glEnd()


#call to create a new building
class Buildings(object):
_registry = []
registry = []

def __init__(self, x, y, width, height, depth, color):
self._registry.append(self)
self.registry.append(self)
self.x = x
self.y = y
self.width = width
Expand All @@ -134,22 +127,8 @@ def set_building(self):
final_vertices = []
global building_count

#positoning
for vert in building_vertices:
new_vert = []

new_x = vert[0] + self.x
new_y = vert[1] + self.y
new_z = vert[2]

new_vert.append(new_x)
new_vert.append(new_y)
new_vert.append(new_z)

new_vertices.append(new_vert)

#scaling
for nvert in new_vertices:
for nvert in building_vertices:
fnew_vert = []

newv_x = nvert[0] * self.width
Expand All @@ -160,7 +139,21 @@ def set_building(self):
fnew_vert.append(newv_y)
fnew_vert.append(newv_z)

final_vertices.append(fnew_vert)
new_vertices.append(fnew_vert)

#positoning
for vert in new_vertices:
new_vert = []

new_x = vert[0] + self.x
new_y = vert[1] + self.y
new_z = vert[2]

new_vert.append(new_x)
new_vert.append(new_y)
new_vert.append(new_z)

final_vertices.append(new_vert)

return final_vertices

Expand Down Expand Up @@ -220,7 +213,7 @@ def draw(self):

ground()

for building in Buildings._registry:
for building in Buildings.registry:
Buildings.draw_building(building)

for b in self.flock.boids:
Expand All @@ -241,7 +234,7 @@ def draw(self):
f = Render(flock)

for num in range(10):
Buildings(random.randrange(-10, 10), random.randrange(-10, 10), random.randrange(1, 10), random.randrange(1, 10), random.randrange(1, 10), (random.randrange(0,2), random.randrange(0,2), random.randrange(0,2)))
Buildings(random.randrange(-100, 100), random.randrange(-100, 100), random.randrange(1, 10), random.randrange(1, 10), random.randrange(1, 10), (random.randrange(0,2), random.randrange(0,2), random.randrange(0,2)))

start(800, 600, 1000)

Expand Down