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
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

#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.



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

#clearObject



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

#invertObject

flip the components



16
17
18
19
20
# File 'lib/vector3.rb', line 16

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

#magnitudeObject

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

#normalizeObject

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

#squareMagnitudeObject



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_sObject



151
152
153
# File 'lib/vector3.rb', line 151

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

#unitObject



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

#vector_product(vector) ⇒ Object



106
107
108
109
110
111
# File 'lib/vector3.rb', line 106

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