Class: CyberarmEngine::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/vector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0, z = 0, weight = 0) ⇒ Vector

Returns a new instance of Vector.



27
28
29
# File 'lib/cyberarm_engine/vector.rb', line 27

def initialize(x = 0, y = 0, z = 0, weight = 0)
  @x, @y, @z, @weight = x, y, z, weight
end

Class Method Details

.backwardObject



23
24
25
# File 'lib/cyberarm_engine/vector.rb', line 23

def self.backward
  Vector.new(0, 0, -1)
end

.downObject



7
8
9
# File 'lib/cyberarm_engine/vector.rb', line 7

def self.down
  Vector.new(0, -1, 0)
end

.forwardObject



19
20
21
# File 'lib/cyberarm_engine/vector.rb', line 19

def self.forward
  Vector.new(0, 0, 1)
end

.leftObject



11
12
13
# File 'lib/cyberarm_engine/vector.rb', line 11

def self.left
  Vector.new(-1, 0, 0)
end

.rightObject



15
16
17
# File 'lib/cyberarm_engine/vector.rb', line 15

def self.right
  Vector.new(1, 0, 0)
end

.upObject



3
4
5
# File 'lib/cyberarm_engine/vector.rb', line 3

def self.up
  Vector.new(0, 1, 0)
end

Instance Method Details

#*(other) ⇒ Object

Multiplies Vector and Numberic or Vector and Vector, excluding @weight



88
89
90
# File 'lib/cyberarm_engine/vector.rb', line 88

def *(other)
  operator("*", other)
end

#+(other) ⇒ Object

Adds Vector and Numberic or Vector and Vector, excluding @weight



78
79
80
# File 'lib/cyberarm_engine/vector.rb', line 78

def +(other)
  operator("+", other)
end

#-(other) ⇒ Object

Subtracts Vector and Numberic or Vector and Vector, excluding @weight



83
84
85
# File 'lib/cyberarm_engine/vector.rb', line 83

def -(other)
  operator("-", other)
end

#/(other) ⇒ Object

Divides Vector and Numberic or Vector and Vector, excluding @weight



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cyberarm_engine/vector.rb', line 93

def /(other)
  # Duplicated to protect from DivideByZero
  if other.is_a?(Numeric)
    Vector.new(
      (@x == 0 ? 0 : @x / other),
      (@y == 0 ? 0 : @y / other),
      (@z == 0 ? 0 : @z / other)
    )
  else
    Vector.new(
      (@x == 0 ? 0 : @x / other.x),
      (@y == 0 ? 0 : @y / other.y),
      (@z == 0 ? 0 : @z / other.z)
    )
  end
end

#==(other) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cyberarm_engine/vector.rb', line 46

def ==(other)
  if other.is_a?(Numeric)
    @x      == other &&
    @y      == other &&
    @z      == other &&
    @weight == other
  else
    @x      == other.x &&
    @y      == other.y &&
    @z      == other.z &&
    @weight == other.weight
  end
end

#angle(other) ⇒ Object

returns degrees



135
136
137
# File 'lib/cyberarm_engine/vector.rb', line 135

def angle(other)
  Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
end

#cross(other) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/cyberarm_engine/vector.rb', line 123

def cross(other)
  a = self.to_a
  b = other.to_a

  Vector.new(
    b[2] * a[1] - b[1] * a[2],
    b[0] * a[2] - b[2] * a[0],
    b[1] * a[0] - b[0] * a[1]
  )
end

#directionObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/cyberarm_engine/vector.rb', line 149

def direction
  # z is pitch
  # y is yaw
  # x is roll
  _x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
  _y = Math.sin(@z.degrees_to_radians)
  _z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)

  Vector.new(_x, _y, _z)
end

#distance(other) ⇒ Object

2D distance using X and Y



173
174
175
# File 'lib/cyberarm_engine/vector.rb', line 173

def distance(other)
  Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
end

#distance3d(other) ⇒ Object

3D distance using X, Y, and Z



183
184
185
# File 'lib/cyberarm_engine/vector.rb', line 183

def distance3d(other)
  Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
end

#dot(other) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cyberarm_engine/vector.rb', line 110

def dot(other)
  product = 0

  a = self.to_a
  b = other.to_a

  3.times do |i|
    product = product + (a[i] * b[i])
  end

  return product
end

#gl_distance2d(other) ⇒ Object

2D distance using X and Z



178
179
180
# File 'lib/cyberarm_engine/vector.rb', line 178

def gl_distance2d(other)
  Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
end

#inverseObject



160
161
162
# File 'lib/cyberarm_engine/vector.rb', line 160

def inverse
  Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
end

#lerp(other, factor) ⇒ Object



168
169
170
# File 'lib/cyberarm_engine/vector.rb', line 168

def lerp(other, factor)
  (self - other) * factor.clamp(0.0, 1.0)
end

#magnitudeObject

returns magnitude of Vector, ignoring #weight



140
141
142
# File 'lib/cyberarm_engine/vector.rb', line 140

def magnitude
  Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
end

#normalizedObject



144
145
146
147
# File 'lib/cyberarm_engine/vector.rb', line 144

def normalized
  mag = magnitude
  self / Vector.new(mag, mag, mag)
end

#sumObject



164
165
166
# File 'lib/cyberarm_engine/vector.rb', line 164

def sum
  @x + @y + @z
end

#to_aObject



187
188
189
# File 'lib/cyberarm_engine/vector.rb', line 187

def to_a
  [@x, @y, @z, @weight]
end

#to_hObject



195
196
197
# File 'lib/cyberarm_engine/vector.rb', line 195

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

#to_sObject



191
192
193
# File 'lib/cyberarm_engine/vector.rb', line 191

def to_s
  "X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
end

#weightObject Also known as: w



40
# File 'lib/cyberarm_engine/vector.rb', line 40

def weight; @weight; end

#weight=(n) ⇒ Object Also known as: w=



41
# File 'lib/cyberarm_engine/vector.rb', line 41

def weight=(n); @weight = n; end

#xObject



31
# File 'lib/cyberarm_engine/vector.rb', line 31

def x; @x; end

#x=(n) ⇒ Object



32
# File 'lib/cyberarm_engine/vector.rb', line 32

def x=(n); @x = n; end

#yObject



34
# File 'lib/cyberarm_engine/vector.rb', line 34

def y; @y; end

#y=(n) ⇒ Object



35
# File 'lib/cyberarm_engine/vector.rb', line 35

def y=(n); @y = n; end

#zObject



37
# File 'lib/cyberarm_engine/vector.rb', line 37

def z; @z; end

#z=(n) ⇒ Object



38
# File 'lib/cyberarm_engine/vector.rb', line 38

def z=(n); @z = n; end