Class: BulldogPhysics::Vector3
- Inherits:
-
Object
- Object
- BulldogPhysics::Vector3
- Defined in:
- lib/vector3.rb
Instance Attribute Summary collapse
-
#pad ⇒ Object
readonly
padding to ensure four world alignment.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
-
#%(vector) ⇒ Object
Updates this vector to be the vector product of its current => value and the given vector.
- #*(scalar) ⇒ Object
- #+(v) ⇒ Object
- #-(v) ⇒ Object
- #addScaledVector(v, scalar) ⇒ Object
- #addVector(v) ⇒ Object
- #clear ⇒ Object
- #componentProduct(v) ⇒ Object
- #componentProductUpdate(v) ⇒ Object
- #crossProduct(v) ⇒ Object
- #crossProductUpdate(v) ⇒ Object
- #divideByScalar(scalar) ⇒ Object
-
#initialize(x = 0, y = 0, z = 0) ⇒ Vector3
constructor
create Vertex, defaults all to zero.
-
#invert ⇒ Object
flip the components.
-
#magnitude ⇒ Object
Gets the magnitude of this vector.
- #multiplyByScalar(scalar) ⇒ Object
-
#normalize ⇒ Object
Turns a non-zero vector into a vector of unit length.
- #scalarProduct(v) ⇒ Object
- #squareMagnitude ⇒ Object
- #subtractVector(v) ⇒ Object
- #to_s ⇒ Object
- #unit ⇒ Object
- #vector_product(vector) ⇒ Object
Constructor Details
#initialize(x = 0, y = 0, z = 0) ⇒ Vector3
create Vertex, defaults all to zero
9 10 11 12 13 |
# File 'lib/vector3.rb', line 9 def initialize(x = 0,y = 0,z = 0) @x = x @y = y @z = z end |
Instance Attribute Details
#pad ⇒ Object (readonly)
padding to ensure four world alignment
6 7 8 |
# File 'lib/vector3.rb', line 6 def pad @pad end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/vector3.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/vector3.rb', line 5 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
5 6 7 |
# File 'lib/vector3.rb', line 5 def z @z end |
Instance Method Details
#%(vector) ⇒ Object
Updates this vector to be the vector product of its current
> value and the given vector.
113 114 115 |
# File 'lib/vector3.rb', line 113 def %(vector) vector_product(vector) end |
#*(scalar) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/vector3.rb', line 92 def *(scalar) if scalar.kind_of? Float Vector3.new(@x*scalar, @y*scalar, @z*scalar) elsif scalar.kind_of? Vector3 return @x * scalar.x + @y * scalar.y + @z * scalar.z end end |
#+(v) ⇒ Object
100 101 102 |
# File 'lib/vector3.rb', line 100 def +(v) Vector3.new(@x + v.x, @y + v.y, @z + v.z) end |
#-(v) ⇒ Object
141 142 143 |
# File 'lib/vector3.rb', line 141 def -(v) Vector3.new(@x - v.x, @y - v.y, @z - v.z) end |
#addScaledVector(v, scalar) ⇒ Object
58 59 60 61 62 |
# File 'lib/vector3.rb', line 58 def addScaledVector(v, scalar) @x += (v.x * scalar) @y += (v.y * scalar) @z += (v.z * scalar) end |
#addVector(v) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/vector3.rb', line 116 def addVector(v) @x += v.x @y += v.y @z += v.z self end |
#clear ⇒ Object
145 146 147 |
# File 'lib/vector3.rb', line 145 def clear @x, @y, @z = 0,0,0 end |
#componentProduct(v) ⇒ Object
64 65 66 |
# File 'lib/vector3.rb', line 64 def componentProduct(v) Vector3.new(@x * v.x, @y * v.y, @z * v.z) end |
#componentProductUpdate(v) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/vector3.rb', line 68 def componentProductUpdate(v) @x *= v.x @y *= v.y @z *= v.z self end |
#crossProduct(v) ⇒ Object
79 80 81 82 83 |
# File 'lib/vector3.rb', line 79 def crossProduct(v) Vector3.new( (@y * v.z) - (@z * v.y), (@z * v.x) - (@x * v.z), (@x * v.y) - (@y * v.x)) end |
#crossProductUpdate(v) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/vector3.rb', line 85 def crossProductUpdate(v) new_v = vectorProduct(v) @x = new_v.x @y = new_v.y @z = new_v.z end |
#divideByScalar(scalar) ⇒ Object
135 136 137 138 139 |
# File 'lib/vector3.rb', line 135 def divideByScalar(scalar) @x *= scalar @y *= scalar @z *= scalar end |
#invert ⇒ Object
flip the components
16 17 18 19 20 |
# File 'lib/vector3.rb', line 16 def invert @x = -@x @y = -@y @z = -@z end |
#magnitude ⇒ Object
Gets the magnitude of this vector.
23 24 25 26 27 |
# File 'lib/vector3.rb', line 23 def magnitude num = (@x*@x) + (@y * @y) + (@z * @z) return 0 if num.nan? Math.sqrt((@x*@x)+(@y*@y)+(@z*@z)) end |
#multiplyByScalar(scalar) ⇒ Object
123 124 125 126 127 |
# File 'lib/vector3.rb', line 123 def multiplyByScalar(scalar) @x *= scalar @y *= scalar @z *= scalar end |
#normalize ⇒ Object
Turns a non-zero vector into a vector of unit length.
34 35 36 37 38 39 40 41 |
# File 'lib/vector3.rb', line 34 def normalize length = self.magnitude if length > 0 @x /= length @y /= length @z /= length end end |
#scalarProduct(v) ⇒ Object
75 76 77 |
# File 'lib/vector3.rb', line 75 def scalarProduct(v) (@x * v.x) + (@y * v.y) + (@z * v.z) end |
#squareMagnitude ⇒ Object
29 30 31 |
# File 'lib/vector3.rb', line 29 def squareMagnitude return (@x*@x)+(@y*@y)+(@z*@z) end |
#subtractVector(v) ⇒ Object
129 130 131 132 133 |
# File 'lib/vector3.rb', line 129 def subtractVector(v) @x -= v.x @y -= v.y @z -= v.z end |
#to_s ⇒ Object
149 150 151 |
# File 'lib/vector3.rb', line 149 def to_s "(#{@x},#{@y},#{@z})" end |
#unit ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vector3.rb', line 43 def unit length = self.magnitude if length > 0 x = @x * (1.0 / length) #x = @x / length y = @y * (1.0 / length) #y = @y / length #z = @z / length z = @z * (1.0 / length) return Vector3.new(x,y,z) else return Vector3.new end end |