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.
115 116 117 |
# File 'lib/vector3.rb', line 115 def %(vector) vector_product(vector) end |
#*(scalar) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/vector3.rb', line 94 def *(scalar) if scalar.kind_of? Numeric 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
102 103 104 |
# File 'lib/vector3.rb', line 102 def +(v) Vector3.new(@x + v.x, @y + v.y, @z + v.z) end |
#-(v) ⇒ Object
143 144 145 |
# File 'lib/vector3.rb', line 143 def -(v) Vector3.new(@x - v.x, @y - v.y, @z - v.z) end |
#addScaledVector(v, scalar) ⇒ Object
60 61 62 63 64 |
# File 'lib/vector3.rb', line 60 def addScaledVector(v, scalar) @x += (v.x * scalar) @y += (v.y * scalar) @z += (v.z * scalar) end |
#addVector(v) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/vector3.rb', line 118 def addVector(v) @x += v.x @y += v.y @z += v.z self end |
#clear ⇒ Object
147 148 149 |
# File 'lib/vector3.rb', line 147 def clear @x, @y, @z = 0,0,0 end |
#componentProduct(v) ⇒ Object
66 67 68 |
# File 'lib/vector3.rb', line 66 def componentProduct(v) Vector3.new(@x * v.x, @y * v.y, @z * v.z) end |
#componentProductUpdate(v) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/vector3.rb', line 70 def componentProductUpdate(v) @x *= v.x @y *= v.y @z *= v.z self end |
#crossProduct(v) ⇒ Object
81 82 83 84 85 |
# File 'lib/vector3.rb', line 81 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
87 88 89 90 91 92 |
# File 'lib/vector3.rb', line 87 def crossProductUpdate(v) new_v = vectorProduct(v) @x = new_v.x @y = new_v.y @z = new_v.z end |
#divideByScalar(scalar) ⇒ Object
137 138 139 140 141 |
# File 'lib/vector3.rb', line 137 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
125 126 127 128 129 |
# File 'lib/vector3.rb', line 125 def multiplyByScalar(scalar) @x *= scalar @y *= scalar @z *= scalar end |
#normalize ⇒ Object
Turns a non-zero vector into a vector of unit length.
35 36 37 38 39 40 41 42 |
# File 'lib/vector3.rb', line 35 def normalize length = self.magnitude if length > 0 @x *= 1.0/length @y *= 1.0/length @z *= 1.0/length end end |
#scalarProduct(v) ⇒ Object
77 78 79 |
# File 'lib/vector3.rb', line 77 def scalarProduct(v) (@x * v.x) + (@y * v.y) + (@z * v.z) end |
#squareMagnitude ⇒ Object
30 31 32 |
# File 'lib/vector3.rb', line 30 def squareMagnitude return (@x*@x)+(@y*@y)+(@z*@z) end |
#subtractVector(v) ⇒ Object
131 132 133 134 135 |
# File 'lib/vector3.rb', line 131 def subtractVector(v) @x -= v.x @y -= v.y @z -= v.z end |
#to_s ⇒ Object
151 152 153 |
# File 'lib/vector3.rb', line 151 def to_s "(#{@x},#{@y},#{@z})" end |
#unit ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vector3.rb', line 45 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 |