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 |
# File 'lib/vector3.rb', line 9 def initialize(x = 0,y = 0,z = 0) @x, @y, @z = x, y, 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.
110 111 112 |
# File 'lib/vector3.rb', line 110 def %(vector) vector_product(vector) end |
#*(scalar) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/vector3.rb', line 89 def *(scalar) if scalar.kind_of? Numeric return 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
97 98 99 |
# File 'lib/vector3.rb', line 97 def +(v) Vector3.new(@x + v.x, @y + v.y, @z + v.z) end |
#-(v) ⇒ Object
138 139 140 |
# File 'lib/vector3.rb', line 138 def -(v) Vector3.new(@x - v.x, @y - v.y, @z - v.z) end |
#addScaledVector(v, scalar) ⇒ Object
55 56 57 58 59 |
# File 'lib/vector3.rb', line 55 def addScaledVector(v, scalar) @x += (v.x * scalar) @y += (v.y * scalar) @z += (v.z * scalar) end |
#addVector(v) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/vector3.rb', line 113 def addVector(v) @x += v.x @y += v.y @z += v.z self end |
#clear ⇒ Object
142 143 144 |
# File 'lib/vector3.rb', line 142 def clear @x, @y, @z = 0,0,0 end |
#componentProduct(v) ⇒ Object
61 62 63 |
# File 'lib/vector3.rb', line 61 def componentProduct(v) Vector3.new(@x * v.x, @y * v.y, @z * v.z) end |
#componentProductUpdate(v) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/vector3.rb', line 65 def componentProductUpdate(v) @x *= v.x @y *= v.y @z *= v.z self end |
#crossProduct(v) ⇒ Object
76 77 78 79 80 |
# File 'lib/vector3.rb', line 76 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
82 83 84 85 86 87 |
# File 'lib/vector3.rb', line 82 def crossProductUpdate(v) new_v = vectorProduct(v) @x = new_v.x @y = new_v.y @z = new_v.z end |
#divideByScalar(scalar) ⇒ Object
132 133 134 135 136 |
# File 'lib/vector3.rb', line 132 def divideByScalar(scalar) @x *= scalar @y *= scalar @z *= scalar end |
#invert ⇒ Object
flip the components
14 15 16 17 18 |
# File 'lib/vector3.rb', line 14 def invert @x = -@x @y = -@y @z = -@z end |
#magnitude ⇒ Object
Gets the magnitude of this vector.
21 22 23 24 25 |
# File 'lib/vector3.rb', line 21 def magnitude num = ((@x*@x) + (@y * @y) + (@z * @z)).abs #return 0 if num.nan? Math.sqrt(num) end |
#multiplyByScalar(scalar) ⇒ Object
120 121 122 123 124 |
# File 'lib/vector3.rb', line 120 def multiplyByScalar(scalar) @x *= scalar @y *= scalar @z *= scalar end |
#normalize ⇒ Object
Turns a non-zero vector into a vector of unit length.
33 34 35 36 37 38 39 40 |
# File 'lib/vector3.rb', line 33 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
72 73 74 |
# File 'lib/vector3.rb', line 72 def scalarProduct(v) (@x * v.x) + (@y * v.y) + (@z * v.z) end |
#squareMagnitude ⇒ Object
28 29 30 |
# File 'lib/vector3.rb', line 28 def squareMagnitude return (@x*@x)+(@y*@y)+(@z*@z) end |
#subtractVector(v) ⇒ Object
126 127 128 129 130 |
# File 'lib/vector3.rb', line 126 def subtractVector(v) @x -= v.x @y -= v.y @z -= v.z end |
#to_s ⇒ Object
146 147 148 |
# File 'lib/vector3.rb', line 146 def to_s "(#{@x},#{@y},#{@z})" end |