-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoidWander.lua
More file actions
133 lines (107 loc) · 3.01 KB
/
BoidWander.lua
File metadata and controls
133 lines (107 loc) · 3.01 KB
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
--
-- Created by IntelliJ IDEA.
-- User: erik
-- Date: 5/7/12
-- Time: 5:02 PM
-- To change this template use File | Settings | File Templates.
--
local sprite = require( "sprite" )
BoidWander = {}
Vector2D = require("Vector2D")
function BoidWander:new(location, ms, mf, si, bsize, wt, wr, wd, ch)
local object = {
maxSpeed = ms,
maxForce = mf,
loc = location,
vel = Vector2D:new(0,0),
acc = Vector2D:new(0,0),
displayObject = si,
BOID_SIZE = bsize or 4,
wanderTheta = wt or 0.0,
wanderR = wr or 16.0,
wanderD= wd or 60.0,
change = ch or 0.5
}
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 + self.BOID_SIZE >= display.contentWidth - 5 then
self.self.wanderTheta = math.pi
self.loc.x = self.loc.x - 1
end
if self.loc.x <= 5 then
self.self.wanderTheta = 0
self.loc.x = self.loc.x + 1
end
if self.loc.y <= 5 then
self.self.wanderTheta = math.pi/2
self.loc.y = self.loc.y + 1
end
if self.loc.y + self.BOID_SIZE >= display.contentHeight - 5 then
self.self.wanderTheta = (3 * math.pi) / 2
self.loc.y = self.loc.y - 1
end
end
function Boid:wander()
local negChange = math.random(2)
local randomNum = math.random() * self.change
if negChange == 2 then
self.self.wanderTheta = self.self.wanderTheta - randomNum
else
self.self.wanderTheta = self.self.wanderTheta + randomNum
end
local circleLoc = self.vel:copy()
circleLoc:normalize()
circleLoc:mult(wanderD)
circleLoc:add(self.loc)
local circleOffset = Vector2D:new(self.wanderR*math.cos(self.self.wanderTheta), self.wanderR*math.sin(self.self.wanderTheta))
local target = circleLoc:copy()
target:add(circleOffset)
self.acc:add(self:steer(target))
end
return Boid