mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-18 05:52:34 +00:00
Add documentation for Vector
This commit is contained in:
@@ -1,25 +1,61 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Vector
|
class Vector
|
||||||
|
##
|
||||||
|
# Creates a up vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, 1, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def self.up
|
def self.up
|
||||||
Vector.new(0, 1, 0)
|
Vector.new(0, 1, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a down vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, -1, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def self.down
|
def self.down
|
||||||
Vector.new(0, -1, 0)
|
Vector.new(0, -1, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a left vector
|
||||||
|
#
|
||||||
|
# Vector.new(-1, 0, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def self.left
|
def self.left
|
||||||
Vector.new(-1, 0, 0)
|
Vector.new(-1, 0, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a right vector
|
||||||
|
#
|
||||||
|
# Vector.new(1, 0, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def self.right
|
def self.right
|
||||||
Vector.new(1, 0, 0)
|
Vector.new(1, 0, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a forward vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, 0, 1)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def self.forward
|
def self.forward
|
||||||
Vector.new(0, 0, 1)
|
Vector.new(0, 0, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a backward vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, 0, -1)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def self.backward
|
def self.backward
|
||||||
Vector.new(0, 0, -1)
|
Vector.new(0, 0, -1)
|
||||||
end
|
end
|
||||||
@@ -43,6 +79,7 @@ module CyberarmEngine
|
|||||||
alias w weight
|
alias w weight
|
||||||
alias w= weight=
|
alias w= weight=
|
||||||
|
|
||||||
|
# @return [Boolean]
|
||||||
def ==(other)
|
def ==(other)
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@x == other &&
|
@x == other &&
|
||||||
@@ -57,11 +94,13 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Create a new vector using {x} and {y} values
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def xy
|
def xy
|
||||||
Vector.new(@x, @y)
|
Vector.new(@x, @y)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Performs math operation, excluding @weight
|
# Performs math operation, excluding {weight}
|
||||||
private def operator(function, other)
|
private def operator(function, other)
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
Vector.new(
|
Vector.new(
|
||||||
@@ -78,22 +117,26 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds Vector and Numberic or Vector and Vector, excluding @weight
|
# Adds Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def +(other)
|
def +(other)
|
||||||
operator("+", other)
|
operator("+", other)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Subtracts Vector and Numberic or Vector and Vector, excluding @weight
|
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def -(other)
|
def -(other)
|
||||||
operator("-", other)
|
operator("-", other)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Multiplies Vector and Numberic or Vector and Vector, excluding @weight
|
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def *(other)
|
def *(other)
|
||||||
operator("*", other)
|
operator("*", other)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Divides Vector and Numberic or Vector and Vector, excluding @weight
|
# Divides Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def /(other)
|
def /(other)
|
||||||
# Duplicated to protect from DivideByZero
|
# Duplicated to protect from DivideByZero
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@@ -111,6 +154,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# dot product of {Vector}
|
||||||
|
# @return [Integer|Float]
|
||||||
def dot(other)
|
def dot(other)
|
||||||
product = 0
|
product = 0
|
||||||
|
|
||||||
@@ -124,6 +169,8 @@ module CyberarmEngine
|
|||||||
return product
|
return product
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# cross product of {Vector}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def cross(other)
|
def cross(other)
|
||||||
a = self.to_a
|
a = self.to_a
|
||||||
b = other.to_a
|
b = other.to_a
|
||||||
@@ -136,24 +183,40 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# returns degrees
|
# returns degrees
|
||||||
|
# @return [Float]
|
||||||
def angle(other)
|
def angle(other)
|
||||||
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns magnitude of Vector, ignoring #weight
|
# returns magnitude of Vector, ignoring #weight
|
||||||
|
# @return [Float]
|
||||||
def magnitude
|
def magnitude
|
||||||
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# 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]
|
||||||
def normalized
|
def normalized
|
||||||
mag = magnitude
|
mag = magnitude
|
||||||
self / Vector.new(mag, mag, mag)
|
self / Vector.new(mag, mag, mag)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# returns a direction {Vector}
|
||||||
|
#
|
||||||
|
# z is pitch
|
||||||
|
#
|
||||||
|
# y is yaw
|
||||||
|
#
|
||||||
|
# x is roll
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def direction
|
def direction
|
||||||
# z is pitch
|
|
||||||
# y is yaw
|
|
||||||
# x is roll
|
|
||||||
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
_y = Math.sin(@z.degrees_to_radians)
|
_y = Math.sin(@z.degrees_to_radians)
|
||||||
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
@@ -161,41 +224,63 @@ module CyberarmEngine
|
|||||||
Vector.new(_x, _y, _z)
|
Vector.new(_x, _y, _z)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# returns an inverse {Vector}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def inverse
|
def inverse
|
||||||
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Adds up values of {x}, {y}, and {z}
|
||||||
|
# @return [Integer|Float]
|
||||||
def sum
|
def sum
|
||||||
@x + @y + @z
|
@x + @y + @z
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# 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]
|
||||||
def lerp(other, factor)
|
def lerp(other, factor)
|
||||||
(self - other) * factor.clamp(0.0, 1.0)
|
(self - other) * factor.clamp(0.0, 1.0)
|
||||||
end
|
end
|
||||||
|
|
||||||
# 2D distance using X and Y
|
# 2D distance using X and Y
|
||||||
|
# @return [Float]
|
||||||
def distance(other)
|
def distance(other)
|
||||||
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
||||||
end
|
end
|
||||||
|
|
||||||
# 2D distance using X and Z
|
# 2D distance using X and Z
|
||||||
|
# @return [Float]
|
||||||
def gl_distance2d(other)
|
def gl_distance2d(other)
|
||||||
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
||||||
end
|
end
|
||||||
|
|
||||||
# 3D distance using X, Y, and Z
|
# 3D distance using X, Y, and Z
|
||||||
|
# @return [Float]
|
||||||
def distance3d(other)
|
def distance3d(other)
|
||||||
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts {Vector} to Array
|
||||||
|
# @return [Array]
|
||||||
def to_a
|
def to_a
|
||||||
[@x, @y, @z, @weight]
|
[@x, @y, @z, @weight]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts {Vector} to String
|
||||||
|
# @return [String]
|
||||||
def to_s
|
def to_s
|
||||||
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts {Vector} to Hash
|
||||||
|
# @return [Hash]
|
||||||
def to_h
|
def to_h
|
||||||
{x: @x, y: @y, z: @z, weight: @weight}
|
{x: @x, y: @y, z: @z, weight: @weight}
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user