lib/cyberarm_engine/vector.rb in cyberarm_engine-0.14.0 vs lib/cyberarm_engine/vector.rb in cyberarm_engine-0.15.0

- old
+ new

@@ -58,41 +58,34 @@ # @return [CyberarmEngine::Vector] def self.backward Vector.new(0, 0, -1) end + attr_accessor :x, :y, :z, :weight + def initialize(x = 0, y = 0, z = 0, weight = 0) - @x, @y, @z, @weight = x, y, z, weight + @x = x + @y = y + @z = z + @weight = weight end - def x; @x; end - def x=(n); @x = n; end - - def y; @y; end - def y=(n); @y = n; end - - def z; @z; end - def z=(n); @z = n; end - - def weight; @weight; end - def weight=(n); @weight = n; end - alias w weight alias w= weight= # @return [Boolean] def ==(other) if other.is_a?(Numeric) @x == other && - @y == other && - @z == other && - @weight == other + @y == other && + @z == other && + @weight == other elsif other.is_a?(Vector) @x == other.x && - @y == other.y && - @z == other.z && - @weight == other.weight + @y == other.y && + @z == other.z && + @weight == other.weight else other == self end end @@ -135,10 +128,21 @@ # @return [CyberarmEngine::Vector] def *(other) operator("*", other) end + 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 + # Divides Vector and Numeric or Vector and Vector, excluding {weight} # @return [CyberarmEngine::Vector] def /(other) # Duplicated to protect from DivideByZero if other.is_a?(Numeric) @@ -159,24 +163,24 @@ # dot product of {Vector} # @return [Integer|Float] def dot(other) product = 0 - a = self.to_a + a = to_a b = other.to_a 3.times do |i| - product = product + (a[i] * b[i]) + product += (a[i] * b[i]) end - return product + product end # cross product of {Vector} # @return [CyberarmEngine::Vector] def cross(other) - a = self.to_a + 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], @@ -185,11 +189,11 @@ end # returns degrees # @return [Float] def angle(other) - Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI + Math.acos(normalized.dot(other.normalized)) * 180 / Math::PI end # returns magnitude of Vector, ignoring #weight # @return [Float] def magnitude @@ -207,11 +211,10 @@ def normalized mag = magnitude self / Vector.new(mag, mag, mag) end - # returns a direction {Vector} # # z is pitch # # y is yaw @@ -252,23 +255,23 @@ end # 2D distance using X and Y # @return [Float] def distance(other) - Math.sqrt((@x-other.x)**2 + (@y-other.y)**2) + Math.sqrt((@x - other.x)**2 + (@y - other.y)**2) end # 2D distance using X and Z # @return [Float] 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 # 3D distance using X, Y, and Z # @return [Float] 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 # Converts {Vector} to Array # @return [Array] def to_a @@ -282,9 +285,9 @@ end # Converts {Vector} to Hash # @return [Hash] def to_h - {x: @x, y: @y, z: @z, weight: @weight} + { x: @x, y: @y, z: @z, weight: @weight } end end -end \ No newline at end of file +end