Class: BulldogPhysics::Vector3

Inherits:
Object
  • Object
show all
Defined in:
lib/vector3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#padObject (readonly)

padding to ensure four world alignment



6
7
8
# File 'lib/vector3.rb', line 6

def pad
  @pad
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/vector3.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/vector3.rb', line 5

def y
  @y
end

#zObject

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

#clearObject



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

#invertObject

flip the components



14
15
16
17
18
# File 'lib/vector3.rb', line 14

def invert
  @x = -@x
  @y = -@y
  @z = -@z
end

#magnitudeObject

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

#normalizeObject

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

#squareMagnitudeObject



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_sObject



146
147
148
# File 'lib/vector3.rb', line 146

def to_s
  "(#{@x},#{@y},#{@z})"
end

#unitObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vector3.rb', line 43

def unit
  length = self.magnitude
  if length > 0
    x = @x * (1.0 / length)
    y = @y * (1.0 / length)
    z = @z * (1.0 / length) 
    return Vector3.new(x,y,z)
  else
    return Vector3.new
  end
end

#vector_product(vector) ⇒ Object



101
102
103
104
105
106
# File 'lib/vector3.rb', line 101

def vector_product(vector)
   Vector3.new(@y*vector.z - @z*vector.y,
           @z*vector.x - @x*vector.z,
           @x*vector.y - @y*vector.x)

end