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
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 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
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
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 |
#direction ⇒ CyberarmEngine::Vector
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
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
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
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
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 |
#inverse ⇒ CyberarmEngine::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>
272 273 274 |
# File 'lib/cyberarm_engine/vector.rb', line 272 def lerp(other, factor) (self - other) * factor.clamp(0.0, 1.0) end |
#magnitude ⇒ Float
returns magnitude of Vector, ignoring #weight
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 |
#normalized ⇒ CyberarmEngine::Vector
returns normalized CyberarmEngine::Vector
230 231 232 233 |
# File 'lib/cyberarm_engine/vector.rb', line 230 def normalized mag = magnitude self / Vector.new(mag, mag, mag) end |
#sum ⇒ Integer|Float
259 260 261 |
# File 'lib/cyberarm_engine/vector.rb', line 259 def sum @x + @y + @z end |
#to_a ⇒ Array
Converts CyberarmEngine::Vector to Array
296 297 298 |
# File 'lib/cyberarm_engine/vector.rb', line 296 def to_a [@x, @y, @z, @weight] end |
#to_h ⇒ Hash
Converts CyberarmEngine::Vector to 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_s ⇒ String
Converts CyberarmEngine::Vector to String
302 303 304 |
# File 'lib/cyberarm_engine/vector.rb', line 302 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 |