Permalink
Browse files

inital commit

  • Loading branch information...
0 parents commit 939edda8c0d0a0cc2a7694d1099c2f499c3161cd @brandontreb committed Jun 4, 2011
Showing with 312 additions and 0 deletions.
  1. +128 −0 Boid.lua
  2. +140 −0 Vector2D.lua
  3. +13 −0 build.settings
  4. +10 −0 config.lua
  5. +21 −0 main.lua
128 Boid.lua
@@ -0,0 +1,128 @@
+Boid = {}
+Vector2D = require("Vector2D")
+
+local BOID_SIZE = 4
+
+function Boid:new(location, ms, mf)
+ local object = {
+ maxSpeed = ms,
+ maxForce = mf,
+ loc = location,
+ vel = Vector2D:new(0,0),
+ acc = Vector2D:new(0,0),
+ wanderTheta = 0.0,
+ displayObject = display.newCircle( display.contentWidth / 2, display.contentHeight/2, BOID_SIZE ),
+ }
+
+ object.displayObject:setFillColor(0,255,0)
+
+ setmetatable(object, { __index = Boid })
+ return object
+end
+
+function Boid:run(event)
+ self:update()
+ self:borders()
+ self:render()
+end
+
+function Boid:update()
+ -- Update velocity
+ self.vel:add(self.acc)
+ -- Limit speed
+ self.vel:limit(self.maxSpeed)
+ -- Move boid
+ self.loc:add(self.vel)
+ -- reset acceleration
+ self.acc:mult(0)
+end
+
+function Boid:render()
+ self.displayObject.x = self.loc.x
+ self.displayObject.y = self.loc.y
+end
+
+function Boid:seek(target)
+ self.acc = self:steer(target, false)
+end
+
+function Boid:arrive(target)
+ self.acc = self:steer(target, true)
+end
+
+function Boid:steer(target, slowdown)
+ local steer
+ local desired = Vector2D:Sub(target, self.loc)
+ local d = desired:magnitude()
+
+ if d > 0 then
+ desired:normalize()
+
+ if slowdown and d < 100.0 then
+ local dampSpeed = self.maxSpeed*(d/100.0) -- This damping is somewhat arbitrary
+ desired:mult(dampSpeed)
+ else
+ desired:mult(self.maxSpeed)
+ end
+
+ steer = Vector2D:Sub(desired, self.vel)
+ steer:limit(self.maxForce)
+ else
+ steer = Vector2D:new(0,0)
+ end
+
+ return steer
+end
+
+function Boid:borders()
+ if self.loc.x + BOID_SIZE >= display.contentWidth - 5 then
+ self.wanderTheta = math.pi
+ self.loc.x = self.loc.x - 1
+ end
+ if self.loc.x <= 5 then
+ self.wanderTheta = 0
+ self.loc.x = self.loc.x + 1
+ end
+
+ if self.loc.y <= 5 then
+ self.wanderTheta = math.pi/2
+ self.loc.y = self.loc.y + 1
+ end
+
+ if self.loc.y + BOID_SIZE >= display.contentHeight - 5 then
+ self.wanderTheta = (3 * math.pi) / 2
+ self.loc.y = self.loc.y - 1
+ end
+end
+
+function Boid:wander()
+ local wanderR = 16.0
+ local wanderD = 60.0
+ local change = 0.5
+
+ if self.boidType == 'zombie' then
+ change = 0.01
+ end
+
+ local negChange = math.random(2)
+ local randomNum = math.random() * change
+ if negChange == 2 then
+ self.wanderTheta = self.wanderTheta - randomNum
+ else
+ self.wanderTheta = self.wanderTheta + randomNum
+ end
+
+ local circleLoc = self.vel:copy()
+
+ circleLoc:normalize()
+ circleLoc:mult(wanderD)
+ circleLoc:add(self.loc)
+
+ local circleOffset = Vector2D:new(wanderR*math.cos(self.wanderTheta), wanderR*math.sin(self.wanderTheta))
+ local target = circleLoc:copy()
+ target:add(circleOffset)
+
+ self.acc:add(self:steer(target))
+end
+
+return Boid
140 Vector2D.lua
@@ -0,0 +1,140 @@
+Vector2D = {}
+
+function Vector2D:new(x, y) -- The constructor
+ local object = { x = x, y = y }
+ setmetatable(object, { __index = Vector2D }) -- Inheritance
+ return object
+end
+
+function Vector2D:copy()
+ return Vector2D:new(self.x, self.y)
+end
+
+function Vector2D:copyTo(otherVector)
+ otherVector.x = self.x
+ otherVector.y = self.y
+end
+
+function Vector2D:magnitude()
+ return math.sqrt(self.x^2 + self.y^2)
+end
+
+function Vector2D:normalize()
+ local temp
+ temp = self:magnitude()
+ if temp > 0 then
+ self.x = self.x / temp
+ self.y = self.y / temp
+ end
+end
+
+function Vector2D:limit(l)
+ if self.x > l then
+ self.x = l
+ end
+
+ if self.y > l then
+ self.y = l
+ end
+end
+
+function Vector2D:equals(vec)
+ if self.x == vec.x and self.y == vec.y then
+ return true
+ else
+ return false
+ end
+end
+
+function Vector2D:add(vec)
+ self.x = self.x + vec.x
+ self.y = self.y + vec.y
+end
+
+function Vector2D:sub(vec)
+ self.x = self.x - vec.x
+ self.y = self.y - vec.y
+end
+
+function Vector2D:mult(s)
+ self.x = self.x * s
+ self.y = self.y * s
+end
+
+function Vector2D:div(s)
+ self.x = self.x / s
+ self.y = self.y / s
+end
+
+function Vector2D:dot(vec)
+ return self.x * vec.x + self.y * vec.y
+end
+
+function Vector2D:dist(vec2)
+ return math.sqrt( (vec2.x - self.x) + (vec2.y - self.y) )
+end
+
+-- Class Methods
+
+function Vector2D:Normalize(vec)
+ local tempVec = Vector2D:new(vec.x,vec.y)
+ local temp
+ temp = tempVec:magnitude()
+ if temp > 0 then
+ tempVec.x = tempVec.x / temp
+ tempVec.y = tempVec.y / temp
+ end
+
+ return tempVec
+end
+
+function Vector2D:Limit(vec,l)
+ local tempVec = Vector2D:new(vec.x,vec.y)
+
+ if tempVec.x > l then
+ tempVec.x = l
+ end
+
+ if tempVec.y > l then
+ tempVec.y = l
+ end
+
+ return tempVec
+end
+
+function Vector2D:Add(vec1, vec2)
+ local vec = Vector2D:new(0,0)
+ vec.x = vec1.x + vec2.x
+ vec.y = vec1.y + vec2.y
+ return vec
+end
+
+function Vector2D:Sub(vec1, vec2)
+ local vec = Vector2D:new(0,0)
+ vec.x = vec1.x - vec2.x
+ vec.y = vec1.y - vec2.y
+
+ return vec
+end
+
+function Vector2D:Mult(vec, s)
+ local tempVec = Vector2D:new(0,0)
+ tempVec.x = vec.x * s
+ tempVec.y = vec.y * s
+
+ return tempVec
+end
+
+function Vector2D:Div(vec, s)
+ local tempVec = Vector2D:new(0,0)
+ tempVec.x = vec.x / s
+ tempVec.y = vec.y / s
+
+ return tempVec
+end
+
+function Vector2D:Dist(vec1, vec2)
+ return math.sqrt( (vec2.x - vec1.x) + (vec2.y - vec1.y) )
+end
+
+return Vector2D
13 build.settings
@@ -0,0 +1,13 @@
+settings = {
+ orientation =
+ {
+ default = "landscapeRight",
+ },
+ iphone =
+ {
+ plist=
+ {
+ UIStatusBarHidden=true,
+ },
+ }
+}
10 config.lua
@@ -0,0 +1,10 @@
+application =
+{
+ content =
+ {
+ fps = 30,
+ width = 320,
+ height = 480,
+ scale = "zoomEven"
+ },
+}
21 main.lua
@@ -0,0 +1,21 @@
+local Boid = require('Boid')
+
+-- Define some limiting constants
+MAX_FORCE = 1.75
+MAX_SPEED = 2.0
+
+-- Start our wanderer in the center
+local loc = Vector2D:new(display.contentWidth / 2,display.contentWidth / 2)
+local wanderer = Boid:new(loc,MAX_FORCE,MAX_SPEED)
+
+function animate(event)
+ wanderer:wander()
+ wanderer:run()
+end
+
+function Main()
+ math.randomseed( os.time() )
+ Runtime:addEventListener( "enterFrame", animate );
+end
+
+Main()

0 comments on commit 939edda

Please sign in to comment.