Class: Stagecraft::Controls::Orbit

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/controls/orbit_controls.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(camera, window = nil, target: Larb::Vec3.new) ⇒ Orbit

Returns a new instance of Orbit.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/stagecraft/controls/orbit_controls.rb', line 10

def initialize(camera, window = nil, target: Larb::Vec3.new)
  @camera = camera
  @target = vector(target)
  @enabled = true
  @rotate_speed = 0.005
  @zoom_speed = 0.15
  @pan_speed = 0.002
  @damping = 12.0
  @min_distance = 0.01
  @max_distance = Float::INFINITY
  @min_polar_angle = 1e-4
  @max_polar_angle = Math::PI - 1e-4
  @azimuth_delta = 0.0
  @polar_delta = 0.0
  @zoom_delta = 0.0
  @pan_delta = Larb::Vec3.new
  @dragging = false
  sync_spherical
  attach(window) if window
end

Instance Attribute Details

#cameraObject (readonly)

Returns the value of attribute camera.



6
7
8
# File 'lib/stagecraft/controls/orbit_controls.rb', line 6

def camera
  @camera
end

#dampingObject

Returns the value of attribute damping.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def damping
  @damping
end

#enabledObject

Returns the value of attribute enabled.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def enabled
  @enabled
end

#max_distanceObject

Returns the value of attribute max_distance.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def max_distance
  @max_distance
end

#max_polar_angleObject

Returns the value of attribute max_polar_angle.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def max_polar_angle
  @max_polar_angle
end

#min_distanceObject

Returns the value of attribute min_distance.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def min_distance
  @min_distance
end

#min_polar_angleObject

Returns the value of attribute min_polar_angle.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def min_polar_angle
  @min_polar_angle
end

#pan_speedObject

Returns the value of attribute pan_speed.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def pan_speed
  @pan_speed
end

#rotate_speedObject

Returns the value of attribute rotate_speed.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def rotate_speed
  @rotate_speed
end

#targetObject

Returns the value of attribute target.



6
7
8
# File 'lib/stagecraft/controls/orbit_controls.rb', line 6

def target
  @target
end

#zoom_speedObject

Returns the value of attribute zoom_speed.



7
8
9
# File 'lib/stagecraft/controls/orbit_controls.rb', line 7

def zoom_speed
  @zoom_speed
end

Instance Method Details

#pan(delta_x, delta_y) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/stagecraft/controls/orbit_controls.rb', line 51

def pan(delta_x, delta_y)
  return self unless enabled

  rotation = camera.world_matrix.extract_rotation
  right = rotation * Larb::Vec3.right
  up = rotation * Larb::Vec3.up
  scale = @radius * pan_speed
  @pan_delta = @pan_delta + (right * (-Float(delta_x) * scale)) + (up * (Float(delta_y) * scale))
  self
end

#rotate(delta_x, delta_y) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/stagecraft/controls/orbit_controls.rb', line 36

def rotate(delta_x, delta_y)
  return self unless enabled

  @azimuth_delta -= Float(delta_x) * rotate_speed
  @polar_delta -= Float(delta_y) * rotate_speed
  self
end

#update(dt = 1.0 / 60.0) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stagecraft/controls/orbit_controls.rb', line 62

def update(dt = 1.0 / 60.0)
  return self unless enabled

  response = 1.0 - Math.exp(-damping * [Float(dt), 0.0].max)
  @azimuth += @azimuth_delta * response
  @polar = (@polar + (@polar_delta * response)).clamp(min_polar_angle, max_polar_angle)
  @radius = (@radius * Math.exp(@zoom_delta * response)).clamp(min_distance, max_distance)
  @target = @target + (@pan_delta * response)
  decay = 1.0 - response
  @azimuth_delta *= decay
  @polar_delta *= decay
  @zoom_delta *= decay
  @pan_delta = @pan_delta * decay
  update_camera
  self
end

#zoom(delta) ⇒ Object



44
45
46
47
48
49
# File 'lib/stagecraft/controls/orbit_controls.rb', line 44

def zoom(delta)
  return self unless enabled

  @zoom_delta += Float(delta) * zoom_speed
  self
end