Class: Motel::MovementStrategies::Linear

Inherits:
Motel::MovementStrategy show all
Includes:
Rotatable
Defined in:
lib/motel/movement_strategies/linear.rb

Overview

The Linear MovementStrategy moves a location in a linear manner as defined by a unit direction vector and a floating point speed.

Instance Attribute Summary collapse

Attributes included from Rotatable

#rot_theta, #rot_x, #rot_y, #rot_z, #stop_angle

Attributes inherited from Motel::MovementStrategy

#step_delay

Instance Method Summary collapse

Methods included from Rotatable

#change_due_to_rotation?, #init_rotation, #rot_to_s, #rotate, #rotation_json, #valid_rotation?

Methods inherited from Motel::MovementStrategy

json_create

Constructor Details

#initialize(args = {}) ⇒ Linear

Motel::MovementStrategies::Linear initializer

Direction vector will be normalized if not already

if movement strategy is not valid (see #valid?)

Options Hash (args):

  • x (Float)

    coordinate of direction vector

  • :dy (Float)

    coordinate of direction vector

  • :dz (Float)

    z coordinate of direction vector

  • :speed (Float)

    speed to assign to movement strategy

Raises:


44
45
46
47
48
49
50
51
52
# File 'lib/motel/movement_strategies/linear.rb', line 44

def initialize(args = {})
  attr_from_args args, :dx => 1, :dy => 0, :dz => 0, :speed => nil,
                       :stop_distance => nil
  init_rotation(args)
  super(args)

  # normalize direction vector
  @dx, @dy, @dz = Motel::normalize(@dx, @dy, @dz)
end

Instance Attribute Details

#dxObject

Unit vector corresponding to the linear movement direction


25
26
27
# File 'lib/motel/movement_strategies/linear.rb', line 25

def dx
  @dx
end

#dyObject

Unit vector corresponding to the linear movement direction


25
26
27
# File 'lib/motel/movement_strategies/linear.rb', line 25

def dy
  @dy
end

#dzObject

Unit vector corresponding to the linear movement direction


25
26
27
# File 'lib/motel/movement_strategies/linear.rb', line 25

def dz
  @dz
end

#speedObject

Distance the location moves per second


28
29
30
# File 'lib/motel/movement_strategies/linear.rb', line 28

def speed
  @speed
end

#stop_distanceObject

Stop location movement automatically after this distance moved, optional


31
32
33
# File 'lib/motel/movement_strategies/linear.rb', line 31

def stop_distance
  @stop_distance
end

Instance Method Details

#change?(loc) ⇒ Boolean

Returns true if we've moved more than specified distance or change_due_to_rotation? returns true


70
71
72
73
# File 'lib/motel/movement_strategies/linear.rb', line 70

def change?(loc)
  change_due_to_rotation?(loc) ||
  (!stop_distance.nil? && loc.distance_moved >= stop_distance)
end

#move(loc, elapsed_seconds) ⇒ Object

Implementation of Motel::MovementStrategy#move


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/motel/movement_strategies/linear.rb', line 76

def move(loc, elapsed_seconds)
  unless valid?
    ::RJR::Logger.warn "linear movement strategy not valid, not proceeding with move"
    return
  end

  ::RJR::Logger.debug \
    "moving location #{loc.id} via linear movement strategy #{speed} #{dx}/#{dy}/#{dz}"

  # calculate distance and update x,y,z accordingly
  distance = speed * elapsed_seconds

  loc.x += distance * dx
  loc.y += distance * dy
  loc.z += distance * dz
  loc.distance_moved += distance

  # skip rotation if orientation is not set
  unless loc.orientation.any? { |lo| lo.nil? }
    rotate(loc, elapsed_seconds)
  end
end

#to_json(*a) ⇒ Object

Convert movement strategy to json representation and return it


100
101
102
103
104
105
106
107
108
109
# File 'lib/motel/movement_strategies/linear.rb', line 100

def to_json(*a)
  { 'json_class' => self.class.name,
    'data'       => { :step_delay => step_delay,
                      :speed => speed,
                      :stop_distance => stop_distance,
                      :dx => dx,
                      :dy => dy,
                      :dz => dz }.merge(rotation_json)
  }.to_json(*a)
end

#to_sObject

Convert movement strategy to human readable string and return it


112
113
114
115
116
117
# File 'lib/motel/movement_strategies/linear.rb', line 112

def to_s
  s = "linear-("
  s += "#{@speed}->#{@dx.round_to(2)},#{@dy.round_to(2)},#{@dz.round_to(2)})"
  s += ")"
  s
end

#valid?Boolean

Return boolean indicating if this movement strategy is valid

Tests the various attributes of the linear movement strategy, returning 'true' if everything is consistent, else false.

Currently tests

  • direction vector is normalized

  • speed is a valid numeric > 0

  • rotation parameters


63
64
65
66
# File 'lib/motel/movement_strategies/linear.rb', line 63

def valid?
  Motel::normalized?(@dx, @dy, @dz) &&
  @speed.numeric? && @speed > 0 && valid_rotation?
end