Class: Moon::Vector3

Inherits:
Object show all
Includes:
Comparable, Serializable, Serializable::Properties
Defined in:
lib/moon/packages/std/vector3.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(data, depth = 0) ⇒ Moon::Vector3

Returns:


124
125
126
# File 'lib/moon/packages/std/vector3.rb', line 124

def self.load(data, depth = 0)
  new data['x'], data['y'], data['z']
end

.zeroMoon::Vector3

Returns:


119
120
121
# File 'lib/moon/packages/std/vector3.rb', line 119

def self.zero
  new 0.0, 0.0, 0.0
end

Instance Method Details

#<=>(other) ⇒ Object


21
22
23
# File 'lib/moon/packages/std/vector3.rb', line 21

def <=>(other)
  [x, y, z] <=> Vector3.extract(other)
end

#[](index) ⇒ Object


53
54
55
56
57
58
59
# File 'lib/moon/packages/std/vector3.rb', line 53

def [](index)
  case index
  when :x, 'x', 0 then x
  when :y, 'y', 1 then y
  when :z, 'z', 2 then z
  end
end

#[]=(index, value) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/moon/packages/std/vector3.rb', line 61

def []=(index, value)
  case index
  when :x, 'x', 0 then self.x = value
  when :y, 'y', 1 then self.y = value
  when :z, 'z', 2 then self.z = value
  end
end

#absMoon::Vector3

Returns:


85
86
87
# File 'lib/moon/packages/std/vector3.rb', line 85

def abs
  Vector3.new x.abs, y.abs, z.abs
end

#ceilMoon::Vector3

Returns:


80
81
82
# File 'lib/moon/packages/std/vector3.rb', line 80

def ceil
  Vector3.new x.ceil, y.ceil, z.ceil
end

#floorMoon::Vector3

Returns:


75
76
77
# File 'lib/moon/packages/std/vector3.rb', line 75

def floor
  Vector3.new x.floor, y.floor, z.floor
end

#lengthsqFloat

Returns:


95
96
97
# File 'lib/moon/packages/std/vector3.rb', line 95

def lengthsq
  dot(self)
end

#move_towards(target, distance) ⇒ Moon::Vector3

Returns:


105
106
107
108
109
# File 'lib/moon/packages/std/vector3.rb', line 105

def move_towards(target, distance)
  diff = target - self
  angle = Math.atan2(diff.y, diff.x)
  self + Vector3.new(Math.cos(angle) * distance, Math.sin(angle) * distance, 0)
end

#near?(other, threshold) ⇒ Boolean

Returns:


99
100
101
102
# File 'lib/moon/packages/std/vector3.rb', line 99

def near?(other, threshold)
  diff = (self - other).abs
  (diff.x <= threshold.x && diff.y <= threshold.y && diff.z <= threshold.z)
end

#project(v) ⇒ Moon::Vector3

Returns:


90
91
92
# File 'lib/moon/packages/std/vector3.rb', line 90

def project(v)
  v * (dot(v) / v.dot(v))
end

#round(*a) ⇒ Moon::Vector3

Returns:


70
71
72
# File 'lib/moon/packages/std/vector3.rb', line 70

def round(*a)
  Vector3.new x.round(*a), y.round(*a), z.round(*a)
end

#sumFloat

Returns:


12
13
14
# File 'lib/moon/packages/std/vector3.rb', line 12

def sum
  x + y + z
end

#to_hObject


49
50
51
# File 'lib/moon/packages/std/vector3.rb', line 49

def to_h
  { x: x, y: y, z: z }
end

#to_sString

Returns:


45
46
47
# File 'lib/moon/packages/std/vector3.rb', line 45

def to_s
  "#{x}, #{y}, #{z}"
end

#turn_towards(target) ⇒ Moon::Vector3

Returns:


112
113
114
115
116
# File 'lib/moon/packages/std/vector3.rb', line 112

def turn_towards(target)
  diff = target - self
  angle = Math.atan2(diff.y, diff.x)
  Vector3.new(Math.cos(angle), Math.sin(angle), 0)
end

#xyObject Also known as: to_vec2


25
26
27
# File 'lib/moon/packages/std/vector3.rb', line 25

def xy
  Vector2.new x, y
end

#xy=(other) ⇒ Object


29
30
31
# File 'lib/moon/packages/std/vector3.rb', line 29

def xy=(other)
  self.x, self.y = *Vector2.extract(other)
end

#xyzObject Also known as: to_vec3, rgb


33
34
35
# File 'lib/moon/packages/std/vector3.rb', line 33

def xyz
  dup
end

#xyz=(other) ⇒ Object Also known as: rgb=


37
38
39
# File 'lib/moon/packages/std/vector3.rb', line 37

def xyz=(other)
  self.x, self.y, self.z = *Vector3.extract(other)
end

#zero?Boolean

Returns:


17
18
19
# File 'lib/moon/packages/std/vector3.rb', line 17

def zero?
  x == 0 && y == 0 && z == 0
end