Class: Ruboid::Boid
- Inherits:
-
Object
- Object
- Ruboid::Boid
- Defined in:
- lib/ruboid/ruboid.rb
Overview
Represents a boid : You can think of it as a fish or a bird
Instance Attribute Summary collapse
-
#position ⇒ Object
position of the boid.
-
#velocity ⇒ Object
distance done by the boid by unit of time chosen by the application using the lib.
Instance Method Summary collapse
-
#alignment(boids) ⇒ Object
Modifies the velocity so the boids have the tendency to go in the same direction : Rule 3 of Conrad Parker’s pseudo code.
- #bound(bound_corner1, bound_corner2, bound_encouragement) ⇒ Object
-
#cohesion(boids) ⇒ Object
Moves the boid to the center of mass : Rule 1 of Conrad Parker’s pseudo code.
- #dimension ⇒ Object
- #go_to(goal) ⇒ Object
-
#initialize(position, velocity) ⇒ Boid
constructor
the initial parameters of the boid are the responsibility of the calling application.
-
#limit(velocity_limit) ⇒ Object
Limits the velocity of the boid so it doesn’t go supersonic.
-
#move ⇒ Object
Move a boid of one step.
-
#separation(boids, distance) ⇒ Object
Moves the boid away from the other boids which are too close to it : Rule 2 of Conrad Parker’s pseudo code.
Constructor Details
#initialize(position, velocity) ⇒ Boid
the initial parameters of the boid are the responsibility of the calling application
143 144 145 146 |
# File 'lib/ruboid/ruboid.rb', line 143 def initialize(position,velocity) @position = position @velocity = velocity end |
Instance Attribute Details
#position ⇒ Object
position of the boid
138 139 140 |
# File 'lib/ruboid/ruboid.rb', line 138 def position @position end |
#velocity ⇒ Object
distance done by the boid by unit of time chosen by the application using the lib
140 141 142 |
# File 'lib/ruboid/ruboid.rb', line 140 def velocity @velocity end |
Instance Method Details
#alignment(boids) ⇒ Object
Modifies the velocity so the boids have the tendency to go in the same direction : Rule 3 of Conrad Parker’s pseudo code
206 207 208 209 210 211 212 213 214 |
# File 'lib/ruboid/ruboid.rb', line 206 def alignment(boids) perceived_velocity = Vector.zero(dimension) boids.each do |b| if b != self perceived_velocity.add(b.velocity) end end perceived_velocity.div(boids.length - 1).sub(@velocity) end |
#bound(bound_corner1, bound_corner2, bound_encouragement) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ruboid/ruboid.rb', line 169 def bound(bound_corner1,bound_corner2,bound_encouragement) v = Vector.zero(dimension) v.each_index do |i| if @position[i] < bound_corner1[i] v[i] = bound_encouragement[i] elsif @position[i] > bound_corner2[i] v[i] = -bound_encouragement[i] end end v end |
#cohesion(boids) ⇒ Object
Moves the boid to the center of mass : Rule 1 of Conrad Parker’s pseudo code
182 183 184 185 186 187 188 189 190 |
# File 'lib/ruboid/ruboid.rb', line 182 def cohesion(boids) perceived_center = Vector.zero(dimension) boids.each do |b| if b != self perceived_center.add(b.position) end end perceived_center.div(boids.length - 1).sub(@position) end |
#dimension ⇒ Object
148 149 150 |
# File 'lib/ruboid/ruboid.rb', line 148 def dimension @position.dimension end |
#go_to(goal) ⇒ Object
165 166 167 |
# File 'lib/ruboid/ruboid.rb', line 165 def go_to(goal) goal.clone.sub(@position) end |
#limit(velocity_limit) ⇒ Object
Limits the velocity of the boid so it doesn’t go supersonic.
158 159 160 161 162 163 |
# File 'lib/ruboid/ruboid.rb', line 158 def limit(velocity_limit) velocity_norm = @velocity.norm if velocity_norm > velocity_limit @velocity.mul(velocity_limit / velocity_norm) end end |
#move ⇒ Object
Move a boid of one step
153 154 155 |
# File 'lib/ruboid/ruboid.rb', line 153 def move @position.add(@velocity) end |
#separation(boids, distance) ⇒ Object
Moves the boid away from the other boids which are too close to it : Rule 2 of Conrad Parker’s pseudo code
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ruboid/ruboid.rb', line 193 def separation(boids,distance) c = Vector.zero(dimension) boids.each do |b| if b != self if b.position.distance_to(@position) < distance c.add(@position).sub(b.position) end end end c end |