shattered_support 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/vector.rb DELETED
@@ -1,206 +0,0 @@
1
-
2
- class Array #:nodoc:
3
- # Vector extensions for array. Allows for coersion of a 3 element Array into a vector.
4
-
5
- # Randomize the order of an array
6
- #
7
- # [1, 2, 3].shuffle #=> [2, 3, 1]
8
- #
9
- def shuffle
10
- sort_by {rand}
11
- end
12
-
13
- # Create a vector from an array.
14
- #
15
- # [1, 2, 3].to_v #=> same as Vector.new(1, 2, 3)
16
- #
17
- def to_v
18
- raise StandardError, "vector #{self.inspect} does not have 3 elements" if self.length < 3
19
- Vector.new self[0], self[1] ,self[2]
20
- end
21
-
22
- # Returns this Array as an Ogre vector object: Vector3. Should only be used internally by
23
- # Shattered to communicate with the Ogre engine.
24
- def to_v3
25
- self.to_v.to_v3
26
- end
27
- end
28
-
29
- module ShatteredSupport #:nodoc:
30
- # Vector is a three dimensional array, that allows for various operations on itself.
31
- # To create a vector, use the shorthand:
32
- # v(0,0,0) # Creates a zero vector
33
- class Vector
34
-
35
- attr_reader :x, :y, :z
36
-
37
- # Create a new Vector object. It requires exactly 3 arguments: x, y and z. Any value that
38
- # can be converted to a float is acceptable.
39
- #
40
- # *NOTE:* The recomended way to create Vector objects is to use the <tt>v(1,2,3)</tt> shorthand.
41
- def initialize(x_val, y_val, z_val)
42
- @x, @y, @z = [x_val, y_val, z_val].collect(&:to_f)
43
- end
44
-
45
- # Iterate through x, y and z with a block. The passed value is the value of each component of
46
- # the vector.
47
- def each(&block)
48
- self.to_a.each do |component|
49
- yield component
50
- end
51
- end
52
-
53
- # Returns this Vector as an Ogre vector object: Vector3. Should only be used internally by
54
- # Shattered to communicate with the Ogre engine.
55
- def to_v3 # :nodoc:
56
- ShatteredOgre::Vector3.new @x, @y, @z
57
- end
58
-
59
- # Returns self
60
- def to_v
61
- self
62
- end
63
-
64
- # Returns an array with x, y and z dumped into its elements.
65
- # v(1, 2, 3).to_a #=> [1.0, 2.0, 3.0]
66
- def to_a
67
- [@x, @y, @z]
68
- end
69
-
70
- # Add 2 Vectors together.
71
- # v(1,1,1) + v(1,2,3) #=> v(2,3,4)
72
- def +(*args)
73
- vector = convert_args_to_vector(args)
74
- Vector.new(
75
- self.x + vector.x,
76
- self.y + vector.y,
77
- self.z + vector.z
78
- )
79
- end
80
-
81
- # Subtract one Vector from another.
82
- # v(1,2,3) - v(1,1,1) #=> v(0,1,2)
83
- def -(args)
84
- vector = convert_args_to_vector(args)
85
- Vector.new(
86
- self.x - vector.x,
87
- self.y - vector.y,
88
- self.z - vector.z
89
- )
90
- end
91
-
92
- # Multiply all components of a vector by a scalar amount.
93
- # v(1,2,3) * 3 #=> v(3,6,9)
94
- def *(value)
95
- result = []
96
- each do |i|
97
- result << i * value
98
- end
99
- result.to_v
100
- end
101
-
102
- # Divide all components of a vector by a scalar amount
103
- # v(5,10,15) / 5 #=> v(1,2,3)
104
- def /(value)
105
- result = Array.new
106
- self.each do |i|
107
- result << i/value.to_f
108
- end
109
- result.to_v
110
- end
111
-
112
- # Returns this Vector but normalized to a length of 1.
113
- # v(9, 0, 0).normalize #=> v(1,0,0)
114
- def normalize
115
- self * (1 / length)
116
- end
117
- alias_method :normalise, :normalize
118
-
119
- # Same as #normalize but modifies the receiver in place.
120
- def normalize!
121
- @x, @y, @z = normalize.to_a
122
- end
123
- alias_method :normalise!, :normalize!
124
-
125
- # Return the value specified by bracket notation. Integers, Symbols or Strings
126
- # are accepted as keys.
127
- #
128
- # vector = v(1,2,3)
129
- # vector[:x] #=> 1
130
- # vector['y'] #=> 2
131
- # vector[2] #=> 3
132
- def [](index)
133
- case
134
- when index == 0 || index == :x || index == 'x'
135
- @x
136
- when index == 1 || index == :y || index == 'y'
137
- @y
138
- when index == 2 || index == :z || index == 'z'
139
- @z
140
- end
141
- end
142
-
143
- # Set the value specified by bracket notation. Accepts the same keys as the #[]
144
- # method.
145
- def []=(index, value)
146
- case
147
- when index == 0 || index == :x || index == 'x'
148
- @x = value.to_f
149
- when index == 1 || index == :y || index == 'y'
150
- @y = value.to_f
151
- when index == 2 || index == :z || index == 'z'
152
- @z = value.to_f
153
- end
154
- end
155
-
156
- # Set the value of the X component.
157
- def x=(value)
158
- @x = value.to_f
159
- end
160
-
161
- # Set the value of the Y component.
162
- def y=(value)
163
- @y = value.to_f
164
- end
165
-
166
- # Set the value of the Z component.
167
- def z=(value)
168
- @z = value.to_f
169
- end
170
-
171
-
172
- # Returns the length of this vector.
173
- # v(0,0,7).length #=> 7
174
- def length
175
- Math.sqrt(x**2 + y**2 + z**2)
176
- end
177
-
178
- # Converts the vector into an easily identifiable form. Mostly used for debugging
179
- # and console output.
180
- # v(1,2,3).to_s #=> "#<Vector [1.0, 2.0, 3.0]>"
181
- def to_s
182
- "#<Vector [#@x, #@y, #@z]>"
183
- end
184
-
185
- # Equality test. This method will return true if all components of both vectors are
186
- # indentical.
187
- def ==(vector)
188
- vector.kind_of?(Vector) &&
189
- x == vector.x &&
190
- y == vector.y &&
191
- z == vector.z
192
- end
193
-
194
- private
195
-
196
- def convert_args_to_vector(*args)
197
- args.flatten!
198
- if args.first.is_a? Vector
199
- args.first
200
- else
201
- args.to_v
202
- end
203
- end
204
-
205
- end
206
- end