Class: CyberarmEngine::Vector
- Inherits:
-
Object
- Object
- CyberarmEngine::Vector
- Defined in:
- lib/cyberarm_engine/vector.rb
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.
-
#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.
- #weight ⇒ Object (also: #w)
- #weight=(n) ⇒ Object (also: #w=)
- #x ⇒ Object
- #x=(n) ⇒ Object
- #xy ⇒ CyberarmEngine::Vector
- #y ⇒ Object
- #y=(n) ⇒ Object
- #z ⇒ Object
- #z=(n) ⇒ Object
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
.backward ⇒ CyberarmEngine::Vector
59 60 61 |
# File 'lib/cyberarm_engine/vector.rb', line 59 def self.backward Vector.new(0, 0, -1) end |
.down ⇒ CyberarmEngine::Vector
19 20 21 |
# File 'lib/cyberarm_engine/vector.rb', line 19 def self.down Vector.new(0, -1, 0) end |
.forward ⇒ CyberarmEngine::Vector
49 50 51 |
# File 'lib/cyberarm_engine/vector.rb', line 49 def self.forward Vector.new(0, 0, 1) end |
.left ⇒ CyberarmEngine::Vector
29 30 31 |
# File 'lib/cyberarm_engine/vector.rb', line 29 def self.left Vector.new(-1, 0, 0) end |
.right ⇒ CyberarmEngine::Vector
39 40 41 |
# File 'lib/cyberarm_engine/vector.rb', line 39 def self.right Vector.new(1, 0, 0) end |
.up ⇒ CyberarmEngine::Vector
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
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
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 |
#direction ⇒ CyberarmEngine::Vector
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
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
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
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
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 |
#inverse ⇒ CyberarmEngine::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>
250 251 252 |
# File 'lib/cyberarm_engine/vector.rb', line 250 def lerp(other, factor) (self - other) * factor.clamp(0.0, 1.0) end |
#magnitude ⇒ Float
returns magnitude of Vector, ignoring #weight
195 196 197 |
# File 'lib/cyberarm_engine/vector.rb', line 195 def magnitude Math.sqrt((@x * @x) + (@y * @y) + (@z * @z)) end |
#normalized ⇒ CyberarmEngine::Vector
returns normalized CyberarmEngine::Vector
207 208 209 210 |
# File 'lib/cyberarm_engine/vector.rb', line 207 def normalized mag = magnitude self / Vector.new(mag, mag, mag) end |
#sum ⇒ Integer|Float
237 238 239 |
# File 'lib/cyberarm_engine/vector.rb', line 237 def sum @x + @y + @z end |
#to_a ⇒ Array
Converts CyberarmEngine::Vector to Array
274 275 276 |
# File 'lib/cyberarm_engine/vector.rb', line 274 def to_a [@x, @y, @z, @weight] end |
#to_h ⇒ Hash
Converts CyberarmEngine::Vector to 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_s ⇒ String
Converts CyberarmEngine::Vector to String
280 281 282 |
# File 'lib/cyberarm_engine/vector.rb', line 280 def to_s "X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}" end |
#weight ⇒ Object 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 |
#x ⇒ Object
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 |
#xy ⇒ CyberarmEngine::Vector
101 102 103 |
# File 'lib/cyberarm_engine/vector.rb', line 101 def xy Vector.new(@x, @y) end |
#y ⇒ Object
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 |
#z ⇒ Object
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 |