Class: Ogre::Vector3

Inherits:
Object show all
Defined in:
lib/shattered_view/ogrerb/vector.rb

Overview

NOTE: The recomended way to create Vector objects is to use the v(1,2,3) shorthand.

Instance Method Summary collapse

Instance Method Details

#*(*args) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/shattered_view/ogrerb/vector.rb', line 152

def *(*args)
	#Handle numbers and vectors
	unless args[0].is_a? Symbol 
		return ogre_multiply(args[0])
	end
	#Symbol to vector conversion
	v = convert_args_to_vector args
	ogre_multiply v
end

#+(*args) ⇒ Object



136
137
138
139
# File 'lib/shattered_view/ogrerb/vector.rb', line 136

def +(*args)
	v = convert_args_to_vector args
	ogre_plus v
end

#-(*args) ⇒ Object



144
145
146
147
# File 'lib/shattered_view/ogrerb/vector.rb', line 144

def -(*args)
	v = convert_args_to_vector args
	ogre_minus v
end

#==(vector) ⇒ Object

Equality test. This method will return true if all components of both vectors are indentical.



212
213
214
215
216
217
218
# File 'lib/shattered_view/ogrerb/vector.rb', line 212

def ==(vector)
  vector = vector.to_v if vector.is_a?(Symbol)
	vector.kind_of?(Ogre::Vector3) && 
	x == vector.x           && 
	y == vector.y           &&
	z == vector.z
end

#[](index) ⇒ Object

Return the value specified by bracket notation. Integers, Symbols or Strings are accepted as keys.

vector = v(1,2,3)
vector[:x]  #=> 1
vector['y'] #=> 2
vector[2]   #=> 3


178
179
180
181
182
183
184
185
186
187
# File 'lib/shattered_view/ogrerb/vector.rb', line 178

def [](index)
	case
		when index == 0 || index == :x || index == 'x'
			x
		when index == 1 || index == :y || index == 'y'
			y
		when index == 2 || index == :z || index == 'z'
			z
	end
end

#[]=(index, value) ⇒ Object

Set the value specified by bracket notation. Accepts the same keys as the #[] method.



191
192
193
194
195
196
197
198
199
200
# File 'lib/shattered_view/ogrerb/vector.rb', line 191

def []=(index, value)
	case
		when index == 0 || index == :x || index == 'x'
			self.x = value.to_f
		when index == 1 || index == :y || index == 'y'
			self.y = value.to_f
		when index == 2 || index == :z || index == 'z'
			self.z = value.to_f
	end
end

#each(&block) ⇒ Object

Iterate through x, y and z with a block. The passed value is the value of each component of the vector.



115
116
117
118
119
# File 'lib/shattered_view/ogrerb/vector.rb', line 115

def each(&block)
	[x,y,z].each do |component|
		yield component
	end
end

#eql?(other) ⇒ Boolean

Equality test for hash indexes.

Returns:

  • (Boolean)


226
227
228
# File 'lib/shattered_view/ogrerb/vector.rb', line 226

def eql?(other)
  x == other.x && y == other.y && z == other.z
end

#hashObject

Create a unique identifier based on x, y and z.



221
222
223
# File 'lib/shattered_view/ogrerb/vector.rb', line 221

def hash
 [x,y,z].hash
end

#ogre_minusObject

Subtract one Vector from another.

v(1,2,3) - v(1,1,1) #=> v(0,1,2)


143
# File 'lib/shattered_view/ogrerb/vector.rb', line 143

alias_method :ogre_minus, :-

#ogre_multiplyObject

Multiply all components of a vector by a scalar amount.

v(1,2,3) * 3 #=> v(3,6,9)


151
# File 'lib/shattered_view/ogrerb/vector.rb', line 151

alias_method :ogre_multiply, :*

#ogre_plusObject

Add 2 Vectors together.

v(1,1,1) + v(1,2,3) #=> v(2,3,4)


135
# File 'lib/shattered_view/ogrerb/vector.rb', line 135

alias_method :ogre_plus, :+

#to_sObject

Converts the vector into an easily identifiable form. Mostly used for debugging and console output.

v(1,2,3).to_s #=> "#<Vector [1.0, 2.0, 3.0]>"


206
207
208
# File 'lib/shattered_view/ogrerb/vector.rb', line 206

def to_s
	"#<Vector [#{x}, #{y}, #{z}]>"
end

#to_vObject

Returns self



122
123
124
# File 'lib/shattered_view/ogrerb/vector.rb', line 122

def to_v
	self
end