Class: CyberarmEngine::Vector
- Inherits:
-
Object
- Object
- CyberarmEngine::Vector
- Defined in:
- lib/cyberarm_engine/vector.rb
Instance Attribute Summary collapse
-
#weight ⇒ Object
(also: #w)
Returns the value of attribute weight.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Class Method Summary collapse
-
.backward ⇒ CyberarmEngine::Vector
Creates a backward vector.
-
.down ⇒ CyberarmEngine::Vector
Creates a down vector.
-
.forward ⇒ CyberarmEngine::Vector
Creates a forward vector.
-
.left ⇒ CyberarmEngine::Vector
Creates a left vector.
-
.right ⇒ CyberarmEngine::Vector
Creates a right vector.
-
.up ⇒ CyberarmEngine::Vector
Creates a up vector.
Instance Method Summary collapse
-
#*(other) ⇒ CyberarmEngine::Vector
Multiplies Vector and Numeric or Vector and Vector, excluding #weight.
-
#+(other) ⇒ CyberarmEngine::Vector
Adds Vector and Numeric or Vector and Vector, excluding #weight.
-
#-(other) ⇒ CyberarmEngine::Vector
Subtracts Vector and Numeric or Vector and Vector, excluding #weight.
-
#/(other) ⇒ CyberarmEngine::Vector
Divides Vector and Numeric or Vector and Vector, excluding #weight.
- #==(other) ⇒ Boolean
-
#angle(other) ⇒ Float
returns degrees.
-
#cross(other) ⇒ CyberarmEngine::Vector
cross product of Vector.
-
#direction ⇒ CyberarmEngine::Vector
returns a direction Vector.
-
#distance(other) ⇒ Float
2D distance using X and Y.
-
#distance3d(other) ⇒ Float
3D distance using X, Y, and Z.
-
#dot(other) ⇒ Integer|Float
dot product of Vector.
-
#gl_distance2d(other) ⇒ Float
2D distance using X and Z.
-
#initialize(x = 0, y = 0, z = 0, weight = 0) ⇒ Vector
constructor
A new instance of Vector.
-
#inverse ⇒ CyberarmEngine::Vector
returns an inverse Vector.
-
#lerp(other, factor) ⇒ CyberarmEngine::Vector
Linear interpolation: smoothly transition between two Vector.
-
#magnitude ⇒ Float
returns magnitude of Vector, ignoring #weight.
- #multiply_transform(transform) ⇒ Object
-
#normalized ⇒ CyberarmEngine::Vector
returns normalized Vector.
- #sum ⇒ Integer|Float
-
#to_a ⇒ Array
Converts Vector to Array.
-
#to_h ⇒ Hash
Converts Vector to Hash.
-
#to_s ⇒ String
Converts Vector to String.
- #xy ⇒ CyberarmEngine::Vector
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
#weight ⇒ Object 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 |
#x ⇒ Object
Returns the value of attribute x.
63 64 65 |
# File 'lib/cyberarm_engine/vector.rb', line 63 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
63 64 65 |
# File 'lib/cyberarm_engine/vector.rb', line 63 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
63 64 65 |
# File 'lib/cyberarm_engine/vector.rb', line 63 def z @z end |
Class Method Details
.backward ⇒ CyberarmEngine::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 |
.down ⇒ CyberarmEngine::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 |
.forward ⇒ CyberarmEngine::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 |
.left ⇒ CyberarmEngine::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 |
.right ⇒ CyberarmEngine::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 |
.up ⇒ CyberarmEngine::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
129 130 131 |
# File 'lib/cyberarm_engine/vector.rb', line 129 def *(other) operator("*", other) end |
#+(other) ⇒ CyberarmEngine::Vector
Adds Vector and Numeric or Vector and Vector, excluding #weight
117 118 119 |
# File 'lib/cyberarm_engine/vector.rb', line 117 def +(other) operator("+", other) end |
#-(other) ⇒ CyberarmEngine::Vector
Subtracts Vector and Numeric or Vector and Vector, excluding #weight
123 124 125 |
# File 'lib/cyberarm_engine/vector.rb', line 123 def -(other) operator("-", other) end |
#/(other) ⇒ CyberarmEngine::Vector
Divides Vector and Numeric or Vector and Vector, excluding #weight
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# 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) ⇒ 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
193 194 195 |
# File 'lib/cyberarm_engine/vector.rb', line 193 def angle(other) Math.acos(normalized.dot(other.normalized)) * 180 / Math::PI end |
#cross(other) ⇒ CyberarmEngine::Vector
cross product of CyberarmEngine::Vector
180 181 182 183 184 185 186 187 188 189 |
# 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 ⇒ CyberarmEngine::Vector
224 225 226 227 228 229 230 |
# 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) ⇒ Float
2D distance using X and Y
259 260 261 |
# File 'lib/cyberarm_engine/vector.rb', line 259 def distance(other) Math.sqrt((@x - other.x)**2 + (@y - other.y)**2) end |
#distance3d(other) ⇒ Float
3D distance using X, Y, and Z
271 272 273 |
# 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) ⇒ Integer|Float
dot product of CyberarmEngine::Vector
165 166 167 168 169 170 171 172 173 174 175 176 |
# 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) ⇒ Float
2D distance using X and Z
265 266 267 |
# File 'lib/cyberarm_engine/vector.rb', line 265 def gl_distance2d(other) Math.sqrt((@x - other.x)**2 + (@z - other.z)**2) end |
#inverse ⇒ CyberarmEngine::Vector
returns an inverse CyberarmEngine::Vector
234 235 236 |
# 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) ⇒ 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>
253 254 255 |
# File 'lib/cyberarm_engine/vector.rb', line 253 def lerp(other, factor) (self - other) * factor.clamp(0.0, 1.0) end |
#magnitude ⇒ Float
returns magnitude of Vector, ignoring #weight
199 200 201 |
# File 'lib/cyberarm_engine/vector.rb', line 199 def magnitude Math.sqrt((@x * @x) + (@y * @y) + (@z * @z)) end |
#multiply_transform(transform) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# 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 ⇒ CyberarmEngine::Vector
returns normalized CyberarmEngine::Vector
211 212 213 214 |
# File 'lib/cyberarm_engine/vector.rb', line 211 def normalized mag = magnitude self / Vector.new(mag, mag, mag) end |
#sum ⇒ Integer|Float
240 241 242 |
# File 'lib/cyberarm_engine/vector.rb', line 240 def sum @x + @y + @z end |
#to_a ⇒ Array
Converts CyberarmEngine::Vector to Array
277 278 279 |
# File 'lib/cyberarm_engine/vector.rb', line 277 def to_a [@x, @y, @z, @weight] end |
#to_h ⇒ Hash
Converts CyberarmEngine::Vector to Hash
289 290 291 |
# File 'lib/cyberarm_engine/vector.rb', line 289 def to_h { x: @x, y: @y, z: @z, weight: @weight } end |
#to_s ⇒ String
Converts CyberarmEngine::Vector to String
283 284 285 |
# File 'lib/cyberarm_engine/vector.rb', line 283 def to_s "X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}" end |
#xy ⇒ CyberarmEngine::Vector
94 95 96 |
# File 'lib/cyberarm_engine/vector.rb', line 94 def xy Vector.new(@x, @y) end |