Class: Lotu::SteeringSystem

Inherits:
BaseSystem show all
Defined in:
lib/lotu/systems/steering_system.rb

Defined Under Namespace

Modules: UserMethods

Instance Method Summary collapse

Methods inherited from BaseSystem

#draw, #dt

Constructor Details

#initialize(user, opts = {}) ⇒ SteeringSystem

Returns a new instance of SteeringSystem.



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
# File 'lib/lotu/systems/steering_system.rb', line 5

def initialize(user, opts={})
  super

  # Add new functionality to Actor
  @user.extend UserMethods

  # Initialize attributes
  default_opts = {
    :mass => 1,
    :max_speed => 350,
    :max_turn_rate => 180,
    :max_force => 300,
    :wander_radius => 120,
    :wander_distance => 240.0
  }
  opts = default_opts.merge!(opts)
  @user.mass = opts[:mass]
  @user.max_speed = opts[:max_speed]
  @user.max_turn_rate = opts[:max_turn_rate]
  @user.max_force = opts[:max_force]
  @user.wander_radius = opts[:wander_radius]
  @user.wander_distance = opts[:wander_distance]

  # More attributes
  @behaviors = {}
  @force = Vector2d.new
  @zero = Vector2d.new
  @user.pos.x = user.x
  @user.pos.y = user.y
end

Instance Method Details

#activate(behavior) ⇒ Object



73
74
75
# File 'lib/lotu/systems/steering_system.rb', line 73

def activate(behavior)
  @behaviors[behavior] = true
end

#arrive(deceleration = :normal) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lotu/systems/steering_system.rb', line 94

def arrive(deceleration = :normal)
  return @zero if @user.target.nil?
  deceleration_values = {
    :fast => 0.5,
    :normal => 1,
    :slow => 2
  }
  deceleration_tweaker = 1.0
  to_target = @user.target - @user.pos
  distance_to_target = to_target.length

  if distance_to_target > 10
    speed = distance_to_target / (deceleration_tweaker * deceleration_values[deceleration])
    speed = [speed, @user.max_speed].min
    desired_velocity = to_target * speed / distance_to_target
    return desired_velocity - @user.vel
  else
    @user.vel /= 1.15
    @user.accel /= 1.15
  end
  return @zero
end

#deactivate(behavior) ⇒ Object



77
78
79
# File 'lib/lotu/systems/steering_system.rb', line 77

def deactivate(behavior)
  @behaviors[behavior] = false
end

#evadeObject



132
133
134
135
136
137
138
139
# File 'lib/lotu/systems/steering_system.rb', line 132

def evade
  return @zero if @user.pursuer.nil?
  to_pursuer = @user.pursuer.pos - @user.pos
  look_ahead_time = to_pursuer.length / (@user.max_speed + @user.pursuer.vel.length)
  predicted_position = @user.pursuer.pos + @user.pursuer.vel * look_ahead_time
  @user.target = @user.pursuer.pos
  return flee
end

#evade_multipleObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/lotu/systems/steering_system.rb', line 141

def evade_multiple
  return @zero if @user.pursuers.empty?
  combined_velocities = Vector2d.new
  combined_positions = Vector2d.new
  @user.pursuers.each do |p|
    combined_velocities += p.vel
    combined_positions += p.pos
  end
  combined_velocities /= @user.pursuers.length
  combined_positions /= @user.pursuers.length
  to_pursuers = combined_positions - @user.pos
  look_ahead_time = to_pursuers.length / (@user.max_speed + combined_velocities.length)
  predicted_position = combined_positions + combined_velocities * look_ahead_time
  @user.target = combined_positions
  return flee
end

#fleeObject



88
89
90
91
92
# File 'lib/lotu/systems/steering_system.rb', line 88

def flee
  return @zero if @user.target.nil?
  desired_velocity = (@user.pos - @user.target).normalize * @user.max_speed
  return desired_velocity - @user.vel
end

#local_to_world(local_target, heading, side, pos) ⇒ Object



170
171
172
173
174
175
# File 'lib/lotu/systems/steering_system.rb', line 170

def local_to_world(local_target, heading, side, pos)
  local_angle = heading.angle_to(local_target)
  x = Gosu.offset_x(local_angle, local_target.length)
  y = Gosu.offset_y(local_angle, local_target.length)
  world_point = Vector2d.new(x, y) + pos
end

#pursuitObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lotu/systems/steering_system.rb', line 117

def pursuit
  return @zero if @user.evader.nil?
  to_evader = @user.evader.pos - @user.pos
  relative_heading = @user.heading.dot(@user.evader.heading)
  if to_evader.dot(@user.heading) > 0 && relative_heading < -0.95
    @user.target = @user.evader.pos
    return seek
  end

  look_ahead_time = to_evader.length / (@user.max_speed + @user.evader.vel.length)
  predicted_position = @user.evader.pos + @user.evader.vel * look_ahead_time
  @user.target = predicted_position
  return seek
end

#seekObject

The steering behaviors themselves



82
83
84
85
86
# File 'lib/lotu/systems/steering_system.rb', line 82

def seek
  return @zero if @user.target.nil?
  desired_velocity = (@user.target - @user.pos).normalize * @user.max_speed
  return desired_velocity - @user.vel
end

#updateObject



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
# File 'lib/lotu/systems/steering_system.rb', line 36

def update
  @user.pos.x = @user.x
  @user.pos.y = @user.y

  @force.zero!
  @behaviors.each_pair do |behavior, active|
    @force += send(behavior) if active
  end

  @user.accel = @force / @user.mass
  @user.accel.truncate!(@user.max_force)

  max_angle = @user.max_turn_rate * @user.dt
  new_velocity = @user.vel + @user.accel * @user.dt
  angle_to_new_velocity = @user.heading.angle_to(new_velocity)

  if angle_to_new_velocity.abs > max_angle
    sign = @user.heading.sign_to(new_velocity)
    corrected_angle = @user.heading.angle + max_angle * sign
    @user.vel.x = Gosu.offset_x(corrected_angle, new_velocity.length)
    @user.vel.y = Gosu.offset_y(corrected_angle, new_velocity.length)
  else
    @user.vel = new_velocity
  end

  @user.vel.truncate!(@user.max_speed)
  @user.pos += @user.vel * @user.dt

  if @user.vel.length > 0.0001
    @user.heading = @user.vel.normalize
  end

  @user.x = @user.pos.x
  @user.y = @user.pos.y
  @user.angle = @user.heading.angle
end

#wanderObject

TODO: Fix wander



159
160
161
162
163
164
165
166
167
168
# File 'lib/lotu/systems/steering_system.rb', line 159

def wander
  wander_jitter = 10

  @user.wander_target += Vector2d.new(Gosu.random(-1,1), Gosu.random(-1,1))
  @user.wander_target.normalize!
  @user.wander_target *= @user.wander_radius
  target_local = @user.wander_target + Vector2d.new(0, @user.wander_distance)
  target_world = local_to_world(target_local, @user.heading, @user.heading.perp, @user.pos)
  return target_world - @user.pos
end