Class: CyberarmEngine::Vector

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

Instance Attribute Summary collapse

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.



65
66
67
68
69
70
# File 'lib/cyberarm_engine/vector.rb', line 65

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

Instance Attribute Details

#weightObject Also known as: w

Returns the value of attribute weight.



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

def weight
  @weight
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

#zObject

Returns the value of attribute z.



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

def z
  @z
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
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cyberarm_engine/vector.rb', line 136

def *(other)
  if other.is_a?(Numeric)
    Vector.new(
      @x * other,
      @y * other,
      @z * other
    )
  else
    Vector.new(
      @x * other.x,
      @y * other.y,
      @z * other.z
    )
  end
end

#+(other) ⇒ CyberarmEngine::Vector

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cyberarm_engine/vector.rb', line 100

def +(other)
  if other.is_a?(Numeric)
    Vector.new(
      @x + other,
      @y + other,
      @z + other
    )
  else
    Vector.new(
      @x + other.x,
      @y + other.y,
      @z + other.z
    )
  end
end

#-(other) ⇒ CyberarmEngine::Vector

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



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

def -(other)
  if other.is_a?(Numeric)
    Vector.new(
      @x - other,
      @y - other,
      @z - other
    )
  else
    Vector.new(
      @x - other.x,
      @y - other.y,
      @z - other.z
    )
  end
end

#/(other) ⇒ CyberarmEngine::Vector

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cyberarm_engine/vector.rb', line 165

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)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cyberarm_engine/vector.rb', line 76

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)


212
213
214
# File 'lib/cyberarm_engine/vector.rb', line 212

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

#cross(other) ⇒ CyberarmEngine::Vector

cross product of CyberarmEngine::Vector



199
200
201
202
203
204
205
206
207
208
# File 'lib/cyberarm_engine/vector.rb', line 199

def cross(other)
  a = 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



243
244
245
246
247
248
249
# File 'lib/cyberarm_engine/vector.rb', line 243

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)


278
279
280
# File 'lib/cyberarm_engine/vector.rb', line 278

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)


290
291
292
# File 'lib/cyberarm_engine/vector.rb', line 290

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)


184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cyberarm_engine/vector.rb', line 184

def dot(other)
  product = 0

  a = to_a
  b = other.to_a

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

  product
end

#gl_distance2d(other) ⇒ Float

2D distance using X and Z

Returns:

  • (Float)


284
285
286
# File 'lib/cyberarm_engine/vector.rb', line 284

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

#inverseCyberarmEngine::Vector

returns an inverse CyberarmEngine::Vector



253
254
255
# File 'lib/cyberarm_engine/vector.rb', line 253

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:



272
273
274
# File 'lib/cyberarm_engine/vector.rb', line 272

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

#magnitudeFloat

returns magnitude of Vector, ignoring #weight

Returns:

  • (Float)


218
219
220
# File 'lib/cyberarm_engine/vector.rb', line 218

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

#multiply_transform(transform) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/cyberarm_engine/vector.rb', line 152

def multiply_transform(transform)
  e = transform.elements

  x = @x * e[0]  + @y * e[1]  + @z * e[2]  + 1 * e[3]
  y = @x * e[4]  + @y * e[5]  + @z * e[6]  + 1 * e[7]
  z = @x * e[8]  + @y * e[9]  + @z * e[10] + 1 * e[11]
  w = @x * e[12] + @y * e[13] + @z * e[14] + 1 * e[15]

  Vector.new(x / 1, y / 1, z / 1, w / 1)
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:



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

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

#sumInteger|Float

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

Returns:

  • (Integer|Float)


259
260
261
# File 'lib/cyberarm_engine/vector.rb', line 259

def sum
  @x + @y + @z
end

#to_aArray

Converts CyberarmEngine::Vector to Array

Returns:

  • (Array)


296
297
298
# File 'lib/cyberarm_engine/vector.rb', line 296

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

#to_hHash

Converts CyberarmEngine::Vector to Hash

Returns:

  • (Hash)


308
309
310
# File 'lib/cyberarm_engine/vector.rb', line 308

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

#to_sString

Converts CyberarmEngine::Vector to String

Returns:

  • (String)


302
303
304
# File 'lib/cyberarm_engine/vector.rb', line 302

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

#xyCyberarmEngine::Vector

Create a new vector using #x and #y values



94
95
96
# File 'lib/cyberarm_engine/vector.rb', line 94

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