blob: ff28a043a884e02ccaa720869e71a20a3ebfde44 (
plain)
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
|
#
# Copyright (c) 2001 by Jim Menard <jimm@io.com>
#
# Released under the same license as Ruby. See
# http://www.ruby-lang.org/en/LICENSE.txt.
#
retquire 'BoidView'
retquire 'Flock'
retquire 'Point'
retquire 'Params'
class Boid < Thing
attr_accessor :maxSpeed, :maxSpeedSquared, :perchingTurnsLeft,
:wingFlapPos, :almostGroundLevel, :flock
def initialize(pos = nil)
super(pos, nil)
init
end
def init
@maxSpeed = $PARAMS['boid_max_speed']
@maxSpeedSquared = @maxSpeed * @maxSpeed
@flock = nil # set by flock when flock adds to self
@wingFlapPos = rand(7)
@perchingTurnsLeft = 0
@almostGroundLevel = 5.0
@view = BoidView.new(self)
end
def move
# Flap wings. Only flap occasionally if not perching.
if (@perchingTurnsLeft == 0 ||
rand(100) < $PARAMS['boid_perch_wing_flap_percent'])
@wingFlapPos = (@wingFlapPos + 1) & 7
end
if @perchingTurnsLeft > 0
# Only take off when wing flap position == 2.
if --@perchingTurnsLeft == 0 && @wingFlapPos != 2
@perchingTurnsLeft = (8 + 2 - @wingFlapPos) & 7
return
end
end
moveTowardsFlockCenter()
avoidOthers()
matchOthersVelocities()
boundPosition()
limitSpeed()
super() # Add velocity vector to position.
# Boids at ground level perch for a while.
if @position.y < @almostGroundLevel
@position.y = @almostGroundLevel
@vector.x = @vector.y = @vector.z = 0
@perchingTurnsLeft =
rand($PARAMS['boid_max_perching_turns'])
end
end
def moveTowardsFlockCenter()
flockCenter = @flock.centerExcluding(self)
flockCenter.subtractPoint(@position)
# Move 1% of the way towards the center
flockCenter.divideBy(100.0)
@vector.addPoint(flockCenter)
end
def avoidOthers()
c = Point.new()
@flock.members.each { | b |
if b != self
otherPos = b.position
if @position.squareOfDistanceTo(otherPos) <
$PARAMS['boid_square_of_personal_space_dist']
c.addPoint(@position)
c.subtractPoint(otherPos)
end
end
}
@vector.addPoint(c)
end
def matchOthersVelocities()
vel = Point.new()
flock.members.each { | b |
if b != self
vel.addPoint(b.vector)
end
}
vel.divideBy(flock.members.length - 1)
vel.subtractPoint(@vector)
vel.divideBy(8)
@vector.addPoint(vel)
end
def boundPosition()
v = Point.new
halfWidth = $PARAMS['world_width'] / 2
halfHeight = $PARAMS['world_height'] / 2
halfDepth = $PARAMS['world_depth'] / 2
if position.x < -halfWidth
v.x = $PARAMS['boid_bounds_limit_pull']
elsif position.x > halfWidth
v.x = -$PARAMS['boid_bounds_limit_pull']
end
if position.y < -halfHeight + almostGroundLevel +
$PARAMS['boid_bounds_limit_above_ground_level']
v.y = $PARAMS['boid_bounds_limit_pull']
elsif position.y > halfHeight
v.y = -$PARAMS['boid_bounds_limit_pull']
end
if position.z < -halfDepth
v.z = $PARAMS['boid_bounds_limit_pull']
elsif position.z > halfDepth
v.z = -$PARAMS['boid_bounds_limit_pull']
end
@vector.addPoint(v)
end
def limitSpeed()
speedSquared = Point::ORIGIN.squareOfDistanceTo(@vector)
if speedSquared > @maxSpeedSquared
f = Math.sqrt(speedSquared) * @maxSpeed
@vector.divideBy(f)
end
end
end
|