Class: Lotu::SteeringSystem
- Inherits:
-
Object
- Object
- Lotu::SteeringSystem
- Defined in:
- lib/lotu/systems/steering_system.rb
Defined Under Namespace
Modules: UserMethods
Instance Method Summary collapse
- #activate(behavior) ⇒ Object
- #arrive(deceleration = :normal) ⇒ Object
- #deactivate(behavior) ⇒ Object
- #evade ⇒ Object
- #evade_multiple ⇒ Object
- #flee ⇒ Object
-
#initialize(user, opts = {}) ⇒ SteeringSystem
constructor
A new instance of SteeringSystem.
- #local_to_world(local_target, heading, side, pos) ⇒ Object
- #pursuit ⇒ Object
-
#seek ⇒ Object
The steering behaviors themselves.
- #update ⇒ Object
-
#wander ⇒ Object
TODO: Fix wander.
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={}) # 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 @user = user @behaviors = {} @force = Vector2d.new @zero = Vector2d.new user.pos.x = user.x user.pos.y = user.y end |
Instance Method Details
#activate(behavior) ⇒ Object
70 71 72 |
# File 'lib/lotu/systems/steering_system.rb', line 70 def activate(behavior) @behaviors[behavior] = true end |
#arrive(deceleration = :normal) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/lotu/systems/steering_system.rb', line 91 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
74 75 76 |
# File 'lib/lotu/systems/steering_system.rb', line 74 def deactivate(behavior) @behaviors[behavior] = false end |
#evade ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/lotu/systems/steering_system.rb', line 129 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_multiple ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lotu/systems/steering_system.rb', line 138 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 |
#flee ⇒ Object
85 86 87 88 89 |
# File 'lib/lotu/systems/steering_system.rb', line 85 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
167 168 169 170 171 172 |
# File 'lib/lotu/systems/steering_system.rb', line 167 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 |
#pursuit ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/lotu/systems/steering_system.rb', line 114 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 |
#seek ⇒ Object
The steering behaviors themselves
79 80 81 82 83 |
# File 'lib/lotu/systems/steering_system.rb', line 79 def seek return @zero if @user.target.nil? desired_velocity = (@user.target - @user.pos).normalize * @user.max_speed return desired_velocity - @user.vel end |
#update ⇒ Object
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 |
# File 'lib/lotu/systems/steering_system.rb', line 36 def update @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 |
#wander ⇒ Object
TODO: Fix wander
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/lotu/systems/steering_system.rb', line 156 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 |