Class: CyberarmEngine::Vector
- Inherits:
-
Object
- Object
- CyberarmEngine::Vector
- Defined in:
- lib/cyberarm_engine/vector.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiplies Vector and Numberic or Vector and Vector, excluding @weight.
-
#+(other) ⇒ Object
Adds Vector and Numberic or Vector and Vector, excluding @weight.
-
#-(other) ⇒ Object
Subtracts Vector and Numberic or Vector and Vector, excluding @weight.
-
#/(other) ⇒ Object
Divides Vector and Numberic or Vector and Vector, excluding @weight.
- #==(other) ⇒ Object
-
#angle(other) ⇒ Object
returns degrees.
- #cross(other) ⇒ Object
- #direction ⇒ Object
-
#distance(other) ⇒ Object
2D distance using X and Y.
-
#distance3d(other) ⇒ Object
3D distance using X, Y, and Z.
- #dot(other) ⇒ Object
-
#gl_distance2d(other) ⇒ Object
2D distance using X and Z.
-
#initialize(x = 0, y = 0, z = 0, weight = 0) ⇒ Vector
constructor
A new instance of Vector.
- #inverse ⇒ Object
- #lerp(other, factor) ⇒ Object
-
#magnitude ⇒ Object
returns magnitude of Vector, ignoring #weight.
- #normalized ⇒ Object
- #sum ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #weight ⇒ Object (also: #w)
- #weight=(n) ⇒ Object (also: #w=)
- #x ⇒ Object
- #x=(n) ⇒ Object
- #xy ⇒ Object
- #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.
27 28 29 |
# File 'lib/cyberarm_engine/vector.rb', line 27 def initialize(x = 0, y = 0, z = 0, weight = 0) @x, @y, @z, @weight = x, y, z, weight end |
Class Method Details
.backward ⇒ Object
23 24 25 |
# File 'lib/cyberarm_engine/vector.rb', line 23 def self.backward Vector.new(0, 0, -1) end |
.down ⇒ Object
7 8 9 |
# File 'lib/cyberarm_engine/vector.rb', line 7 def self.down Vector.new(0, -1, 0) end |
.forward ⇒ Object
19 20 21 |
# File 'lib/cyberarm_engine/vector.rb', line 19 def self.forward Vector.new(0, 0, 1) end |
.left ⇒ Object
11 12 13 |
# File 'lib/cyberarm_engine/vector.rb', line 11 def self.left Vector.new(-1, 0, 0) end |
.right ⇒ Object
15 16 17 |
# File 'lib/cyberarm_engine/vector.rb', line 15 def self.right Vector.new(1, 0, 0) end |
.up ⇒ Object
3 4 5 |
# File 'lib/cyberarm_engine/vector.rb', line 3 def self.up Vector.new(0, 1, 0) end |
Instance Method Details
#*(other) ⇒ Object
Multiplies Vector and Numberic or Vector and Vector, excluding @weight
92 93 94 |
# File 'lib/cyberarm_engine/vector.rb', line 92 def *(other) operator("*", other) end |
#+(other) ⇒ Object
Adds Vector and Numberic or Vector and Vector, excluding @weight
82 83 84 |
# File 'lib/cyberarm_engine/vector.rb', line 82 def +(other) operator("+", other) end |
#-(other) ⇒ Object
Subtracts Vector and Numberic or Vector and Vector, excluding @weight
87 88 89 |
# File 'lib/cyberarm_engine/vector.rb', line 87 def -(other) operator("-", other) end |
#/(other) ⇒ Object
Divides Vector and Numberic or Vector and Vector, excluding @weight
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cyberarm_engine/vector.rb', line 97 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) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cyberarm_engine/vector.rb', line 46 def ==(other) if other.is_a?(Numeric) @x == other && @y == other && @z == other && @weight == other else @x == other.x && @y == other.y && @z == other.z && @weight == other.weight end end |
#angle(other) ⇒ Object
returns degrees
139 140 141 |
# File 'lib/cyberarm_engine/vector.rb', line 139 def angle(other) Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI end |
#cross(other) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cyberarm_engine/vector.rb', line 127 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 ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/cyberarm_engine/vector.rb', line 153 def direction # z is pitch # y is yaw # x is roll _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) ⇒ Object
2D distance using X and Y
177 178 179 |
# File 'lib/cyberarm_engine/vector.rb', line 177 def distance(other) Math.sqrt((@x-other.x)**2 + (@y-other.y)**2) end |
#distance3d(other) ⇒ Object
3D distance using X, Y, and Z
187 188 189 |
# File 'lib/cyberarm_engine/vector.rb', line 187 def distance3d(other) Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2) end |
#dot(other) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cyberarm_engine/vector.rb', line 114 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) ⇒ Object
2D distance using X and Z
182 183 184 |
# File 'lib/cyberarm_engine/vector.rb', line 182 def gl_distance2d(other) Math.sqrt((@x-other.x)**2 + (@z-other.z)**2) end |
#inverse ⇒ Object
164 165 166 |
# File 'lib/cyberarm_engine/vector.rb', line 164 def inverse Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z) end |
#lerp(other, factor) ⇒ Object
172 173 174 |
# File 'lib/cyberarm_engine/vector.rb', line 172 def lerp(other, factor) (self - other) * factor.clamp(0.0, 1.0) end |
#magnitude ⇒ Object
returns magnitude of Vector, ignoring #weight
144 145 146 |
# File 'lib/cyberarm_engine/vector.rb', line 144 def magnitude Math.sqrt((@x * @x) + (@y * @y) + (@z * @z)) end |
#normalized ⇒ Object
148 149 150 151 |
# File 'lib/cyberarm_engine/vector.rb', line 148 def normalized mag = magnitude self / Vector.new(mag, mag, mag) end |
#sum ⇒ Object
168 169 170 |
# File 'lib/cyberarm_engine/vector.rb', line 168 def sum @x + @y + @z end |
#to_a ⇒ Object
191 192 193 |
# File 'lib/cyberarm_engine/vector.rb', line 191 def to_a [@x, @y, @z, @weight] end |
#to_h ⇒ Object
199 200 201 |
# File 'lib/cyberarm_engine/vector.rb', line 199 def to_h {x: @x, y: @y, z: @z, weight: @weight} end |
#to_s ⇒ Object
195 196 197 |
# File 'lib/cyberarm_engine/vector.rb', line 195 def to_s "X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}" end |
#weight ⇒ Object Also known as: w
40 |
# File 'lib/cyberarm_engine/vector.rb', line 40 def weight; @weight; end |
#weight=(n) ⇒ Object Also known as: w=
41 |
# File 'lib/cyberarm_engine/vector.rb', line 41 def weight=(n); @weight = n; end |
#x ⇒ Object
31 |
# File 'lib/cyberarm_engine/vector.rb', line 31 def x; @x; end |
#x=(n) ⇒ Object
32 |
# File 'lib/cyberarm_engine/vector.rb', line 32 def x=(n); @x = n; end |
#xy ⇒ Object
60 61 62 |
# File 'lib/cyberarm_engine/vector.rb', line 60 def xy Vector.new(@x, @y) end |
#y ⇒ Object
34 |
# File 'lib/cyberarm_engine/vector.rb', line 34 def y; @y; end |
#y=(n) ⇒ Object
35 |
# File 'lib/cyberarm_engine/vector.rb', line 35 def y=(n); @y = n; end |
#z ⇒ Object
37 |
# File 'lib/cyberarm_engine/vector.rb', line 37 def z; @z; end |
#z=(n) ⇒ Object
38 |
# File 'lib/cyberarm_engine/vector.rb', line 38 def z=(n); @z = n; end |