Class: Ogre::Vector3

Inherits:
Object show all
Defined in:
lib/shattered_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



121
122
123
124
125
126
127
128
129
# File 'lib/shattered_ogrerb/vector.rb', line 121

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



105
106
107
108
# File 'lib/shattered_ogrerb/vector.rb', line 105

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

#-(*args) ⇒ Object



113
114
115
116
# File 'lib/shattered_ogrerb/vector.rb', line 113

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.



181
182
183
184
185
186
187
# File 'lib/shattered_ogrerb/vector.rb', line 181

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


147
148
149
150
151
152
153
154
155
156
# File 'lib/shattered_ogrerb/vector.rb', line 147

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.



160
161
162
163
164
165
166
167
168
169
# File 'lib/shattered_ogrerb/vector.rb', line 160

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.



84
85
86
87
88
# File 'lib/shattered_ogrerb/vector.rb', line 84

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

#eql?(other) ⇒ Boolean

Equality test for hash indexes.

Returns:

  • (Boolean)


195
196
197
# File 'lib/shattered_ogrerb/vector.rb', line 195

def eql?(other)
  return self.to_a.eql?(other.to_a)
end

#hashObject

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



190
191
192
# File 'lib/shattered_ogrerb/vector.rb', line 190

def hash
 return self.to_a.hash
end

#ogre_minusObject

Subtract one Vector from another.

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


112
# File 'lib/shattered_ogrerb/vector.rb', line 112

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)


120
# File 'lib/shattered_ogrerb/vector.rb', line 120

alias_method :ogre_multiply, :*

#ogre_plusObject

Add 2 Vectors together.

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


104
# File 'lib/shattered_ogrerb/vector.rb', line 104

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


175
176
177
# File 'lib/shattered_ogrerb/vector.rb', line 175

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

#to_vObject

Returns self



91
92
93
# File 'lib/shattered_ogrerb/vector.rb', line 91

def to_v
	self
end