Class: Boid

Inherits:
Thing show all
Defined in:
ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb

Instance Attribute Summary collapse

Attributes inherited from Thing

#position, #vector, #view

Instance Method Summary collapse

Methods inherited from Thing

#draw, #pixelsPerSecToPixelsPerMove

Constructor Details

#initialize(pos = nil) ⇒ Boid

Returns a new instance of Boid.



18
19
20
21
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 18

def initialize(pos = nil)
	super(pos, nil)
	init
end

Instance Attribute Details

#almostGroundLevelObject

Returns the value of attribute almostGroundLevel.



15
16
17
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 15

def almostGroundLevel
  @almostGroundLevel
end

#flockObject

Returns the value of attribute flock.



15
16
17
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 15

def flock
  @flock
end

#maxSpeedObject

Returns the value of attribute maxSpeed.



15
16
17
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 15

def maxSpeed
  @maxSpeed
end

#maxSpeedSquaredObject

Returns the value of attribute maxSpeedSquared.



15
16
17
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 15

def maxSpeedSquared
  @maxSpeedSquared
end

#perchingTurnsLeftObject

Returns the value of attribute perchingTurnsLeft.



15
16
17
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 15

def perchingTurnsLeft
  @perchingTurnsLeft
end

#wingFlapPosObject

Returns the value of attribute wingFlapPos.



15
16
17
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 15

def wingFlapPos
  @wingFlapPos
end

Instance Method Details

#avoidOthersObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 75

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

#boundPositionObject



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
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 104

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

#initObject



23
24
25
26
27
28
29
30
31
32
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 23

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

#limitSpeedObject



133
134
135
136
137
138
139
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 133

def limitSpeed()
	speedSquared = Point::ORIGIN.squareOfDistanceTo(@vector)
	if speedSquared > @maxSpeedSquared
 f = Math.sqrt(speedSquared) * @maxSpeed
 @vector.divideBy(f)
	end
end

#matchOthersVelocitiesObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 90

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

#moveObject



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
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 34

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

#moveTowardsFlockCenterObject



66
67
68
69
70
71
72
73
# File 'ext/ruby/qtruby/examples/ruboids/ruboids/Boid.rb', line 66

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