gosling 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/gosling.rb +4 -0
- data/lib/gosling/actor.rb +235 -0
- data/lib/gosling/circle.rb +48 -0
- data/lib/gosling/collision.rb +126 -0
- data/lib/gosling/image_library.rb +17 -0
- data/lib/gosling/inheritance_error.rb +4 -0
- data/lib/gosling/polygon.rb +52 -0
- data/lib/gosling/rect.rb +38 -0
- data/lib/gosling/sprite.rb +35 -0
- data/lib/gosling/transform.rb +223 -0
- data/lib/gosling/version.rb +3 -0
- data/spec/actor_spec.rb +670 -0
- data/spec/circle_spec.rb +48 -0
- data/spec/collision_spec.rb +1183 -0
- data/spec/image_library_spec.rb +15 -0
- data/spec/images/bg2.png +0 -0
- data/spec/images/cursor.png +0 -0
- data/spec/images/display_case.png +0 -0
- data/spec/images/full_icons_07_18.png +0 -0
- data/spec/images/key.png +0 -0
- data/spec/images/nil.png +0 -0
- data/spec/polygon_spec.rb +213 -0
- data/spec/rect_spec.rb +87 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/sprite_spec.rb +43 -0
- data/spec/transform_spec.rb +328 -0
- metadata +98 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
describe Gosling::ImageLibrary do
|
2
|
+
it "returns a Gosu:Image reference when given a filename" do
|
3
|
+
expect(Gosling::ImageLibrary.get("C:/Users/Ben/Pictures/icons/me_64.png")).to be_instance_of(Gosu::Image)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "raises an argument error if the file does not exist" do
|
7
|
+
expect { Gosling::ImageLibrary.get("C:/does/not/exist.png") }.to raise_error(ArgumentError)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "does not create a new Gosu:Image if it already has one cached" do
|
11
|
+
image_a = Gosling::ImageLibrary.get("C:/Users/Ben/Pictures/icons/me_64.png")
|
12
|
+
image_b = Gosling::ImageLibrary.get("C:/Users/Ben/Pictures/icons/me_64.png")
|
13
|
+
expect(image_a).to be == image_b
|
14
|
+
end
|
15
|
+
end
|
data/spec/images/bg2.png
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/spec/images/key.png
ADDED
Binary file
|
data/spec/images/nil.png
ADDED
Binary file
|
@@ -0,0 +1,213 @@
|
|
1
|
+
describe Gosling::Polygon do
|
2
|
+
before(:all) do
|
3
|
+
@window = Gosu::Window.new(640, 480, false)
|
4
|
+
@polygon = Gosling::Polygon.new(@window)
|
5
|
+
end
|
6
|
+
|
7
|
+
describe '#get_vertices' do
|
8
|
+
it 'returns a list of three or more vertices' do
|
9
|
+
expect(@polygon.get_vertices).to be_instance_of(Array)
|
10
|
+
expect(@polygon.get_vertices[0]).to be_instance_of(Vector)
|
11
|
+
expect(@polygon.get_vertices.length).to be >= 3
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#set_vertices' do
|
16
|
+
it 'assigns new vertices' do
|
17
|
+
vertices = [
|
18
|
+
Vector[ 1, 0, 1],
|
19
|
+
Vector[ 0, 1, 1],
|
20
|
+
Vector[-1, 0, 1],
|
21
|
+
Vector[ 0, -1, 1],
|
22
|
+
]
|
23
|
+
|
24
|
+
polygon = Gosling::Polygon.new(@window)
|
25
|
+
polygon.set_vertices(vertices)
|
26
|
+
expect(polygon.get_vertices).to be == vertices
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'raises an error if the parameter is not an array' do
|
30
|
+
polygon = Gosling::Polygon.new(@window)
|
31
|
+
expect { polygon.set_vertices("foo") }.to raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'raises an error if the parameter array contains non-vectors' do
|
35
|
+
vertices = [
|
36
|
+
[ 1, 0, 1],
|
37
|
+
[ 0, 1, 1],
|
38
|
+
[-1, 0, 1],
|
39
|
+
[ 0, -1, 1],
|
40
|
+
]
|
41
|
+
|
42
|
+
polygon = Gosling::Polygon.new(@window)
|
43
|
+
expect { polygon.set_vertices(vertices) }.to raise_error(ArgumentError)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'raises an error if the parameter array is too short' do
|
47
|
+
vertices = [
|
48
|
+
Vector[ 1, 0, 1],
|
49
|
+
Vector[ 0, 1, 1]
|
50
|
+
]
|
51
|
+
|
52
|
+
polygon = Gosling::Polygon.new(@window)
|
53
|
+
expect { polygon.set_vertices(vertices) }.to raise_error(ArgumentError)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'raises an error if any vertices in the parameter array are not length 3' do
|
57
|
+
vertices = [
|
58
|
+
Vector[ 1, 0],
|
59
|
+
Vector[ 0, 1],
|
60
|
+
Vector[-1, 0],
|
61
|
+
Vector[ 0, -1],
|
62
|
+
]
|
63
|
+
|
64
|
+
polygon = Gosling::Polygon.new(@window)
|
65
|
+
expect { polygon.set_vertices(vertices) }.to raise_error(ArgumentError)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#get_global_vertices' do
|
70
|
+
before(:all) do
|
71
|
+
@global_polygon = Gosling::Polygon.new(@window)
|
72
|
+
@global_polygon.set_vertices([
|
73
|
+
Vector[1, 1, 0],
|
74
|
+
Vector[0, -1, 0],
|
75
|
+
Vector[-1, -1, 0],
|
76
|
+
Vector[-1, 2, 0]
|
77
|
+
])
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'returns a list of three or more vertices' do
|
81
|
+
result = @global_polygon.get_global_vertices
|
82
|
+
expect(result).to be_instance_of(Array)
|
83
|
+
expect(result[0]).to be_instance_of(Vector)
|
84
|
+
expect(result.length).to be >= 3
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'respects centering' do
|
88
|
+
@global_polygon.x = 0
|
89
|
+
@global_polygon.y = 0
|
90
|
+
@global_polygon.center_x = 10
|
91
|
+
@global_polygon.center_y = 2
|
92
|
+
@global_polygon.scale_x = 1
|
93
|
+
@global_polygon.scale_y = 1
|
94
|
+
@global_polygon.rotation = 0
|
95
|
+
|
96
|
+
vertices = @global_polygon.get_global_vertices
|
97
|
+
expect(vertices).to be == [
|
98
|
+
Vector[-9, -1, 0],
|
99
|
+
Vector[-10, -3, 0],
|
100
|
+
Vector[-11, -3, 0],
|
101
|
+
Vector[-11, 0, 0]
|
102
|
+
]
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'respects scaling' do
|
106
|
+
@global_polygon.x = 0
|
107
|
+
@global_polygon.y = 0
|
108
|
+
@global_polygon.center_x = 0
|
109
|
+
@global_polygon.center_y = 0
|
110
|
+
@global_polygon.scale_x = 3
|
111
|
+
@global_polygon.scale_y = 2
|
112
|
+
@global_polygon.rotation = 0
|
113
|
+
|
114
|
+
vertices = @global_polygon.get_global_vertices
|
115
|
+
expect(vertices).to be == [
|
116
|
+
Vector[3, 2, 0],
|
117
|
+
Vector[0, -2, 0],
|
118
|
+
Vector[-3, -2, 0],
|
119
|
+
Vector[-3, 4, 0]
|
120
|
+
]
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'respects rotation' do
|
124
|
+
@global_polygon.x = 0
|
125
|
+
@global_polygon.y = 0
|
126
|
+
@global_polygon.center_x = 0
|
127
|
+
@global_polygon.center_y = 0
|
128
|
+
@global_polygon.scale_x = 1
|
129
|
+
@global_polygon.scale_y = 1
|
130
|
+
@global_polygon.rotation = Math::PI / 2
|
131
|
+
|
132
|
+
vertices = @global_polygon.get_global_vertices
|
133
|
+
expect(vertices).to be == [
|
134
|
+
Vector[1, -1, 0],
|
135
|
+
Vector[-1, 0, 0],
|
136
|
+
Vector[-1, 1, 0],
|
137
|
+
Vector[2, 1, 0]
|
138
|
+
]
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'respects translation' do
|
142
|
+
@global_polygon.x = -50
|
143
|
+
@global_polygon.y = 10
|
144
|
+
@global_polygon.center_x = 0
|
145
|
+
@global_polygon.center_y = 0
|
146
|
+
@global_polygon.scale_x = 1
|
147
|
+
@global_polygon.scale_y = 1
|
148
|
+
@global_polygon.rotation = 0
|
149
|
+
|
150
|
+
vertices = @global_polygon.get_global_vertices
|
151
|
+
expect(vertices).to be == [
|
152
|
+
Vector[-49, 11, 0],
|
153
|
+
Vector[-50, 9, 0],
|
154
|
+
Vector[-51, 9, 0],
|
155
|
+
Vector[-51, 12, 0]
|
156
|
+
]
|
157
|
+
end
|
158
|
+
|
159
|
+
context 'with a long ancestry' do
|
160
|
+
before do
|
161
|
+
@global_polygon.x = 0
|
162
|
+
@global_polygon.y = 0
|
163
|
+
@global_polygon.center_x = 0
|
164
|
+
@global_polygon.center_y = 0
|
165
|
+
@global_polygon.scale_x = 1
|
166
|
+
@global_polygon.scale_y = 1
|
167
|
+
@global_polygon.rotation = 0
|
168
|
+
|
169
|
+
centered_view = Gosling::Actor.new(@window)
|
170
|
+
centered_view.center_x = 10
|
171
|
+
centered_view.center_y = 2
|
172
|
+
|
173
|
+
scaled_view = Gosling::Actor.new(@window)
|
174
|
+
scaled_view.scale_x = 3
|
175
|
+
scaled_view.scale_y = 2
|
176
|
+
|
177
|
+
rotated_view = Gosling::Actor.new(@window)
|
178
|
+
rotated_view.rotation = Math::PI / 2
|
179
|
+
|
180
|
+
translated_view = Gosling::Actor.new(@window)
|
181
|
+
translated_view.x = -50
|
182
|
+
translated_view.y = 10
|
183
|
+
|
184
|
+
centered_view.add_child(scaled_view)
|
185
|
+
scaled_view.add_child(rotated_view)
|
186
|
+
rotated_view.add_child(translated_view)
|
187
|
+
translated_view.add_child(@global_polygon)
|
188
|
+
|
189
|
+
@ancestry = [
|
190
|
+
centered_view,
|
191
|
+
scaled_view,
|
192
|
+
rotated_view,
|
193
|
+
translated_view,
|
194
|
+
@global_polygon
|
195
|
+
]
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'respects all ancestors' do
|
199
|
+
vertices = @global_polygon.get_global_vertices
|
200
|
+
expect(vertices).to be == [
|
201
|
+
Vector[(1 + 10) * 3 - 10, (1 - 50) * -2 - 2, 0],
|
202
|
+
Vector[(-1 + 10) * 3 - 10, (0 - 50) * -2 - 2, 0],
|
203
|
+
Vector[(-1 + 10) * 3 - 10, (-1 - 50) * -2 - 2, 0],
|
204
|
+
Vector[(2 + 10) * 3 - 10, (-1 - 50) * -2 - 2, 0]
|
205
|
+
]
|
206
|
+
end
|
207
|
+
|
208
|
+
after do
|
209
|
+
@ancestry.each { |actor| actor.parent.remove_child(actor) if actor.parent }
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
data/spec/rect_spec.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
describe Gosling::Rect do
|
2
|
+
before(:all) do
|
3
|
+
@window = Gosu::Window.new(640, 480, false)
|
4
|
+
@rect = Gosling::Rect.new(@window)
|
5
|
+
end
|
6
|
+
|
7
|
+
it 'starts with width and height 1' do
|
8
|
+
expect(@rect.width).to be == 1
|
9
|
+
expect(@rect.height).to be == 1
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#width' do
|
13
|
+
it 'returns our width' do
|
14
|
+
expect(@rect.width).to be_kind_of(Numeric)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#set_width' do
|
19
|
+
it 'alters our width, updating our vertices' do
|
20
|
+
rect = Gosling::Rect.new(@window)
|
21
|
+
rect.width = 10
|
22
|
+
expect(rect.width).to be == 10
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'cannot be less than one' do
|
26
|
+
rect = Gosling::Rect.new(@window)
|
27
|
+
expect { rect.width = 0 }.to raise_error(ArgumentError)
|
28
|
+
expect { rect.width = -100 }.to raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'updates our vertices' do
|
32
|
+
expected_vertices = [
|
33
|
+
Vector[ 0, 0, 0],
|
34
|
+
Vector[11, 0, 0],
|
35
|
+
Vector[11, 1, 0],
|
36
|
+
Vector[ 0, 1, 0]
|
37
|
+
]
|
38
|
+
|
39
|
+
rect = Gosling::Rect.new(@window)
|
40
|
+
rect.width = 11
|
41
|
+
expect(rect.get_vertices).to be == expected_vertices
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#height' do
|
46
|
+
it 'returns our height' do
|
47
|
+
expect(@rect.height).to be_kind_of(Numeric)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#set_height' do
|
52
|
+
it 'alters our height, updating our vertices' do
|
53
|
+
rect = Gosling::Rect.new(@window)
|
54
|
+
rect.height = 15
|
55
|
+
expect(rect.height).to be == 15
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'cannot be less than one' do
|
59
|
+
rect = Gosling::Rect.new(@window)
|
60
|
+
expect { rect.height = 0 }.to raise_error(ArgumentError)
|
61
|
+
expect { rect.height = -200 }.to raise_error(ArgumentError)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'updates our vertices' do
|
65
|
+
expected_vertices = [
|
66
|
+
Vector[0, 0, 0],
|
67
|
+
Vector[1, 0, 0],
|
68
|
+
Vector[1, 40, 0],
|
69
|
+
Vector[0, 40, 0]
|
70
|
+
]
|
71
|
+
|
72
|
+
rect = Gosling::Rect.new(@window)
|
73
|
+
rect.height = 40
|
74
|
+
expect(rect.get_vertices).to be == expected_vertices
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'does not allow set_vertices to be called directly' do
|
79
|
+
vertices = [
|
80
|
+
Vector[0, 0, 0],
|
81
|
+
Vector[1, 10, 0],
|
82
|
+
Vector[2, 20, 0],
|
83
|
+
Vector[3, 40, 0]
|
84
|
+
]
|
85
|
+
expect { @rect.set_vertices(vertices) }.to raise_error(NoMethodError)
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../lib/gosling.rb'
|
data/spec/sprite_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
describe Gosling::Sprite do
|
2
|
+
before(:all) do
|
3
|
+
@window = Gosu::Window.new(640, 480, false)
|
4
|
+
@local_path = File.dirname(__FILE__)
|
5
|
+
@image = Gosling::ImageLibrary.get(File.join(@local_path, 'images/key.png'))
|
6
|
+
|
7
|
+
@sprite = Gosling::Sprite.new(@window)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#get_image' do
|
11
|
+
it 'returns our image' do
|
12
|
+
get_sprite = Gosling::Sprite.new(@window)
|
13
|
+
expect(get_sprite.get_image).to be == nil
|
14
|
+
get_sprite.set_image(@image)
|
15
|
+
expect(get_sprite.get_image).to be_instance_of(Gosu::Image)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#set_image' do
|
20
|
+
before(:each) do
|
21
|
+
@image_sprite = Gosling::Sprite.new(@window)
|
22
|
+
@image_sprite.set_image(@image)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'complains if given something other than an image' do
|
26
|
+
expect { Gosling::Sprite.new(@window).set_image(:foo) }.to raise_error(ArgumentError)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'sets our image' do
|
30
|
+
expect(@image_sprite.get_image).to be == @image
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'automatically updates our width and height' do
|
34
|
+
expect(@image_sprite.width).to be == @image.width
|
35
|
+
expect(@image_sprite.height).to be == @image.height
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'does not allow set_width or set_height to be called directly' do
|
40
|
+
expect { @sprite.set_width(10) }.to raise_error(NoMethodError)
|
41
|
+
expect { @sprite.set_height(20) }.to raise_error(NoMethodError)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,328 @@
|
|
1
|
+
describe Gosling::Transform do
|
2
|
+
before(:all) do
|
3
|
+
@read_only_tf = Gosling::Transform.new
|
4
|
+
end
|
5
|
+
|
6
|
+
it 'has a center' do
|
7
|
+
expect(@read_only_tf.center).to be_instance_of(Vector)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'has a scale' do
|
11
|
+
expect(@read_only_tf.scale).to be_instance_of(Vector)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'has a rotation' do
|
15
|
+
expect(@read_only_tf.rotation).to be_instance_of(Fixnum)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'has a translation' do
|
19
|
+
expect(@read_only_tf.translation).to be_instance_of(Vector)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#set_center' do
|
23
|
+
it 'accepts a size 3 vector' do
|
24
|
+
tf = Gosling::Transform.new
|
25
|
+
expect { tf.set_center(Vector[0,0,0]) }.not_to raise_error
|
26
|
+
expect { tf.set_center(Vector[0,0,0,0]) }.to raise_error(ArgumentError)
|
27
|
+
expect { tf.set_center(Vector[0,0]) }.to raise_error(ArgumentError)
|
28
|
+
expect { tf.set_center(:foo) }.to raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'sets the center transform' do
|
32
|
+
v = Vector[10.to_r, 20.to_r, 0.to_r]
|
33
|
+
tf = Gosling::Transform.new
|
34
|
+
tf.set_center(v)
|
35
|
+
expect(tf.center).to be == v
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'does not alter the z value, which should always be 0' do
|
39
|
+
tf = Gosling::Transform.new
|
40
|
+
[
|
41
|
+
Vector[1, 1, 1],
|
42
|
+
Vector[1, 1, 13],
|
43
|
+
Vector[1, 1, 7],
|
44
|
+
Vector[1, 1, 29373],
|
45
|
+
Vector[1, 1, -1],
|
46
|
+
Vector[1, 1, 0],
|
47
|
+
Vector[1, 1, -328.7],
|
48
|
+
].each do |v|
|
49
|
+
tf.set_center(v)
|
50
|
+
expect(tf.center[2]).to be == 0
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#set_scale' do
|
56
|
+
it 'accepts a size 2 vector' do
|
57
|
+
tf = Gosling::Transform.new
|
58
|
+
expect { tf.set_scale(Vector[1,1]) }.not_to raise_error
|
59
|
+
expect { tf.set_scale(Vector[1,1,1]) }.to raise_error(ArgumentError)
|
60
|
+
expect { tf.set_scale(Vector[1]) }.to raise_error(ArgumentError)
|
61
|
+
expect { tf.set_scale(:foo) }.to raise_error(ArgumentError)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'sets the scale transform' do
|
65
|
+
v = Vector[2.to_r, 0.5.to_r]
|
66
|
+
tf = Gosling::Transform.new
|
67
|
+
tf.set_scale(v)
|
68
|
+
expect(tf.scale).to be == v
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#set_rotation' do
|
73
|
+
it 'sets the rotation transform' do
|
74
|
+
r = Math::PI / 2
|
75
|
+
tf = Gosling::Transform.new
|
76
|
+
tf.set_rotation(r)
|
77
|
+
expect(tf.rotation).to be == r
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#set_translation' do
|
82
|
+
it 'accepts a size 3 vector' do
|
83
|
+
tf = Gosling::Transform.new
|
84
|
+
expect { tf.set_translation(Vector[0,0,0]) }.not_to raise_error
|
85
|
+
expect { tf.set_translation(Vector[0,0,0,0]) }.to raise_error(ArgumentError)
|
86
|
+
expect { tf.set_translation(Vector[0,0]) }.to raise_error(ArgumentError)
|
87
|
+
expect { tf.set_translation(:foo) }.to raise_error(ArgumentError)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'sets the translation transform' do
|
91
|
+
v = Vector[1024.to_r, 768.to_r, 0.to_r]
|
92
|
+
tf = Gosling::Transform.new
|
93
|
+
tf.set_translation(v)
|
94
|
+
expect(tf.translation).to be == v
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'does not alter the z value, which should always be 0' do
|
98
|
+
tf = Gosling::Transform.new
|
99
|
+
[
|
100
|
+
Vector[1, 1, 1],
|
101
|
+
Vector[1, 1, 13],
|
102
|
+
Vector[1, 1, 7],
|
103
|
+
Vector[1, 1, 29373],
|
104
|
+
Vector[1, 1, -1],
|
105
|
+
Vector[1, 1, 0],
|
106
|
+
Vector[1, 1, -328.7],
|
107
|
+
].each do |v|
|
108
|
+
tf.set_translation(v)
|
109
|
+
expect(tf.translation[2]).to be == 0
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#to_matrix' do
|
115
|
+
it 'returns a matrix' do
|
116
|
+
expect(@read_only_tf.to_matrix).to be_instance_of(Matrix)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'centers correctly' do
|
121
|
+
tf = Gosling::Transform.new
|
122
|
+
tf.set_center(Vector[10.to_r, 20.to_r, 0.to_r])
|
123
|
+
expected_matrix = Matrix[
|
124
|
+
[1, 0, -10],
|
125
|
+
[0, 1, -20],
|
126
|
+
[0, 0, 1]
|
127
|
+
]
|
128
|
+
expect(tf.to_matrix).to be == expected_matrix
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'scales correctly' do
|
132
|
+
tf = Gosling::Transform.new
|
133
|
+
tf.set_scale(Vector[2.to_r, 0.5.to_r])
|
134
|
+
expected_matrix = Matrix[
|
135
|
+
[2, 0, 0],
|
136
|
+
[0, 0.5, 0],
|
137
|
+
[0, 0, 1]
|
138
|
+
]
|
139
|
+
expect(tf.to_matrix).to be == expected_matrix
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'rotates correctly' do
|
143
|
+
tf = Gosling::Transform.new
|
144
|
+
tf.set_rotation(Math::PI / 2)
|
145
|
+
expected_matrix = Matrix[
|
146
|
+
[ 0, 1, 0],
|
147
|
+
[-1, 0, 0],
|
148
|
+
[ 0, 0, 1]
|
149
|
+
]
|
150
|
+
expect(tf.to_matrix).to be == expected_matrix
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'translates correctly' do
|
154
|
+
tf = Gosling::Transform.new
|
155
|
+
tf.set_translation(Vector[1024.to_r, 768.to_r, 0.to_r])
|
156
|
+
expected_matrix = Matrix[
|
157
|
+
[1, 0, 1024],
|
158
|
+
[0, 1, 768],
|
159
|
+
[0, 0, 1]
|
160
|
+
]
|
161
|
+
expect(tf.to_matrix).to be == expected_matrix
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'applies all transforms in the correct order' do
|
165
|
+
tf = Gosling::Transform.new
|
166
|
+
tf.set_center(Vector[10.to_r, 20.to_r, 0.to_r])
|
167
|
+
tf.set_scale(Vector[2.to_r, 0.5.to_r])
|
168
|
+
tf.set_rotation(Math::PI / 2)
|
169
|
+
tf.set_translation(Vector[1024.to_r, 768.to_r, 0.to_r])
|
170
|
+
|
171
|
+
expected_matrix = Matrix[
|
172
|
+
[0.0, 0.5, 1014.0],
|
173
|
+
[-2.0, 0.0, 788.0],
|
174
|
+
[0.0, 0.0, 1.0]
|
175
|
+
]
|
176
|
+
expect(tf.to_matrix).to be == expected_matrix
|
177
|
+
|
178
|
+
v = Vector[0.to_r, -50.to_r, 1.to_r]
|
179
|
+
v_transformed = Vector[989.to_r, 788.to_r, 1.to_r]
|
180
|
+
expect(tf.to_matrix * v).to be == v_transformed
|
181
|
+
|
182
|
+
v = Vector[100.to_r, -50.to_r, 1.to_r]
|
183
|
+
v_transformed = Vector[989.to_r, 588.to_r, 1.to_r]
|
184
|
+
expect(tf.to_matrix * v).to be == v_transformed
|
185
|
+
|
186
|
+
v = Vector[100.to_r, 50.to_r, 1.to_r]
|
187
|
+
v_transformed = Vector[1039.to_r, 588.to_r, 1.to_r]
|
188
|
+
expect(tf.to_matrix * v).to be == v_transformed
|
189
|
+
|
190
|
+
v = Vector[0.to_r, 50.to_r, 1.to_r]
|
191
|
+
v_transformed = Vector[1039.to_r, 788.to_r, 1.to_r]
|
192
|
+
expect(tf.to_matrix * v).to be == v_transformed
|
193
|
+
end
|
194
|
+
|
195
|
+
describe '#transform_point' do
|
196
|
+
it 'expects a length 3 vector' do
|
197
|
+
expect { @read_only_tf.transform_point(Vector[1, 0, 1]) }.not_to raise_error
|
198
|
+
expect { @read_only_tf.transform_point(Vector[1, 0, 1, 1]) }.to raise_error(ArgumentError)
|
199
|
+
expect { @read_only_tf.transform_point(Vector[1, 0]) }.to raise_error(ArgumentError)
|
200
|
+
expect { @read_only_tf.transform_point(:foo) }.to raise_error(ArgumentError)
|
201
|
+
expect { @read_only_tf.transform_point(nil) }.to raise_error(ArgumentError)
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'returns a length 3 vector' do
|
205
|
+
result = @read_only_tf.transform_point(Vector[1, 0, 1])
|
206
|
+
expect(result).to be_instance_of(Vector)
|
207
|
+
expect(result.size).to be == 3
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'always returns a z value of 0' do
|
211
|
+
[
|
212
|
+
Vector[1, 1, 1],
|
213
|
+
Vector[-1, -1, -1],
|
214
|
+
Vector[-22, -22, 0],
|
215
|
+
Vector[-11, 13, 34],
|
216
|
+
Vector[37, -4, -15],
|
217
|
+
Vector[34, 39, -16],
|
218
|
+
Vector[-48, 23, -32],
|
219
|
+
Vector[24, -39, 42],
|
220
|
+
Vector[49, 44, -15],
|
221
|
+
Vector[27, 23, 42],
|
222
|
+
Vector[33, -25, -20],
|
223
|
+
Vector[-46, -18, 48],
|
224
|
+
].each do |v|
|
225
|
+
expect(@read_only_tf.transform_point(v)[2]).to be == 0
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'transforms the point correctly' do
|
230
|
+
tf = Gosling::Transform.new
|
231
|
+
tf.set_center(Vector[5.to_r, 20.to_r, 0.to_r])
|
232
|
+
tf.set_scale(Vector[2.to_r, 0.5.to_r])
|
233
|
+
tf.set_rotation(Math::PI / 2)
|
234
|
+
tf.set_translation(Vector[1024.to_r, 768.to_r, 0.to_r])
|
235
|
+
|
236
|
+
[
|
237
|
+
[0, 0],
|
238
|
+
[10, -20],
|
239
|
+
[-1024, -768],
|
240
|
+
[-5, 999],
|
241
|
+
[5, 20]
|
242
|
+
].each do |pt|
|
243
|
+
x, y = pt
|
244
|
+
expect(tf.transform_point(Vector[x, y, 0])).to be == Vector[(y - 20) * 0.5 + 1024, (x - 5) * -2 + 768, 0]
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
describe '#untransform_point' do
|
250
|
+
it 'expects a length 3 vector' do
|
251
|
+
expect { @read_only_tf.untransform_point(Vector[1, 0, 1]) }.not_to raise_error
|
252
|
+
expect { @read_only_tf.untransform_point(Vector[1, 0, 1, 1]) }.to raise_error(ArgumentError)
|
253
|
+
expect { @read_only_tf.untransform_point(Vector[1, 0]) }.to raise_error(ArgumentError)
|
254
|
+
expect { @read_only_tf.untransform_point(:foo) }.to raise_error(ArgumentError)
|
255
|
+
expect { @read_only_tf.untransform_point(nil) }.to raise_error(ArgumentError)
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'returns a length 3 vector' do
|
259
|
+
result = @read_only_tf.untransform_point(Vector[1, 0, 1])
|
260
|
+
expect(result).to be_instance_of(Vector)
|
261
|
+
expect(result.size).to be == 3
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'always returns a z value of 0' do
|
265
|
+
[
|
266
|
+
Vector[1, 1, 1],
|
267
|
+
Vector[-1, -1, -1],
|
268
|
+
Vector[-22, -22, 0],
|
269
|
+
Vector[-11, 13, 34],
|
270
|
+
Vector[37, -4, -15],
|
271
|
+
Vector[34, 39, -16],
|
272
|
+
Vector[-48, 23, -32],
|
273
|
+
Vector[24, -39, 42],
|
274
|
+
Vector[49, 44, -15],
|
275
|
+
Vector[27, 23, 42],
|
276
|
+
Vector[33, -25, -20],
|
277
|
+
Vector[-46, -18, 48],
|
278
|
+
].each do |v|
|
279
|
+
expect(@read_only_tf.untransform_point(v)[2]).to be == 0
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
it 'untransforms the point correctly' do
|
284
|
+
tf = Gosling::Transform.new
|
285
|
+
tf.set_center(Vector[5.to_r, 20.to_r, 0.to_r])
|
286
|
+
tf.set_scale(Vector[2.to_r, 0.5.to_r])
|
287
|
+
tf.set_rotation(Math::PI / 2)
|
288
|
+
tf.set_translation(Vector[1024.to_r, 768.to_r, 0.to_r])
|
289
|
+
|
290
|
+
[
|
291
|
+
[1014, 778],
|
292
|
+
[1004, 758],
|
293
|
+
[630, 2826],
|
294
|
+
[788, 3027],
|
295
|
+
[768, 1024]
|
296
|
+
].each do |pt|
|
297
|
+
x, y = pt
|
298
|
+
expect(tf.untransform_point(Vector[x, y, 0])).to be == Vector[(y - 768) * -0.5 + 5, (x - 1024) * 2 + 20, 0]
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
it 'undoes the results of transform_point' do
|
303
|
+
tf = Gosling::Transform.new
|
304
|
+
tf.set_center(Vector[5.to_r, 20.to_r, 0.to_r])
|
305
|
+
tf.set_scale(Vector[2.to_r, 0.5.to_r])
|
306
|
+
tf.set_rotation(Math::PI / 2)
|
307
|
+
tf.set_translation(Vector[1024.to_r, 768.to_r, 0.to_r])
|
308
|
+
|
309
|
+
[
|
310
|
+
Vector[1, 1, 0],
|
311
|
+
Vector[-1, -1, 0],
|
312
|
+
Vector[-22, -22, 0],
|
313
|
+
Vector[-11, 13, 0],
|
314
|
+
Vector[37, -4, 0],
|
315
|
+
Vector[34, 39, 0],
|
316
|
+
Vector[-48, 23, 0],
|
317
|
+
Vector[24, -39, 0],
|
318
|
+
Vector[49, 44, 0],
|
319
|
+
Vector[27, 23, 0],
|
320
|
+
Vector[33, -25, 0],
|
321
|
+
Vector[-46, -18, 0],
|
322
|
+
].each do |v|
|
323
|
+
expect(tf.untransform_point(tf.transform_point(v))).to be == v
|
324
|
+
expect(tf.transform_point(tf.untransform_point(v))).to be == v
|
325
|
+
end
|
326
|
+
end
|
327
|
+
end
|
328
|
+
end
|