class CyberarmEngine::Vector

Attributes

w[RW]
w=[RW]
weight[RW]
x[RW]
y[RW]
z[RW]

Public Class Methods

backward() click to toggle source

Creates a backward vector

Vector.new(0, 0, -1)

@return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 59
def self.backward
  Vector.new(0, 0, -1)
end
down() click to toggle source

Creates a down vector

Vector.new(0, -1, 0)

@return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 19
def self.down
  Vector.new(0, -1, 0)
end
forward() click to toggle source

Creates a forward vector

Vector.new(0, 0, 1)

@return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 49
def self.forward
  Vector.new(0, 0, 1)
end
left() click to toggle source

Creates a left vector

Vector.new(-1, 0, 0)

@return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 29
def self.left
  Vector.new(-1, 0, 0)
end
new(x = 0, y = 0, z = 0, weight = 0) click to toggle source
# 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
right() click to toggle source

Creates a right vector

Vector.new(1, 0, 0)

@return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 39
def self.right
  Vector.new(1, 0, 0)
end
up() click to toggle source

Creates a up vector

Vector.new(0, 1, 0)

@return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 9
def self.up
  Vector.new(0, 1, 0)
end

Public Instance Methods

*(other) click to toggle source

Multiplies Vector and Numeric or Vector and Vector, excluding {weight} @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 129
def *(other)
  operator("*", other)
end
+(other) click to toggle source

Adds Vector and Numeric or Vector and Vector, excluding {weight} @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 117
def +(other)
  operator("+", other)
end
-(other) click to toggle source

Subtracts Vector and Numeric or Vector and Vector, excluding {weight} @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 123
def -(other)
  operator("-", other)
end
/(other) click to toggle source

Divides Vector and Numeric or Vector and Vector, excluding {weight} @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 146
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) click to toggle source

@return [Boolean]

# 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) click to toggle source

returns degrees @return [Float]

# File lib/cyberarm_engine/vector.rb, line 193
def angle(other)
  Math.acos(normalized.dot(other.normalized)) * 180 / Math::PI
end
cross(other) click to toggle source

cross product of {Vector} @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 180
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
direction() click to toggle source

returns a direction {Vector}

z is pitch

y is yaw

x is roll @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 224
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) click to toggle source

2D distance using X and Y @return [Float]

# File lib/cyberarm_engine/vector.rb, line 259
def distance(other)
  Math.sqrt((@x - other.x)**2 + (@y - other.y)**2)
end
distance3d(other) click to toggle source

3D distance using X, Y, and Z @return [Float]

# File lib/cyberarm_engine/vector.rb, line 271
def distance3d(other)
  Math.sqrt((@x - other.x)**2 + (@y - other.y)**2 + (@z - other.z)**2)
end
dot(other) click to toggle source

dot product of {Vector} @return [Integer|Float]

# File lib/cyberarm_engine/vector.rb, line 165
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) click to toggle source

2D distance using X and Z @return [Float]

# File lib/cyberarm_engine/vector.rb, line 265
def gl_distance2d(other)
  Math.sqrt((@x - other.x)**2 + (@z - other.z)**2)
end
inverse() click to toggle source

returns an inverse {Vector} @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 234
def inverse
  Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
end
lerp(other, factor) click to toggle source

Linear interpolation: smoothly transition between two {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>

@param other [CyberarmEngine::Vector | Integer | Float] value to subtract from @param factor [Float] how complete transition to other is, in range [0.0..1.0] @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 253
def lerp(other, factor)
  (self - other) * factor.clamp(0.0, 1.0)
end
magnitude() click to toggle source

returns magnitude of Vector, ignoring weight @return [Float]

# File lib/cyberarm_engine/vector.rb, line 199
def magnitude
  Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
end
multiply_transform(transform) click to toggle source
# File lib/cyberarm_engine/vector.rb, line 133
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
normalized() click to toggle source

returns normalized {Vector}

@example

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

@return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 211
def normalized
  mag = magnitude
  self / Vector.new(mag, mag, mag)
end
sum() click to toggle source

Adds up values of {x}, {y}, and {z} @return [Integer|Float]

# File lib/cyberarm_engine/vector.rb, line 240
def sum
  @x + @y + @z
end
to_a() click to toggle source

Converts {Vector} to Array @return [Array]

# File lib/cyberarm_engine/vector.rb, line 277
def to_a
  [@x, @y, @z, @weight]
end
to_h() click to toggle source

Converts {Vector} to Hash @return [Hash]

# File lib/cyberarm_engine/vector.rb, line 289
def to_h
  { x: @x, y: @y, z: @z, weight: @weight }
end
to_s() click to toggle source

Converts {Vector} to String @return [String]

# File lib/cyberarm_engine/vector.rb, line 283
def to_s
  "X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
end
xy() click to toggle source

Create a new vector using {x} and {y} values @return [CyberarmEngine::Vector]

# File lib/cyberarm_engine/vector.rb, line 94
def xy
  Vector.new(@x, @y)
end

Private Instance Methods

operator(function, other) click to toggle source

Performs math operation, excluding {weight}

# File lib/cyberarm_engine/vector.rb, line 99
        def operator(function, other)
  if other.is_a?(Numeric)
    Vector.new(
      @x.send(:"#{function}", other),
      @y.send(:"#{function}", other),
      @z.send(:"#{function}", other)
    )
  else
    Vector.new(
      @x.send(:"#{function}", other.x),
      @y.send(:"#{function}", other.y),
      @z.send(:"#{function}", other.z)
    )
  end
end