Class: Moon::Vector2

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_angle(a) ⇒ Object


143
144
145
# File 'lib/moon/packages/std/vector2.rb', line 143

def self.for_angle(a)
  new Math.cos(a), Math.sin(a)
end

.load(data, depth = 0) ⇒ Object


152
153
154
# File 'lib/moon/packages/std/vector2.rb', line 152

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

.zeroMoon::Vector2


148
149
150
# File 'lib/moon/packages/std/vector2.rb', line 148

def self.zero
  new 0.0, 0.0
end

Instance Method Details

#<=>(other) ⇒ Integer


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

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

#[](index) ⇒ Object


47
48
49
50
51
52
# File 'lib/moon/packages/std/vector2.rb', line 47

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

#[]=(index, value) ⇒ Object


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

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

#absVector2


77
78
79
# File 'lib/moon/packages/std/vector2.rb', line 77

def abs
  Vector2.new x.abs, y.abs
end

#angleFloat


102
103
104
# File 'lib/moon/packages/std/vector2.rb', line 102

def angle
  Math.atan2(y, x)
end

#angle=(n) ⇒ Object


107
108
109
110
111
# File 'lib/moon/packages/std/vector2.rb', line 107

def angle=(n)
  l = length
  self.x = l * Math.cos(n)
  self.y = l * Math.sin(n)
end

#ceilVector2


72
73
74
# File 'lib/moon/packages/std/vector2.rb', line 72

def ceil
  Vector2.new x.ceil, y.ceil
end

#floorVector2


67
68
69
# File 'lib/moon/packages/std/vector2.rb', line 67

def floor
  Vector2.new x.floor, y.floor
end

#lengthsqFloat


97
98
99
# File 'lib/moon/packages/std/vector2.rb', line 97

def lengthsq
  dot(self)
end

#move_towards(target, distance) ⇒ Object


133
134
135
136
# File 'lib/moon/packages/std/vector2.rb', line 133

def move_towards(target, distance)
  angle = (target - self).angle
  self + Vector2.new(Math.cos(angle) * distance, Math.sin(angle) * distance)
end

#near?(other, dist) ⇒ Boolean


126
127
128
129
# File 'lib/moon/packages/std/vector2.rb', line 126

def near?(other, dist)
  length = (self - other).length
  length < (dist * dist)
end

#perpVector2


82
83
84
# File 'lib/moon/packages/std/vector2.rb', line 82

def perp
  Vector2.new(-y, x)
end

#project(v) ⇒ Vector2


92
93
94
# File 'lib/moon/packages/std/vector2.rb', line 92

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

#round(*a) ⇒ Vector2


62
63
64
# File 'lib/moon/packages/std/vector2.rb', line 62

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

#rperpVector2


87
88
89
# File 'lib/moon/packages/std/vector2.rb', line 87

def rperp
  Vector2.new(y, -x)
end

#slerp(v, t) ⇒ Object


113
114
115
116
117
118
119
120
121
122
# File 'lib/moon/packages/std/vector2.rb', line 113

def slerp(v, t)
  omega = [[normalize.dot(v.normalize), 1.0].min, -1.0].max
  if omega < 1e-3
    lerp(v, t)
  else
    denom = 1.0 / Math.sin(omega)
    (self * ((Math.sin(1.0 - t) * omega) * denom)) +
    (v * ((Math.sin(t) * omega) * denom))
  end
end

#sumFloat


11
12
13
# File 'lib/moon/packages/std/vector2.rb', line 11

def sum
  x + y
end

#to_hHash<Symbol, Float>


43
44
45
# File 'lib/moon/packages/std/vector2.rb', line 43

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

#to_sString


38
39
40
# File 'lib/moon/packages/std/vector2.rb', line 38

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

#turn_towards(target) ⇒ Object


138
139
140
141
# File 'lib/moon/packages/std/vector2.rb', line 138

def turn_towards(target)
  angle = (target - self).angle
  Vector2.new(Math.cos(angle), Math.sin(angle))
end

#xyMoon::Vector2 Also known as: to_vec2


26
27
28
# File 'lib/moon/packages/std/vector2.rb', line 26

def xy
  dup
end

#xy=(other) ⇒ Object


31
32
33
# File 'lib/moon/packages/std/vector2.rb', line 31

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

#zero?Boolean


16
17
18
# File 'lib/moon/packages/std/vector2.rb', line 16

def zero?
  x == 0.0 && y == 0.0
end