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.



63
64
65
# File 'lib/cyberarm_engine/vector.rb', line 63

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

Class Method Details

.backwardCyberarmEngine::Vector

Creates a backward vector

Vector.new(0, 0, -1)


59
60
61
# File 'lib/cyberarm_engine/vector.rb', line 59

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

.downCyberarmEngine::Vector

Creates a down vector

Vector.new(0, -1, 0)


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

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

.forwardCyberarmEngine::Vector

Creates a forward vector

Vector.new(0, 0, 1)


49
50
51
# File 'lib/cyberarm_engine/vector.rb', line 49

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

.leftCyberarmEngine::Vector

Creates a left vector

Vector.new(-1, 0, 0)


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

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

.rightCyberarmEngine::Vector

Creates a right vector

Vector.new(1, 0, 0)


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

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

.upCyberarmEngine::Vector

Creates a up vector

Vector.new(0, 1, 0)


9
10
11
# File 'lib/cyberarm_engine/vector.rb', line 9

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

Instance Method Details

#*(other) ⇒ CyberarmEngine::Vector

Multiplies Vector and Numeric or Vector and Vector, excluding #weight



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

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

#+(other) ⇒ CyberarmEngine::Vector

Adds Vector and Numeric or Vector and Vector, excluding #weight



124
125
126
# File 'lib/cyberarm_engine/vector.rb', line 124

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

#-(other) ⇒ CyberarmEngine::Vector

Subtracts Vector and Numeric or Vector and Vector, excluding #weight



130
131
132
# File 'lib/cyberarm_engine/vector.rb', line 130

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

#/(other) ⇒ CyberarmEngine::Vector

Divides Vector and Numeric or Vector and Vector, excluding #weight



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cyberarm_engine/vector.rb', line 142

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) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cyberarm_engine/vector.rb', line 83

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

#angle(other) ⇒ Float

returns degrees

Returns:

  • (Float)


189
190
191
# File 'lib/cyberarm_engine/vector.rb', line 189

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

#cross(other) ⇒ CyberarmEngine::Vector

cross product of CyberarmEngine::Vector



176
177
178
179
180
181
182
183
184
185
# File 'lib/cyberarm_engine/vector.rb', line 176

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

#directionCyberarmEngine::Vector

returns a direction CyberarmEngine::Vector

z is pitch

y is yaw

x is roll



221
222
223
224
225
226
227
# File 'lib/cyberarm_engine/vector.rb', line 221

def direction
  _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) ⇒ Float

2D distance using X and Y

Returns:

  • (Float)


256
257
258
# File 'lib/cyberarm_engine/vector.rb', line 256

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

#distance3d(other) ⇒ Float

3D distance using X, Y, and Z

Returns:

  • (Float)


268
269
270
# File 'lib/cyberarm_engine/vector.rb', line 268

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

#dot(other) ⇒ Integer|Float

dot product of CyberarmEngine::Vector

Returns:

  • (Integer|Float)


161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cyberarm_engine/vector.rb', line 161

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) ⇒ Float

2D distance using X and Z

Returns:

  • (Float)


262
263
264
# File 'lib/cyberarm_engine/vector.rb', line 262

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

#inverseCyberarmEngine::Vector

returns an inverse CyberarmEngine::Vector



231
232
233
# File 'lib/cyberarm_engine/vector.rb', line 231

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

#lerp(other, factor) ⇒ CyberarmEngine::Vector

Linear interpolation: smoothly transition between two CyberarmEngine::Vector

CyberarmEngine::Vector.new(100, 100, 100).lerp( CyberarmEngine::Vector.new(0, 0, 0), 0.75 )
# => <CyberarmEngine::Vector:0x0001 @x=75.0, @y=75.0, @z=75.0, @weight=0>

Parameters:

  • other (CyberarmEngine::Vector | Integer | Float)

    value to subtract from

  • factor (Float)

    how complete transition to other is, in range [0.0..1.0]

Returns:



250
251
252
# File 'lib/cyberarm_engine/vector.rb', line 250

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

#magnitudeFloat

returns magnitude of Vector, ignoring #weight

Returns:

  • (Float)


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

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

#normalizedCyberarmEngine::Vector

returns normalized CyberarmEngine::Vector

Examples:

CyberarmEngine::Vector.new(50, 21.2, 45).normalized
# => <CyberarmEngine::Vector:0x001 @x=0.7089... @y=0.3005... @z=0.6380... @weight=0>

Returns:



207
208
209
210
# File 'lib/cyberarm_engine/vector.rb', line 207

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

#sumInteger|Float

Adds up values of #x, #y, and #z

Returns:

  • (Integer|Float)


237
238
239
# File 'lib/cyberarm_engine/vector.rb', line 237

def sum
  @x + @y + @z
end

#to_aArray

Converts CyberarmEngine::Vector to Array

Returns:

  • (Array)


274
275
276
# File 'lib/cyberarm_engine/vector.rb', line 274

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

#to_hHash

Converts CyberarmEngine::Vector to Hash

Returns:

  • (Hash)


286
287
288
# File 'lib/cyberarm_engine/vector.rb', line 286

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

#to_sString

Converts CyberarmEngine::Vector to String

Returns:

  • (String)


280
281
282
# File 'lib/cyberarm_engine/vector.rb', line 280

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

#weightObject Also known as: w



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

def weight; @weight; end

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



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

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

#xObject



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

def x; @x; end

#x=(n) ⇒ Object



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

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

#xyCyberarmEngine::Vector

Create a new vector using #x and #y values



101
102
103
# File 'lib/cyberarm_engine/vector.rb', line 101

def xy
  Vector.new(@x, @y)
end

#yObject



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

def y; @y; end

#y=(n) ⇒ Object



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

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

#zObject



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

def z; @z; end

#z=(n) ⇒ Object



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

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