Module: Motel::MovementStrategies::Rotatable

Included in:
Follow, Linear, Rotate
Defined in:
lib/motel/movement_strategies/rotate.rb

Overview

Mixin to include in other modules to provide rotation capabilities while undergoing other movement

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rot_thetaObject

Axis angle describing rotation


16
17
18
# File 'lib/motel/movement_strategies/rotate.rb', line 16

def rot_theta
  @rot_theta
end

#rot_xObject

Axis angle describing rotation


16
17
18
# File 'lib/motel/movement_strategies/rotate.rb', line 16

def rot_x
  @rot_x
end

#rot_yObject

Axis angle describing rotation


16
17
18
# File 'lib/motel/movement_strategies/rotate.rb', line 16

def rot_y
  @rot_y
end

#rot_zObject

Axis angle describing rotation


16
17
18
# File 'lib/motel/movement_strategies/rotate.rb', line 16

def rot_z
  @rot_z
end

#stop_angleObject

Stop location rotation automatically after this many degrees, optional


19
20
21
# File 'lib/motel/movement_strategies/rotate.rb', line 19

def stop_angle
  @stop_angle
end

Instance Method Details

#change_due_to_rotation?(loc) ⇒ Boolean

Return boolean indicating if location has rotated by specified stop_angle

Returns:

  • (Boolean)

38
39
40
# File 'lib/motel/movement_strategies/rotate.rb', line 38

def change_due_to_rotation?(loc)
  !stop_angle.nil? && loc.angle_rotated >= stop_angle
end

#init_rotation(args = {}) ⇒ Object

Initialize rotation params from args hash


22
23
24
25
26
27
28
# File 'lib/motel/movement_strategies/rotate.rb', line 22

def init_rotation(args = {})
  attr_from_args args, :rot_theta => 0,
                       :rot_x     => 0,
                       :rot_y     => 0,
                       :rot_z     => 1,
                       :stop_angle => nil
end

#rot_to_sObject

Return string representation of rotation


61
62
63
# File 'lib/motel/movement_strategies/rotate.rb', line 61

def rot_to_s
  "#{rot_theta.round_to(2)}/#{rot_x.round_to(2)}/#{rot_y.round_to(2)}/#{rot_z.round_to(2)}"
end

#rotate(loc, elapsed_seconds) ⇒ Object

Rotate the specified location. Takes same parameters as Motel::MovementStrategy#move to update location's orientation after the specified elapsed interval.


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/motel/movement_strategies/rotate.rb', line 45

def rotate(loc, elapsed_seconds)
  # update location's orientation
  # TODO if loc.angle_rotated + angle_rotated > stop_angle only move by  stop_angle - loc.angle_rotated
  angle_rotated = @rot_theta * elapsed_seconds
  nor =
    Motel.rotate(loc.orx, loc.ory, loc.orz,
                 angle_rotated,
                 @rot_x, @rot_y, @rot_z)
  loc.orx = nor[0]
  loc.ory = nor[1]
  loc.orz = nor[2]
  loc.angle_rotated += angle_rotated
  loc.orientation
end

#rotation_jsonObject

Return rotation params to incorporate in json value


66
67
68
69
70
71
72
# File 'lib/motel/movement_strategies/rotate.rb', line 66

def rotation_json
  {:rot_theta => rot_theta,
   :rot_x     => rot_x,
   :rot_y     => rot_y,
   :rot_z     => rot_z,
   :stop_angle => stop_angle}
end

#valid_rotation?Boolean

Return boolean indicating if rotation parameters are valid

Returns:

  • (Boolean)

31
32
33
34
35
# File 'lib/motel/movement_strategies/rotate.rb', line 31

def valid_rotation?
 @rot_theta.numeric? && @rot_theta > -6.28 && @rot_theta < 6.28 &&
 @rot_x.numeric? && @rot_y.numeric? && @rot_z.numeric? &&
 Motel.normalized?(@rot_x, @rot_y, @rot_z)
end