gosling 1.0.0 → 2.0.0
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.
- checksums.yaml +4 -4
- data/lib/gosling/actor.rb +234 -54
- data/lib/gosling/circle.rb +27 -5
- data/lib/gosling/collision.rb +55 -20
- data/lib/gosling/image_library.rb +7 -0
- data/lib/gosling/patches.rb +42 -0
- data/lib/gosling/polygon.rb +39 -11
- data/lib/gosling/rect.rb +13 -4
- data/lib/gosling/sprite.rb +14 -0
- data/lib/gosling/transform.rb +264 -163
- data/lib/gosling/utils.rb +20 -0
- data/lib/gosling/version.rb +5 -1
- data/spec/actor_spec.rb +47 -44
- data/spec/circle_spec.rb +2 -3
- data/spec/collision_spec.rb +207 -206
- data/spec/polygon_spec.rb +36 -36
- data/spec/rect_spec.rb +12 -12
- data/spec/transform_spec.rb +138 -149
- metadata +18 -2
@@ -0,0 +1,20 @@
|
|
1
|
+
##
|
2
|
+
# Raises an ArgumentError unless the object is of the specified type.
|
3
|
+
#
|
4
|
+
def type_check(obj, type)
|
5
|
+
raise ArgumentError.new("Expected #{type}, but received #{obj.inspect}!") unless obj.is_a?(type)
|
6
|
+
end
|
7
|
+
|
8
|
+
##
|
9
|
+
# Raises an ArgumentError unless the object is one of the listed types.
|
10
|
+
#
|
11
|
+
def types_check(obj, *types)
|
12
|
+
raise ArgumentError.new("Expected one of #{types.inspect}, but received #{obj.inspect}!") unless types.any? { |type| obj.is_a?(type) }
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Raises an ArgumentError unless the object is truthy.
|
17
|
+
#
|
18
|
+
def boolean_check(obj)
|
19
|
+
raise ArgumentError.new("Expected true or false, but received #{obj.inspect}!") unless [true, false].include?(obj)
|
20
|
+
end
|
data/lib/gosling/version.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
#--
|
2
|
+
# MAJOR version when you make incompatible changes,
|
3
|
+
# MINOR version when you add functionality in a backwards-compatible manner, and
|
4
|
+
# PATCH version when you make backwards-compatible bug fixes.
|
1
5
|
module Gosling
|
2
|
-
VERSION = '
|
6
|
+
VERSION = '2.0.0'
|
3
7
|
end
|
data/spec/actor_spec.rb
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
module Gosling
|
2
|
+
class Actor
|
3
|
+
public :render
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
1
7
|
describe Gosling::Actor do
|
2
8
|
before(:all) do
|
3
9
|
@window = Gosu::Window.new(640, 480, false)
|
@@ -186,14 +192,14 @@ describe Gosling::Actor do
|
|
186
192
|
|
187
193
|
describe '#render' do
|
188
194
|
it 'the method exists' do
|
189
|
-
expect { @read_only_actor.render(
|
195
|
+
expect { @read_only_actor.render(Snow::Mat3.new) }.not_to raise_error
|
190
196
|
end
|
191
197
|
end
|
192
198
|
|
193
199
|
describe '#draw' do
|
194
200
|
before(:all) do
|
195
201
|
@draw_actor = Gosling::Actor.new(@window)
|
196
|
-
@mat =
|
202
|
+
@mat = Snow::Mat3.new
|
197
203
|
end
|
198
204
|
|
199
205
|
context 'when visible' do
|
@@ -237,15 +243,15 @@ describe Gosling::Actor do
|
|
237
243
|
end
|
238
244
|
|
239
245
|
it 'passes its children a comprehensive transformation matrix' do
|
240
|
-
parameter_mat =
|
241
|
-
|
242
|
-
|
243
|
-
|
246
|
+
parameter_mat = Snow::Mat3[
|
247
|
+
1, 0, 10,
|
248
|
+
0, 1, 20,
|
249
|
+
0, 0, 1
|
244
250
|
]
|
245
|
-
self_mat =
|
246
|
-
|
247
|
-
|
248
|
-
|
251
|
+
self_mat = Snow::Mat3[
|
252
|
+
2, 0, 0,
|
253
|
+
0, 3, 0,
|
254
|
+
0, 0, 1
|
249
255
|
]
|
250
256
|
result_mat = parameter_mat * self_mat
|
251
257
|
|
@@ -264,7 +270,7 @@ describe Gosling::Actor do
|
|
264
270
|
|
265
271
|
describe '#is_point_in_bounds' do
|
266
272
|
it 'returns false' do
|
267
|
-
expect(@read_only_actor.is_point_in_bounds(
|
273
|
+
expect(@read_only_actor.is_point_in_bounds(Snow::Vec3[0,0,1])).to be false
|
268
274
|
end
|
269
275
|
end
|
270
276
|
|
@@ -277,7 +283,7 @@ describe Gosling::Actor do
|
|
277
283
|
it 'returns nil even if hit' do
|
278
284
|
@parent.is_tangible = false
|
279
285
|
allow(@parent).to receive(:is_point_in_bounds).and_return(true)
|
280
|
-
expect(@parent.get_actor_at(
|
286
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == nil
|
281
287
|
@parent.is_tangible = true
|
282
288
|
end
|
283
289
|
end
|
@@ -285,12 +291,12 @@ describe Gosling::Actor do
|
|
285
291
|
context 'with no children' do
|
286
292
|
it 'returns itself if the point is within its bounds' do
|
287
293
|
allow(@parent).to receive(:is_point_in_bounds).and_return(true)
|
288
|
-
expect(@parent.get_actor_at(
|
294
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == @parent
|
289
295
|
end
|
290
296
|
|
291
297
|
it 'returns nil if point is not within its bounds' do
|
292
298
|
allow(@parent).to receive(:is_point_in_bounds).and_return(false)
|
293
|
-
expect(@parent.get_actor_at(
|
299
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == nil
|
294
300
|
end
|
295
301
|
end
|
296
302
|
|
@@ -309,8 +315,8 @@ describe Gosling::Actor do
|
|
309
315
|
allow(@child1).to receive(:is_point_in_bounds).and_return(true)
|
310
316
|
allow(@child2).to receive(:is_point_in_bounds).and_return(true)
|
311
317
|
|
312
|
-
expect(@parent.get_actor_at(
|
313
|
-
expect(@parent.get_actor_at(
|
318
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == nil
|
319
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == @parent
|
314
320
|
@parent.are_children_tangible = true
|
315
321
|
end
|
316
322
|
end
|
@@ -320,7 +326,7 @@ describe Gosling::Actor do
|
|
320
326
|
allow(@child1).to receive(:is_point_in_bounds).and_return(false, false, true, true)
|
321
327
|
allow(@child2).to receive(:is_point_in_bounds).and_return(true)
|
322
328
|
|
323
|
-
4.times { expect(@parent.get_actor_at(
|
329
|
+
4.times { expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == @child2 }
|
324
330
|
end
|
325
331
|
|
326
332
|
it "returns the first child if the second was not hit" do
|
@@ -328,7 +334,7 @@ describe Gosling::Actor do
|
|
328
334
|
allow(@child1).to receive(:is_point_in_bounds).and_return(true)
|
329
335
|
allow(@child2).to receive(:is_point_in_bounds).and_return(false)
|
330
336
|
|
331
|
-
2.times { expect(@parent.get_actor_at(
|
337
|
+
2.times { expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == @child1 }
|
332
338
|
end
|
333
339
|
|
334
340
|
it "returns itself if neither child was hit" do
|
@@ -336,7 +342,7 @@ describe Gosling::Actor do
|
|
336
342
|
allow(@child1).to receive(:is_point_in_bounds).and_return(false)
|
337
343
|
allow(@child2).to receive(:is_point_in_bounds).and_return(false)
|
338
344
|
|
339
|
-
expect(@parent.get_actor_at(
|
345
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == @parent
|
340
346
|
end
|
341
347
|
|
342
348
|
it 'returns nil if point is not within it or its children' do
|
@@ -344,7 +350,7 @@ describe Gosling::Actor do
|
|
344
350
|
allow(@child1).to receive(:is_point_in_bounds).and_return(false)
|
345
351
|
allow(@child2).to receive(:is_point_in_bounds).and_return(false)
|
346
352
|
|
347
|
-
expect(@parent.get_actor_at(
|
353
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == nil
|
348
354
|
end
|
349
355
|
|
350
356
|
context 'with a mask child' do
|
@@ -354,7 +360,7 @@ describe Gosling::Actor do
|
|
354
360
|
allow(@child1).to receive(:is_point_in_bounds).and_return(true)
|
355
361
|
allow(@child2).to receive(:is_point_in_bounds).and_return(false)
|
356
362
|
|
357
|
-
expect(@parent.get_actor_at(
|
363
|
+
expect(@parent.get_actor_at(Snow::Vec3[0,0,1])).to be == @parent
|
358
364
|
@child1.is_mask = false
|
359
365
|
end
|
360
366
|
end
|
@@ -374,7 +380,7 @@ describe Gosling::Actor do
|
|
374
380
|
it 'returns an empty array even if hit' do
|
375
381
|
@parent.is_tangible = false
|
376
382
|
allow(@parent).to receive(:is_point_in_bounds).and_return(true)
|
377
|
-
expect(@parent.get_actors_at(
|
383
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be_empty
|
378
384
|
@parent.is_tangible = true
|
379
385
|
end
|
380
386
|
end
|
@@ -382,12 +388,12 @@ describe Gosling::Actor do
|
|
382
388
|
context 'with no children' do
|
383
389
|
it 'returns an array containing itself if the point is within its bounds' do
|
384
390
|
allow(@parent).to receive(:is_point_in_bounds).and_return(true)
|
385
|
-
expect(@parent.get_actors_at(
|
391
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@parent]
|
386
392
|
end
|
387
393
|
|
388
394
|
it 'returns an empty array if point is not within its bounds' do
|
389
395
|
allow(@parent).to receive(:is_point_in_bounds).and_return(false)
|
390
|
-
expect(@parent.get_actors_at(
|
396
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be_empty
|
391
397
|
end
|
392
398
|
end
|
393
399
|
|
@@ -406,8 +412,8 @@ describe Gosling::Actor do
|
|
406
412
|
allow(@child1).to receive(:is_point_in_bounds).and_return(true)
|
407
413
|
allow(@child2).to receive(:is_point_in_bounds).and_return(true, false)
|
408
414
|
|
409
|
-
expect(@parent.get_actors_at(
|
410
|
-
expect(@parent.get_actors_at(
|
415
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == []
|
416
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@parent]
|
411
417
|
@parent.are_children_tangible = true
|
412
418
|
end
|
413
419
|
end
|
@@ -417,14 +423,14 @@ describe Gosling::Actor do
|
|
417
423
|
allow(@child1).to receive(:is_point_in_bounds).and_return(false, false, true, true, false, false, true, true)
|
418
424
|
allow(@child2).to receive(:is_point_in_bounds).and_return(false, false, false, false, true, true, true, true)
|
419
425
|
|
420
|
-
expect(@parent.get_actors_at(
|
421
|
-
expect(@parent.get_actors_at(
|
422
|
-
expect(@parent.get_actors_at(
|
423
|
-
expect(@parent.get_actors_at(
|
424
|
-
expect(@parent.get_actors_at(
|
425
|
-
expect(@parent.get_actors_at(
|
426
|
-
expect(@parent.get_actors_at(
|
427
|
-
expect(@parent.get_actors_at(
|
426
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == []
|
427
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@parent]
|
428
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@child1]
|
429
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@child1, @parent]
|
430
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@child2]
|
431
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@child2, @parent]
|
432
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@child2, @child1]
|
433
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@child2, @child1, @parent]
|
428
434
|
end
|
429
435
|
|
430
436
|
context 'with a mask child' do
|
@@ -437,7 +443,7 @@ describe Gosling::Actor do
|
|
437
443
|
allow(@child1).to receive(:is_point_in_bounds).and_return(true)
|
438
444
|
allow(@child2).to receive(:is_point_in_bounds).and_return(false)
|
439
445
|
|
440
|
-
expect(@parent.get_actors_at(
|
446
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@parent]
|
441
447
|
end
|
442
448
|
|
443
449
|
it 'returns the parent only once if both children are masks and are hit' do
|
@@ -446,7 +452,7 @@ describe Gosling::Actor do
|
|
446
452
|
allow(@child1).to receive(:is_point_in_bounds).and_return(true)
|
447
453
|
allow(@child2).to receive(:is_point_in_bounds).and_return(true)
|
448
454
|
|
449
|
-
expect(@parent.get_actors_at(
|
455
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@parent]
|
450
456
|
@child2.is_mask = false
|
451
457
|
end
|
452
458
|
|
@@ -455,7 +461,7 @@ describe Gosling::Actor do
|
|
455
461
|
allow(@child1).to receive(:is_point_in_bounds).and_return(true)
|
456
462
|
allow(@child2).to receive(:is_point_in_bounds).and_return(false)
|
457
463
|
|
458
|
-
expect(@parent.get_actors_at(
|
464
|
+
expect(@parent.get_actors_at(Snow::Vec3[0,0,1])).to be == [@parent]
|
459
465
|
end
|
460
466
|
|
461
467
|
after(:all) do
|
@@ -472,9 +478,7 @@ describe Gosling::Actor do
|
|
472
478
|
describe '#get_global_transform' do
|
473
479
|
it 'returns a 3x3 matrix' do
|
474
480
|
result = @parent.get_global_transform
|
475
|
-
expect(result).to be_instance_of(
|
476
|
-
expect(result.row_size).to be == 3
|
477
|
-
expect(result.column_size).to be == 3
|
481
|
+
expect(result).to be_instance_of(Snow::Mat3)
|
478
482
|
end
|
479
483
|
|
480
484
|
it 'is a composite of its own transform plus all of its ancestors' do
|
@@ -498,7 +502,7 @@ describe Gosling::Actor do
|
|
498
502
|
rotated_view.add_child(translated_view)
|
499
503
|
translated_view.add_child(@parent)
|
500
504
|
|
501
|
-
expected =
|
505
|
+
expected = @parent.transform.to_matrix * translated_view.transform.to_matrix * rotated_view.transform.to_matrix * scaled_view.transform.to_matrix * centered_view.transform.to_matrix
|
502
506
|
|
503
507
|
result = @parent.get_global_transform
|
504
508
|
expect(result).to be == expected
|
@@ -510,8 +514,7 @@ describe Gosling::Actor do
|
|
510
514
|
describe '#get_global_position' do
|
511
515
|
it 'returns a 3d vector' do
|
512
516
|
result = @parent.get_global_position
|
513
|
-
expect(result).to be_instance_of(
|
514
|
-
expect(result.size).to be == 3
|
517
|
+
expect(result).to be_instance_of(Snow::Vec3)
|
515
518
|
end
|
516
519
|
|
517
520
|
context 'with a long ancestry' do
|
@@ -555,7 +558,7 @@ describe Gosling::Actor do
|
|
555
558
|
|
556
559
|
it 'respects all ancestors' do
|
557
560
|
result = @parent.get_global_position
|
558
|
-
expect(result).to be ==
|
561
|
+
expect(result).to be == Snow::Vec3[(0 + 10) * 3 - 10, (0 - 50) * -2 - 2, 0]
|
559
562
|
end
|
560
563
|
|
561
564
|
after do
|
data/spec/circle_spec.rb
CHANGED
@@ -28,15 +28,14 @@ describe Gosling::Circle do
|
|
28
28
|
|
29
29
|
it 'returns a size three vector' do
|
30
30
|
result = @circle.get_point_at_angle(Math::PI)
|
31
|
-
expect(result).to be_instance_of(
|
32
|
-
expect(result.size).to be == 3
|
31
|
+
expect(result).to be_instance_of(Snow::Vec3)
|
33
32
|
end
|
34
33
|
|
35
34
|
it 'returns a point on this circle in local-space' do
|
36
35
|
@circle.radius = 7
|
37
36
|
|
38
37
|
angles = (0...16).map { |x| Math::PI * x / 8 }
|
39
|
-
unit_vectors = angles.map { |a|
|
38
|
+
unit_vectors = angles.map { |a| Snow::Vec3[Math.cos(a), Math.sin(a), 0] }
|
40
39
|
|
41
40
|
angles.each_index do |i|
|
42
41
|
angle = angles[i]
|
data/spec/collision_spec.rb
CHANGED
@@ -60,16 +60,16 @@ describe Gosling::Collision do
|
|
60
60
|
|
61
61
|
@polygon1 = Gosling::Polygon.new(@window)
|
62
62
|
@polygon1.set_vertices([
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
Snow::Vec3[ 0, 5, 0],
|
64
|
+
Snow::Vec3[ 5, -5, 0],
|
65
|
+
Snow::Vec3[-5, -5, 0]
|
66
66
|
])
|
67
67
|
|
68
68
|
@polygon2 = Gosling::Polygon.new(@window)
|
69
69
|
@polygon2.set_vertices([
|
70
|
-
|
71
|
-
|
72
|
-
|
70
|
+
Snow::Vec3[ 0, -5, 0],
|
71
|
+
Snow::Vec3[ 5, 5, 0],
|
72
|
+
Snow::Vec3[-5, 5, 0]
|
73
73
|
])
|
74
74
|
|
75
75
|
@rect1 = Gosling::Rect.new(@window)
|
@@ -413,198 +413,197 @@ describe Gosling::Collision do
|
|
413
413
|
|
414
414
|
describe '#is_point_in_shape?' do
|
415
415
|
it 'expects a point and an actor' do
|
416
|
-
expect { Gosling::Collision.is_point_in_shape?(
|
416
|
+
expect { Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 0, 0], @actor1) }.not_to raise_error
|
417
417
|
|
418
|
-
expect { Gosling::Collision.is_point_in_shape?(@actor1,
|
418
|
+
expect { Gosling::Collision.is_point_in_shape?(@actor1, Snow::Vec3[0, 0, 0]) }.to raise_error(ArgumentError)
|
419
419
|
expect { Gosling::Collision.is_point_in_shape?(@actor1, :foo) }.to raise_error(ArgumentError)
|
420
|
-
expect { Gosling::Collision.is_point_in_shape?(:bar,
|
420
|
+
expect { Gosling::Collision.is_point_in_shape?(:bar, Snow::Vec3[0, 0, 0]) }.to raise_error(ArgumentError)
|
421
421
|
expect { Gosling::Collision.is_point_in_shape?() }.to raise_error(ArgumentError)
|
422
422
|
end
|
423
423
|
|
424
424
|
context 'point vs. actor' do
|
425
425
|
it 'never collides' do
|
426
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
426
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 0, 0], @actor1)).to be false
|
427
427
|
end
|
428
428
|
end
|
429
429
|
|
430
430
|
context 'point vs. circle' do
|
431
431
|
it 'returns true if point is in shape' do
|
432
432
|
clean_shape(@circle1)
|
433
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
434
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
435
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
436
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
437
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
438
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
439
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
440
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
441
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
433
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 0, 0], @circle1)).to be true
|
434
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, 0, 0], @circle1)).to be true
|
435
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, 0, 0], @circle1)).to be true
|
436
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 4, 0], @circle1)).to be true
|
437
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -4, 0], @circle1)).to be true
|
438
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[5, 0, 0], @circle1)).to be true
|
439
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-5, 0, 0], @circle1)).to be true
|
440
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 5, 0], @circle1)).to be true
|
441
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -5, 0], @circle1)).to be true
|
442
442
|
end
|
443
443
|
|
444
444
|
it 'returns false if point is not in shape' do
|
445
445
|
clean_shape(@circle1)
|
446
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
447
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
448
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
449
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
450
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
451
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
452
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
453
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
446
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[6, 0, 0], @circle1)).to be false
|
447
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-6, 0, 0], @circle1)).to be false
|
448
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 6, 0], @circle1)).to be false
|
449
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -6, 0], @circle1)).to be false
|
450
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, 4, 0], @circle1)).to be false
|
451
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, 4, 0], @circle1)).to be false
|
452
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, -4, 0], @circle1)).to be false
|
453
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, -4, 0], @circle1)).to be false
|
454
454
|
end
|
455
455
|
end
|
456
456
|
|
457
457
|
context 'point vs. polygon' do
|
458
458
|
it 'returns true if point is in shape' do
|
459
459
|
clean_shape(@polygon1)
|
460
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
461
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
462
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
463
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
464
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
465
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
466
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
467
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
468
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
460
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 0, 0], @polygon1)).to be true
|
461
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 4, 0], @polygon1)).to be true
|
462
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -4, 0], @polygon1)).to be true
|
463
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, -4, 0], @polygon1)).to be true
|
464
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, -4, 0], @polygon1)).to be true
|
465
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 5, 0], @polygon1)).to be true
|
466
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -5, 0], @polygon1)).to be true
|
467
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[5, -5, 0], @polygon1)).to be true
|
468
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-5, -5, 0], @polygon1)).to be true
|
469
469
|
end
|
470
470
|
|
471
471
|
it 'returns false if point is not in shape' do
|
472
472
|
clean_shape(@polygon1)
|
473
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
474
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
475
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
476
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
477
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
478
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
473
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 6, 0], @polygon1)).to be false
|
474
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -6, 0], @polygon1)).to be false
|
475
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[6, -6, 0], @polygon1)).to be false
|
476
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-6, -6, 0], @polygon1)).to be false
|
477
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, 4, 0], @polygon1)).to be false
|
478
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, 4, 0], @polygon1)).to be false
|
479
479
|
end
|
480
480
|
end
|
481
481
|
|
482
482
|
context 'point vs. rect' do
|
483
483
|
it 'returns true if point is in shape' do
|
484
484
|
clean_rect(@rect1)
|
485
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
486
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
487
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
488
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
489
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
490
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
491
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
492
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
493
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
494
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
495
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
496
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
497
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
498
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
499
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
500
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
501
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
485
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 0, 0], @rect1)).to be true
|
486
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, -4, 0], @rect1)).to be true
|
487
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -4, 0], @rect1)).to be true
|
488
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, -4, 0], @rect1)).to be true
|
489
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, 0, 0], @rect1)).to be true
|
490
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[4, 4, 0], @rect1)).to be true
|
491
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 4, 0], @rect1)).to be true
|
492
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, 4, 0], @rect1)).to be true
|
493
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-4, 0, 0], @rect1)).to be true
|
494
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-5, -5, 0], @rect1)).to be true
|
495
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -5, 0], @rect1)).to be true
|
496
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[5, -5, 0], @rect1)).to be true
|
497
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[5, 0, 0], @rect1)).to be true
|
498
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[5, 5, 0], @rect1)).to be true
|
499
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 5, 0], @rect1)).to be true
|
500
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-5, 5, 0], @rect1)).to be true
|
501
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-5, 0, 0], @rect1)).to be true
|
502
502
|
end
|
503
503
|
|
504
504
|
it 'returns false if point is not in shape' do
|
505
505
|
clean_rect(@rect1)
|
506
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
507
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
508
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
509
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
510
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
511
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
512
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
513
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
506
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-6, -6, 0], @rect1)).to be false
|
507
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -6, 0], @rect1)).to be false
|
508
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[6, -6, 0], @rect1)).to be false
|
509
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[6, 0, 0], @rect1)).to be false
|
510
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[6, 6, 0], @rect1)).to be false
|
511
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 6, 0], @rect1)).to be false
|
512
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-6, 6, 0], @rect1)).to be false
|
513
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-6, 0, 0], @rect1)).to be false
|
514
514
|
end
|
515
515
|
end
|
516
516
|
|
517
517
|
context 'point vs. sprite' do
|
518
518
|
it 'returns true if point is in shape' do
|
519
519
|
clean_sprite(@sprite1)
|
520
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
521
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
522
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
523
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
524
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
525
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
526
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
527
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
528
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
529
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
530
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
531
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
532
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
533
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
534
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
535
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
536
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
520
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 0, 0], @sprite1)).to be true
|
521
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-7, -7, 0], @sprite1)).to be true
|
522
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -7, 0], @sprite1)).to be true
|
523
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[7, -7, 0], @sprite1)).to be true
|
524
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[7, 0, 0], @sprite1)).to be true
|
525
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[7, 7, 0], @sprite1)).to be true
|
526
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 7, 0], @sprite1)).to be true
|
527
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-7, 7, 0], @sprite1)).to be true
|
528
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-7, 0, 0], @sprite1)).to be true
|
529
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-8, -8, 0], @sprite1)).to be true
|
530
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -8, 0], @sprite1)).to be true
|
531
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[8, -8, 0], @sprite1)).to be true
|
532
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[8, 0, 0], @sprite1)).to be true
|
533
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[8, 8, 0], @sprite1)).to be true
|
534
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 8, 0], @sprite1)).to be true
|
535
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-8, 8, 0], @sprite1)).to be true
|
536
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-8, 0, 0], @sprite1)).to be true
|
537
537
|
end
|
538
538
|
|
539
539
|
it 'returns false if point is not in shape' do
|
540
540
|
clean_sprite(@sprite1)
|
541
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
542
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
543
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
544
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
545
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
546
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
547
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
548
|
-
expect(Gosling::Collision.is_point_in_shape?(
|
541
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-9, -9, 0], @sprite1)).to be false
|
542
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, -9, 0], @sprite1)).to be false
|
543
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[9, -9, 0], @sprite1)).to be false
|
544
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[9, 0, 0], @sprite1)).to be false
|
545
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[9, 9, 0], @sprite1)).to be false
|
546
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[0, 9, 0], @sprite1)).to be false
|
547
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-9, 9, 0], @sprite1)).to be false
|
548
|
+
expect(Gosling::Collision.is_point_in_shape?(Snow::Vec3[-9, 0, 0], @sprite1)).to be false
|
549
549
|
end
|
550
550
|
end
|
551
551
|
end
|
552
552
|
|
553
553
|
describe '#get_normal' do
|
554
554
|
it 'expects a 3d vector' do
|
555
|
-
expect { Gosling::Collision.get_normal(
|
556
|
-
expect { Gosling::Collision.get_normal(
|
557
|
-
expect { Gosling::Collision.get_normal(
|
555
|
+
expect { Gosling::Collision.get_normal(Snow::Vec3[1, 0, 0]) }.not_to raise_error
|
556
|
+
expect { Gosling::Collision.get_normal(Snow::Vec3[1, 0, 1, 0]) }.to raise_error(ArgumentError)
|
557
|
+
expect { Gosling::Collision.get_normal(Snow::Vec3[1, 0]) }.to raise_error(ArgumentError)
|
558
558
|
expect { Gosling::Collision.get_normal(:foo) }.to raise_error(ArgumentError)
|
559
559
|
expect { Gosling::Collision.get_normal(nil) }.to raise_error(ArgumentError)
|
560
560
|
end
|
561
561
|
|
562
562
|
it 'returns a 3d vector' do
|
563
|
-
result = Gosling::Collision.get_normal(
|
564
|
-
expect(result).to be_instance_of(
|
565
|
-
expect(result.size).to be == 3
|
563
|
+
result = Gosling::Collision.get_normal(Snow::Vec3[1, 0, 0])
|
564
|
+
expect(result).to be_instance_of(Snow::Vec3)
|
566
565
|
end
|
567
566
|
|
568
567
|
it 'z value of returned vector is always 0' do
|
569
568
|
[
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
569
|
+
Snow::Vec3[1, 1, 0],
|
570
|
+
Snow::Vec3[-1, -1, -1],
|
571
|
+
Snow::Vec3[-22, -22, 0],
|
572
|
+
Snow::Vec3[-11, 13, 34],
|
573
|
+
Snow::Vec3[37, -4, -15],
|
574
|
+
Snow::Vec3[34, 39, -16],
|
575
|
+
Snow::Vec3[-48, 23, -32],
|
576
|
+
Snow::Vec3[24, -39, 42],
|
577
|
+
Snow::Vec3[49, 44, -15],
|
578
|
+
Snow::Vec3[27, 23, 42],
|
579
|
+
Snow::Vec3[33, -25, -20],
|
580
|
+
Snow::Vec3[-46, -18, 48],
|
582
581
|
].each do |v|
|
583
582
|
expect(Gosling::Collision.get_normal(v)[2]).to be == 0
|
584
583
|
end
|
585
584
|
end
|
586
585
|
|
587
586
|
it 'raises an error when given a zero length vector' do
|
588
|
-
expect { Gosling::Collision.get_normal(
|
587
|
+
expect { Gosling::Collision.get_normal(Snow::Vec3[0, 0, 0]) }.to raise_error(ArgumentError)
|
589
588
|
end
|
590
589
|
|
591
590
|
it 'returns a vector that is +/- 90 degrees from the original' do
|
592
591
|
[
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
592
|
+
Snow::Vec3[1, 1, 0],
|
593
|
+
Snow::Vec3[-1, -1, -1],
|
594
|
+
Snow::Vec3[-22, -22, 0],
|
595
|
+
Snow::Vec3[-11, 13, 34],
|
596
|
+
Snow::Vec3[37, -4, -15],
|
597
|
+
Snow::Vec3[34, 39, -16],
|
598
|
+
Snow::Vec3[-48, 23, -32],
|
599
|
+
Snow::Vec3[24, -39, 42],
|
600
|
+
Snow::Vec3[49, 44, -15],
|
601
|
+
Snow::Vec3[27, 23, 42],
|
602
|
+
Snow::Vec3[33, -25, -20],
|
603
|
+
Snow::Vec3[-46, -18, 48],
|
605
604
|
].each do |v|
|
606
605
|
norm_v = Gosling::Collision.get_normal(v)
|
607
|
-
radians = Math.acos(v.
|
606
|
+
radians = Math.acos(v.dot_product(norm_v) / (v.magnitude * norm_v.magnitude))
|
608
607
|
expect(radians.abs).to be == Math::PI / 2
|
609
608
|
end
|
610
609
|
end
|
@@ -613,18 +612,18 @@ describe Gosling::Collision do
|
|
613
612
|
describe '#get_polygon_separation_axes' do
|
614
613
|
it 'expects an array of length 3 vectors' do
|
615
614
|
good_vector_array = [
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
615
|
+
Snow::Vec3[3, 1, 0],
|
616
|
+
Snow::Vec3[4, 2, 0],
|
617
|
+
Snow::Vec3[5, 3, 0],
|
618
|
+
Snow::Vec3[1, 4, 0],
|
619
|
+
Snow::Vec3[2, 5, 0]
|
621
620
|
]
|
622
621
|
bad_vector_array = [
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
622
|
+
Snow::Vec2[9, 11],
|
623
|
+
Snow::Vec3[7, 12, 0],
|
624
|
+
Snow::Vec4[5, 13, 1, 0],
|
625
|
+
Snow::Vec2[3, 14],
|
626
|
+
Snow::Vec2[1, 15]
|
628
627
|
]
|
629
628
|
p = Gosling::Polygon.new(@window)
|
630
629
|
expect { Gosling::Collision.get_polygon_separation_axes(good_vector_array) }.not_to raise_error
|
@@ -636,24 +635,24 @@ describe Gosling::Collision do
|
|
636
635
|
|
637
636
|
it 'returns an array of 3d vectors' do
|
638
637
|
vertices = [
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
638
|
+
Snow::Vec3[3, 1, 0],
|
639
|
+
Snow::Vec3[4, 2, 0],
|
640
|
+
Snow::Vec3[5, 3, 0],
|
641
|
+
Snow::Vec3[1, 4, 0],
|
642
|
+
Snow::Vec3[2, 5, 0]
|
644
643
|
]
|
645
644
|
result = Gosling::Collision.get_polygon_separation_axes(vertices)
|
646
645
|
expect(result).to be_instance_of(Array)
|
647
|
-
expect(result.reject { |v| v.is_a?(
|
646
|
+
expect(result.reject { |v| v.is_a?(Snow::Vec3) }).to be_empty
|
648
647
|
end
|
649
648
|
|
650
649
|
it 'skips length zero sides' do
|
651
650
|
vertices = [
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
651
|
+
Snow::Vec3[1, 1, 0],
|
652
|
+
Snow::Vec3[1, 1, 0],
|
653
|
+
Snow::Vec3[1, 2, 0],
|
654
|
+
Snow::Vec3[2, 2, 0],
|
655
|
+
Snow::Vec3[2, 2, 0]
|
657
656
|
]
|
658
657
|
result = Gosling::Collision.get_polygon_separation_axes(vertices)
|
659
658
|
expect(result.length).to be == 3
|
@@ -661,19 +660,19 @@ describe Gosling::Collision do
|
|
661
660
|
|
662
661
|
it 'returns correct values' do
|
663
662
|
vertices = [
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
663
|
+
Snow::Vec3[ 2, 1, 0],
|
664
|
+
Snow::Vec3[ 1, -1, 0],
|
665
|
+
Snow::Vec3[ 0, -2, 0],
|
666
|
+
Snow::Vec3[-1, -1, 0],
|
667
|
+
Snow::Vec3[-1, 2, 0]
|
669
668
|
]
|
670
669
|
result = Gosling::Collision.get_polygon_separation_axes(vertices)
|
671
670
|
expect(result.length).to be == 5
|
672
|
-
expect(result[0]).to be ==
|
673
|
-
expect(result[1]).to be ==
|
674
|
-
expect(result[2]).to be ==
|
675
|
-
expect(result[3]).to be ==
|
676
|
-
expect(result[4]).to be ==
|
671
|
+
expect(result[0]).to be == Snow::Vec3[ 2, -1, 0].normalize
|
672
|
+
expect(result[1]).to be == Snow::Vec3[ 1, -1, 0].normalize
|
673
|
+
expect(result[2]).to be == Snow::Vec3[-1, -1, 0].normalize
|
674
|
+
expect(result[3]).to be == Snow::Vec3[-3, 0, 0].normalize
|
675
|
+
expect(result[4]).to be == Snow::Vec3[ 1, 3, 0].normalize
|
677
676
|
end
|
678
677
|
end
|
679
678
|
|
@@ -700,8 +699,7 @@ describe Gosling::Collision do
|
|
700
699
|
@circle2.y = -5
|
701
700
|
|
702
701
|
result = Gosling::Collision.get_circle_separation_axis(@circle1, @circle2)
|
703
|
-
expect(result).to be_instance_of(
|
704
|
-
expect(result.size).to be == 3
|
702
|
+
expect(result).to be_instance_of(Snow::Vec3)
|
705
703
|
end
|
706
704
|
|
707
705
|
it "returns nil if distance beween shape centers is 0" do
|
@@ -727,7 +725,7 @@ describe Gosling::Collision do
|
|
727
725
|
@circle2.y = -5
|
728
726
|
|
729
727
|
result = Gosling::Collision.get_circle_separation_axis(@circle1, @circle2)
|
730
|
-
expect(result).to be ==
|
728
|
+
expect(result).to be == Snow::Vec3[1, 1, 0].normalize
|
731
729
|
end
|
732
730
|
end
|
733
731
|
|
@@ -750,19 +748,22 @@ describe Gosling::Collision do
|
|
750
748
|
it 'returns an array of 3d vectors' do
|
751
749
|
result = Gosling::Collision.get_separation_axes(@polygon1, @polygon2)
|
752
750
|
expect(result).to be_instance_of(Array)
|
753
|
-
expect(result.reject { |v| v.is_a?(
|
751
|
+
expect(result.reject { |v| v.is_a?(Snow::Vec3) }).to be_empty
|
754
752
|
end
|
755
753
|
|
756
754
|
it 'returns only unit vectors' do
|
757
755
|
result = Gosling::Collision.get_separation_axes(@polygon1, @circle1)
|
758
756
|
expect(result).to be_instance_of(Array)
|
759
|
-
|
757
|
+
result.each do |v|
|
758
|
+
expect(v).to be_instance_of(Snow::Vec3)
|
759
|
+
expect(v.magnitude).to be_between(0.99999999, 1.00000001)
|
760
|
+
end
|
760
761
|
end
|
761
762
|
|
762
763
|
it 'returns only right-facing (positive x direction) vectors' do
|
763
764
|
result = Gosling::Collision.get_separation_axes(@rect2, @polygon1)
|
764
765
|
expect(result).to be_instance_of(Array)
|
765
|
-
expect(result.reject { |v| v.is_a?(
|
766
|
+
expect(result.reject { |v| v.is_a?(Snow::Vec3) && v[0] >= 0 }).to be_empty
|
766
767
|
end
|
767
768
|
|
768
769
|
it 'returns only unique vectors' do
|
@@ -774,7 +775,7 @@ describe Gosling::Collision do
|
|
774
775
|
it 'is commutative' do
|
775
776
|
result1 = Gosling::Collision.get_separation_axes(@rect2, @polygon2)
|
776
777
|
result2 = Gosling::Collision.get_separation_axes(@polygon2, @rect2)
|
777
|
-
expect(result1.
|
778
|
+
expect(result1.map { |x| x.to_s }.sort).to be == result2.map { |x| x.to_s }.sort
|
778
779
|
end
|
779
780
|
|
780
781
|
it 'respects centering' do
|
@@ -786,9 +787,9 @@ describe Gosling::Collision do
|
|
786
787
|
|
787
788
|
result = Gosling::Collision.get_separation_axes(@polygon1, @circle1)
|
788
789
|
expect(result).to be == [
|
789
|
-
|
790
|
-
|
791
|
-
|
790
|
+
Snow::Vec3[10, 5, 0].normalize,
|
791
|
+
Snow::Vec3[0, -10, 0].normalize,
|
792
|
+
Snow::Vec3[10, -5, 0].normalize
|
792
793
|
]
|
793
794
|
end
|
794
795
|
|
@@ -801,9 +802,9 @@ describe Gosling::Collision do
|
|
801
802
|
|
802
803
|
result = Gosling::Collision.get_separation_axes(@polygon1, @circle1)
|
803
804
|
expect(result).to be == [
|
804
|
-
|
805
|
-
|
806
|
-
|
805
|
+
Snow::Vec3[20, 15, 0].normalize,
|
806
|
+
Snow::Vec3[0, -30, 0].normalize,
|
807
|
+
Snow::Vec3[20, -15, 0].normalize
|
807
808
|
]
|
808
809
|
end
|
809
810
|
|
@@ -815,9 +816,9 @@ describe Gosling::Collision do
|
|
815
816
|
clean_shape(@circle1)
|
816
817
|
|
817
818
|
expect(result).to be == [
|
818
|
-
|
819
|
-
|
820
|
-
|
819
|
+
Snow::Vec3[5, -10, 0].normalize,
|
820
|
+
Snow::Vec3[10, 0, 0].normalize,
|
821
|
+
Snow::Vec3[5, 10, 0].normalize
|
821
822
|
]
|
822
823
|
end
|
823
824
|
|
@@ -830,10 +831,10 @@ describe Gosling::Collision do
|
|
830
831
|
|
831
832
|
result = Gosling::Collision.get_separation_axes(@polygon1, @circle1)
|
832
833
|
expect(result).to be == [
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
834
|
+
Snow::Vec3[10, 5, 0].normalize,
|
835
|
+
Snow::Vec3[0, -10, 0].normalize,
|
836
|
+
Snow::Vec3[10, -5, 0].normalize,
|
837
|
+
Snow::Vec3[50, -10, 0].normalize
|
837
838
|
]
|
838
839
|
end
|
839
840
|
|
@@ -908,7 +909,7 @@ describe Gosling::Collision do
|
|
908
909
|
|
909
910
|
describe '#project_onto_axis' do
|
910
911
|
it 'expects a shape and a 3d unit vector' do
|
911
|
-
axis =
|
912
|
+
axis = Snow::Vec3[1, 1, 0]
|
912
913
|
|
913
914
|
expect { Gosling::Collision.project_onto_axis(@sprite1, axis) }.not_to raise_error
|
914
915
|
expect { Gosling::Collision.project_onto_axis(@rect1, axis) }.not_to raise_error
|
@@ -916,15 +917,15 @@ describe Gosling::Collision do
|
|
916
917
|
expect { Gosling::Collision.project_onto_axis(@polygon1, axis) }.not_to raise_error
|
917
918
|
|
918
919
|
expect { Gosling::Collision.project_onto_axis(:foo, axis) }.to raise_error(ArgumentError)
|
919
|
-
expect { Gosling::Collision.project_onto_axis(@sprite1,
|
920
|
-
expect { Gosling::Collision.project_onto_axis(@rect1,
|
920
|
+
expect { Gosling::Collision.project_onto_axis(@sprite1, Snow::Vec4[1, 1, 0, 2]) }.to raise_error(ArgumentError)
|
921
|
+
expect { Gosling::Collision.project_onto_axis(@rect1, Snow::Vec2[1, 1]) }.to raise_error(ArgumentError)
|
921
922
|
expect { Gosling::Collision.project_onto_axis(@polygon1, :foo) }.to raise_error(ArgumentError)
|
922
923
|
expect { Gosling::Collision.project_onto_axis(@circle1, @circle1, axis) }.to raise_error(ArgumentError)
|
923
924
|
expect { Gosling::Collision.project_onto_axis() }.to raise_error(ArgumentError)
|
924
925
|
end
|
925
926
|
|
926
927
|
it 'returns an array of two numbers' do
|
927
|
-
axis =
|
928
|
+
axis = Snow::Vec3[1, 1, 0]
|
928
929
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
929
930
|
expect(result).to be_instance_of(Array)
|
930
931
|
expect(result.length).to be == 2
|
@@ -938,7 +939,7 @@ describe Gosling::Collision do
|
|
938
939
|
@circle1.y = 0
|
939
940
|
@circle1.radius = 5
|
940
941
|
|
941
|
-
axis =
|
942
|
+
axis = Snow::Vec3[-1, 1, 0].normalize
|
942
943
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
943
944
|
expect(result).to be == [-5, 5]
|
944
945
|
|
@@ -947,7 +948,7 @@ describe Gosling::Collision do
|
|
947
948
|
@circle1.y = 0
|
948
949
|
@circle1.radius = 5
|
949
950
|
|
950
|
-
axis =
|
951
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
951
952
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
952
953
|
expect(result).to be == [0, 10]
|
953
954
|
end
|
@@ -957,11 +958,11 @@ describe Gosling::Collision do
|
|
957
958
|
@circle1.center_x = 3
|
958
959
|
@circle1.center_y = 6
|
959
960
|
|
960
|
-
axis =
|
961
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
961
962
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
962
963
|
expect(result).to be == [-8, 2]
|
963
964
|
|
964
|
-
axis =
|
965
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
965
966
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
966
967
|
expect(result).to be == [-11, -1]
|
967
968
|
end
|
@@ -971,11 +972,11 @@ describe Gosling::Collision do
|
|
971
972
|
@circle1.scale_x = 2
|
972
973
|
@circle1.scale_y = 0.5
|
973
974
|
|
974
|
-
axis =
|
975
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
975
976
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
976
977
|
expect(result).to be == [-10, 10]
|
977
978
|
|
978
|
-
axis =
|
979
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
979
980
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
980
981
|
expect(result).to be == [-2.5, 2.5]
|
981
982
|
end
|
@@ -984,7 +985,7 @@ describe Gosling::Collision do
|
|
984
985
|
clean_shape(@circle1)
|
985
986
|
@circle1.rotation = Math::PI
|
986
987
|
|
987
|
-
axis =
|
988
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
988
989
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
989
990
|
expect(result).to be == [-5, 5]
|
990
991
|
end
|
@@ -994,11 +995,11 @@ describe Gosling::Collision do
|
|
994
995
|
@circle1.x = -12
|
995
996
|
@circle1.y = 23
|
996
997
|
|
997
|
-
axis =
|
998
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
998
999
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
999
1000
|
expect(result).to be == [-17, -7]
|
1000
1001
|
|
1001
|
-
axis =
|
1002
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1002
1003
|
result = Gosling::Collision.project_onto_axis(@circle1, axis)
|
1003
1004
|
expect(result).to be == [18, 28]
|
1004
1005
|
end
|
@@ -1010,17 +1011,17 @@ describe Gosling::Collision do
|
|
1010
1011
|
|
1011
1012
|
create_inheritance_chain([@center_actor, @scale_actor, @rotate_actor, @translate_actor, circle])
|
1012
1013
|
|
1013
|
-
axis =
|
1014
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
1014
1015
|
result = Gosling::Collision.project_onto_axis(circle, axis)
|
1015
1016
|
expect(result).to be == [-936.0, -866.0]
|
1016
1017
|
|
1017
|
-
axis =
|
1018
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1018
1019
|
result = Gosling::Collision.project_onto_axis(circle, axis)
|
1019
1020
|
expect(result).to be == [290.0, 340.0]
|
1020
1021
|
|
1021
|
-
axis =
|
1022
|
+
axis = Snow::Vec3[1, 1, 0].normalize
|
1022
1023
|
result = Gosling::Collision.project_onto_axis(circle, axis)
|
1023
|
-
expect(result).to be == [-443.
|
1024
|
+
expect(result).to be == [-443.1343965537543, -385.5947509968793]
|
1024
1025
|
|
1025
1026
|
break_inheritance_chain([@center_actor, @scale_actor, @rotate_actor, @translate_actor, circle])
|
1026
1027
|
end
|
@@ -1028,21 +1029,21 @@ describe Gosling::Collision do
|
|
1028
1029
|
|
1029
1030
|
context 'with a polygon' do
|
1030
1031
|
it 'returns expected values' do
|
1031
|
-
axis =
|
1032
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
1032
1033
|
clean_shape(@polygon2)
|
1033
1034
|
@polygon2.x = 0
|
1034
1035
|
@polygon2.y = 0
|
1035
1036
|
result = Gosling::Collision.project_onto_axis(@polygon2, axis)
|
1036
1037
|
expect(result).to be == [-5, 5]
|
1037
1038
|
|
1038
|
-
axis =
|
1039
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1039
1040
|
clean_shape(@polygon1)
|
1040
1041
|
@polygon1.x = 0
|
1041
1042
|
@polygon1.y = 5
|
1042
1043
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1043
1044
|
expect(result).to be == [0, 10]
|
1044
1045
|
|
1045
|
-
axis =
|
1046
|
+
axis = Snow::Vec3[1, -1, 0].normalize
|
1046
1047
|
clean_shape(@polygon1)
|
1047
1048
|
@polygon1.x = 0
|
1048
1049
|
@polygon1.y = 0
|
@@ -1056,11 +1057,11 @@ describe Gosling::Collision do
|
|
1056
1057
|
@polygon1.center_x = 5
|
1057
1058
|
@polygon1.center_y = -1
|
1058
1059
|
|
1059
|
-
axis =
|
1060
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
1060
1061
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1061
1062
|
expect(result).to be == [-10, 0]
|
1062
1063
|
|
1063
|
-
axis =
|
1064
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1064
1065
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1065
1066
|
expect(result).to be == [-4, 6]
|
1066
1067
|
end
|
@@ -1070,11 +1071,11 @@ describe Gosling::Collision do
|
|
1070
1071
|
@polygon1.scale_x = 3
|
1071
1072
|
@polygon1.scale_y = 2
|
1072
1073
|
|
1073
|
-
axis =
|
1074
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
1074
1075
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1075
1076
|
expect(result).to be == [-15, 15]
|
1076
1077
|
|
1077
|
-
axis =
|
1078
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1078
1079
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1079
1080
|
expect(result).to be == [-10, 10]
|
1080
1081
|
end
|
@@ -1083,11 +1084,11 @@ describe Gosling::Collision do
|
|
1083
1084
|
clean_shape(@polygon1)
|
1084
1085
|
@polygon1.rotation = Math::PI / 4
|
1085
1086
|
|
1086
|
-
axis =
|
1087
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
1087
1088
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1088
1089
|
expect(result).to be == [-7.0710678118654755, 3.5355339059327373]
|
1089
1090
|
|
1090
|
-
axis =
|
1091
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1091
1092
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1092
1093
|
expect(result).to be == [-7.0710678118654755, 3.5355339059327378]
|
1093
1094
|
end
|
@@ -1097,11 +1098,11 @@ describe Gosling::Collision do
|
|
1097
1098
|
@polygon1.x = -7
|
1098
1099
|
@polygon1.y = 13
|
1099
1100
|
|
1100
|
-
axis =
|
1101
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
1101
1102
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1102
1103
|
expect(result).to be == [-12, -2]
|
1103
1104
|
|
1104
|
-
axis =
|
1105
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1105
1106
|
result = Gosling::Collision.project_onto_axis(@polygon1, axis)
|
1106
1107
|
expect(result).to be == [8, 18]
|
1107
1108
|
end
|
@@ -1113,15 +1114,15 @@ describe Gosling::Collision do
|
|
1113
1114
|
|
1114
1115
|
create_inheritance_chain([@center_actor, @scale_actor, @rotate_actor, @translate_actor, polygon])
|
1115
1116
|
|
1116
|
-
axis =
|
1117
|
+
axis = Snow::Vec3[1, 0, 0].normalize
|
1117
1118
|
result = Gosling::Collision.project_onto_axis(polygon, axis)
|
1118
1119
|
expect(result).to be == [-918.5, -883.5]
|
1119
1120
|
|
1120
|
-
axis =
|
1121
|
+
axis = Snow::Vec3[0, 1, 0].normalize
|
1121
1122
|
result = Gosling::Collision.project_onto_axis(polygon, axis)
|
1122
1123
|
expect(result).to be == [302.5, 327.5]
|
1123
1124
|
|
1124
|
-
axis =
|
1125
|
+
axis = Snow::Vec3[1, 1, 0].normalize
|
1125
1126
|
result = Gosling::Collision.project_onto_axis(polygon, axis)
|
1126
1127
|
expect(result).to be == [-426.7389424460814, -393.1513703397204]
|
1127
1128
|
|