Skip to content

Instantly share code, notes, and snippets.

@bengolder
Created August 3, 2011 01:48
Show Gist options
  • Save bengolder/1121701 to your computer and use it in GitHub Desktop.
Save bengolder/1121701 to your computer and use it in GitHub Desktop.
CookieBoids ARCH 124B section 002 example
from Rhino.Geometry import Point3d, Vector3d, Curve
# define our class
class RoboCookie:
def __init__(self, point):
self.flavor = "strawberry"
self.location = point
self.friends = None
self.direction = Vector3d( -point.X, -point.Y, -point.Z )
self.direction.Unitize()
self.direction = Vector3d.Multiply( stepSize, self.direction )
self.tail = [point]
def broadcastLocation(self):
print "I am at:", self.location
def findFriends(self, all):
self.friends = []
for cookie in all:
if cookie != self:
distance = self.location.DistanceTo( cookie.location )
if distance < friendDistance:
self.friends.append( cookie )
def fly(self):
newPoint = Point3d.Add( self.location, self.direction )
self.tail.append( newPoint )
self.location = newPoint
def findDirection(self):
# align to friends
# move towards friends
if self.friends:
alignVector = self.align()
cohereVector = self.cohere()
newDirection = Vector3d.Add( alignVector, cohereVector )
newDirection.Unitize()
self.direction = Vector3d.Multiply( stepSize, newDirection )
def align(self):
others = self.friends[:]
vector = others.pop(0).direction
for other in others:
vector = Vector3d.Add( vector, other.direction )
vector.Unitize()
return vector
def cohere(self):
others = self.friends[:]
point = others.pop(0).location
vector = Point3d.Subtract( point, self.location)
for other in others:
newPoint = other.location
newVector = Point3d.Subtract( self.location, newPoint )
vector = Vector3d.Add( vector, newVector )
vector.Unitize()
return vector
# run our script
cookies = []
# make all the cookies
for point in startPoints:
newCookie = RoboCookie( point )
cookies.append( newCookie )
newCookie.broadcastLocation()
for i in range(numSteps):
for cookie in cookies:
# find friends
cookie.findFriends( cookies )
# find direction
cookie.findDirection()
# move each cookie
for cookie in cookies:
cookie.fly()
cookiePositions = []
trails = []
# find friends
for cookie in cookies:
cookie.findFriends( cookies )
cookiePositions.append( cookie.location )
curve = Curve.CreateControlPointCurve( cookie.tail, 3 )
trails.append( curve )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment