-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoidsExample.py
142 lines (114 loc) · 3.9 KB
/
BoidsExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
__author__ = 'david.schoemehl'
from turtle import Turtle, Screen, Vec2D
import random
from math import cos, radians, sqrt, pow, fmod
class Schooler(Turtle):
swarm = []
def __init__(self):
Turtle.__init__(self)
self.up()
self.setheading(random.randrange(360))
self.setpos(random.randrange(-200,200),random.randrange(-200,200))
#self.down()
self.newHead = None
self.velocity = Vec2D(0,0)
self.neighbordist = 200
self.viewangle = 45
self.neighbors = []
Schooler.swarm.append(self)
def moveAllBoidsToNewPositions(self):
v1 = Vec2D(0,0)
v2 = Vec2D(0,0)
v3 = Vec2D(0,0)
v4 = Vec2D(0,0)
windV = Vec2D(0,0)
del self.neighbors[:]
self.findNieghbors()
v1 = self.rule1()
v2 = self.rule2()
v3 = self.rule3()
v4 = self.bound_position()
#limit the max velocity
vlimit = 2
self.velocity = self.velocity + v1 + v2 + v3 + v4 + windV
if (self.mag(self.velocity) > vlimit):
self.velocity = self.unit(self.velocity) * vlimit
#self.color((int(self.pos()[0]%254),0,0), (int(self.pos()[0]%254),0,0))
#self.color( (10, 0, 0), (10, 0, 0))
self.setheading(self.towards(self.pos() + self.velocity))
self.setpos(self.pos() + self.velocity)
def findNieghbors(self):
for other in Schooler.swarm:
if self != other:
if abs(self.pos() - other.pos()) < self.neighbordist:
head = self.towards(other) - self.heading()
#if cos(radians(head)) < 15:
print abs(head)
if abs(head) < self.viewangle:
self.neighbors.append(other)
#print len(self.neighbors)
def rule1(self):
percievedCenter = Vec2D(0,0)
print len(self.neighbors)
if(len(self.neighbors)):
for other in self.neighbors:
if self != other:
percievedCenter += other.pos()
percievedCenter = Vec2D(percievedCenter[0]/(int(len(self.neighbors))), percievedCenter[1]/(int(len(self.neighbors))))
percievedCenter = (percievedCenter - self.pos())
return percievedCenter * .01
def rule2(self):
repulsionVector = Vec2D(0,0)
for other in self.neighbors:
if self != other:
if abs(self.pos() - other.pos()) < 40:
repulsionVector = repulsionVector - (other.pos() - self.pos())
return repulsionVector
def rule3(self):
averageVelocity = Vec2D(0,0)
for other in self.neighbors:
if self !=other:
averageVelocity = averageVelocity + other.velocity
averageVelocity = averageVelocity *(1/(len(Schooler.swarm) - 1))
return averageVelocity
def bound_position(self):
xmin = -600
xmax = 600
ymin = -600
ymax = 600
xadj = 0
yadj = 0
if self.pos()[0] < xmin:
xadj = 10
if self.pos()[0] > xmax:
xadj = -10
if self.pos()[1] < ymin:
yadj = 10
if self.pos()[1] > ymax:
yadj = -10
#print xadj, yadj
return Vec2D(xadj,yadj)
def mag(self,vec):
return sqrt( pow( vec[0],2) + pow(vec[1],2) )
def unit(self,vec):
magnitude = self.mag(vec)
return Vec2D(vec[0]/magnitude, vec[1]/magnitude)
def main():
swarmSize = 30
t = Turtle()
win = Screen()
win.setworldcoordinates(-600,-600,600,600)
t.speed(10)
t.tracer(15)
t.hideturtle()
for i in range(swarmSize):
Schooler()
#for turn in range(1000):
while True:
try:
for schooler in Schooler.swarm:
schooler.moveAllBoidsToNewPositions()
except KeyboardInterrupt:
break
win.exitonclick()
main()