|
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | import arcade |
| 6 | +from pyglet.math import Vec2 |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def test_sprites_at_point(): |
@@ -534,6 +535,145 @@ def test_gpu_collision_flipped_sprites(window, wall_scale, bullet_scale): |
534 | 535 | assert arcade.check_for_collision_with_list(bullet, walls, method=gpu) == [] |
535 | 536 |
|
536 | 537 |
|
| 538 | +def _reference_min_overlap(sprite1, sprite2): |
| 539 | + """Smallest overlap over the x and y axes and every edge normal, in pixels""" |
| 540 | + poly_a = sprite1.hit_box.get_adjusted_points() |
| 541 | + poly_b = sprite2.hit_box.get_adjusted_points() |
| 542 | + axes = [(1.0, 0.0), (0.0, 1.0)] |
| 543 | + for polygon in (poly_a, poly_b): |
| 544 | + for i in range(len(polygon)): |
| 545 | + (x1, y1), (x2, y2) = polygon[i], polygon[(i + 1) % len(polygon)] |
| 546 | + length = ((y2 - y1) ** 2 + (x1 - x2) ** 2) ** 0.5 |
| 547 | + if length: |
| 548 | + axes.append(((y2 - y1) / length, (x1 - x2) / length)) |
| 549 | + best = None |
| 550 | + for nx, ny in axes: |
| 551 | + projected_a = [nx * x + ny * y for x, y in poly_a] |
| 552 | + projected_b = [nx * x + ny * y for x, y in poly_b] |
| 553 | + overlap = min(max(projected_a) - min(projected_b), max(projected_b) - min(projected_a)) |
| 554 | + best = overlap if best is None else min(best, overlap) |
| 555 | + return best |
| 556 | + |
| 557 | + |
| 558 | +def _random_convex_sprite(rng, textures, shared_angle): |
| 559 | + sprite = arcade.Sprite(rng.choice(textures)) |
| 560 | + sprite.scale = (rng.choice([-1, 1]) * rng.choice([0.25, 0.5, 1, 1.5]), |
| 561 | + rng.choice([-1, 1]) * rng.choice([0.25, 0.5, 1, 1.5])) # fmt: skip |
| 562 | + if rng.random() < 0.5: |
| 563 | + sprite.angle = shared_angle |
| 564 | + else: |
| 565 | + sprite.angle = rng.choice([0, 0, 90, 180, 30, rng.uniform(0, 360)]) |
| 566 | + sprite.position = rng.randint(-160, 160) / 2, rng.randint(-160, 160) / 2 |
| 567 | + return sprite |
| 568 | + |
| 569 | + |
| 570 | +def test_get_collision_info_basics(window): |
| 571 | + a = arcade.SpriteSolidColor(10, 10) |
| 572 | + b = arcade.SpriteSolidColor(10, 10, center_x=8, center_y=1) |
| 573 | + # The smallest overlap is 2 pixels in x, so move a left |
| 574 | + assert arcade.get_collision_info(a, b) == (Vec2(-1.0, 0.0), 2.0) |
| 575 | + # Swapping the sprites flips the normal |
| 576 | + assert arcade.get_collision_info(b, a) == (Vec2(1.0, 0.0), 2.0) |
| 577 | + |
| 578 | + # Sinking into the floor: pushed straight up by exactly the overlap |
| 579 | + player = arcade.SpriteSolidColor(10, 10, center_x=20, center_y=3) |
| 580 | + floor = arcade.SpriteSolidColor(100, 10) |
| 581 | + info = arcade.get_collision_info(player, floor) |
| 582 | + assert info.normal == Vec2(0.0, 1.0) |
| 583 | + assert info.depth == 7.0 |
| 584 | + player.position += info.normal * info.depth |
| 585 | + assert player.position == (20, 10) |
| 586 | + assert arcade.check_for_collision(player, floor) is False |
| 587 | + |
| 588 | + # Identical sprites in the same place: moved up |
| 589 | + assert arcade.get_collision_info(a, arcade.SpriteSolidColor(10, 10)) == (Vec2(0.0, 1.0), 10.0) |
| 590 | + |
| 591 | + # Touching or apart: no collision |
| 592 | + assert arcade.get_collision_info(a, arcade.SpriteSolidColor(10, 10, center_x=10)) is None |
| 593 | + assert arcade.get_collision_info(a, arcade.SpriteSolidColor(10, 10, center_x=50)) is None |
| 594 | + |
| 595 | + |
| 596 | +def test_get_collision_info_diagonal(window): |
| 597 | + """A diagonal edge gives a diagonal normal""" |
| 598 | + box = arcade.SpriteSolidColor(20, 20) |
| 599 | + box.angle = 45 |
| 600 | + other = arcade.SpriteSolidColor(20, 20, center_x=12, center_y=12) |
| 601 | + info = arcade.get_collision_info(other, box) |
| 602 | + assert info.normal.x == pytest.approx(2**-0.5) |
| 603 | + assert info.normal.y == pytest.approx(2**-0.5) |
| 604 | + assert info.depth == pytest.approx(_reference_min_overlap(other, box)) |
| 605 | + |
| 606 | + |
| 607 | +def test_get_collision_info_type_errors(window): |
| 608 | + a = arcade.SpriteSolidColor(10, 10) |
| 609 | + with pytest.raises(TypeError): |
| 610 | + arcade.get_collision_info("moo", a) |
| 611 | + with pytest.raises(TypeError): |
| 612 | + arcade.get_collision_info(a, "moo") |
| 613 | + with pytest.raises(TypeError): |
| 614 | + arcade.get_collision_info(a, arcade.SpriteList()) |
| 615 | + |
| 616 | + |
| 617 | +def test_get_collision_info_matches_check_for_collision(window): |
| 618 | + """It finds a collision exactly when check_for_collision does, for any hit box""" |
| 619 | + rng = random.Random(77) |
| 620 | + textures = [ |
| 621 | + arcade.load_texture(":resources:images/tiles/grassMid.png"), |
| 622 | + arcade.load_texture(":resources:images/items/coinGold.png"), |
| 623 | + arcade.load_texture(":resources:images/space_shooter/laserBlue01.png"), |
| 624 | + arcade.load_texture( |
| 625 | + ":resources:images/space_shooter/meteorGrey_big1.png", |
| 626 | + hit_box_algorithm=arcade.hitbox.algo_detailed, |
| 627 | + ), |
| 628 | + ] |
| 629 | + results = {True: 0, False: 0} |
| 630 | + for _ in range(3000): |
| 631 | + shared_angle = rng.choice([0, 90, 180, 45, 30, rng.uniform(0, 360)]) |
| 632 | + a = _random_convex_sprite(rng, textures, shared_angle) |
| 633 | + b = _random_convex_sprite(rng, textures, shared_angle) |
| 634 | + expected = arcade.check_for_collision(a, b) |
| 635 | + assert (arcade.get_collision_info(a, b) is not None) is expected |
| 636 | + assert (arcade.get_collision_info(b, a) is not None) is expected |
| 637 | + results[expected] += 1 |
| 638 | + assert min(results.values()) > 300 |
| 639 | + |
| 640 | + |
| 641 | +def test_get_collision_info_separates(window): |
| 642 | + """For convex hit boxes, moving by normal * depth is the smallest move that separates them""" |
| 643 | + rng = random.Random(78) |
| 644 | + textures = [ |
| 645 | + arcade.load_texture(":resources:images/tiles/grassMid.png"), |
| 646 | + arcade.load_texture(":resources:images/items/coinGold.png"), |
| 647 | + arcade.load_texture(":resources:images/space_shooter/laserBlue01.png"), |
| 648 | + arcade.load_texture( |
| 649 | + ":resources:images/animated_characters/female_person/femalePerson_idle.png" |
| 650 | + ), |
| 651 | + ] |
| 652 | + checked = 0 |
| 653 | + for _ in range(3000): |
| 654 | + shared_angle = rng.choice([0, 90, 180, 45, 30, rng.uniform(0, 360)]) |
| 655 | + a = _random_convex_sprite(rng, textures, shared_angle) |
| 656 | + b = _random_convex_sprite(rng, textures, shared_angle) |
| 657 | + info = arcade.get_collision_info(a, b) |
| 658 | + if info is None: |
| 659 | + continue |
| 660 | + checked += 1 |
| 661 | + assert info.depth > 0 |
| 662 | + assert info.normal.length() == pytest.approx(1.0) |
| 663 | + assert info.depth == pytest.approx(_reference_min_overlap(a, b), abs=1e-6) |
| 664 | + |
| 665 | + start = a.position |
| 666 | + # A little further than depth separates them |
| 667 | + a.position = start + info.normal * (info.depth + 1e-6) |
| 668 | + assert arcade.check_for_collision(a, b) is False |
| 669 | + # A little less doesn't |
| 670 | + if info.depth > 1e-5: |
| 671 | + a.position = start + info.normal * (info.depth - 1e-6) |
| 672 | + assert arcade.check_for_collision(a, b) is True |
| 673 | + a.position = start |
| 674 | + assert checked > 300 |
| 675 | + |
| 676 | + |
537 | 677 | def test_check_for_collision_with_list(window): |
538 | 678 | # TODO: Check that the right collision function is called internally |
539 | 679 | a = arcade.SpriteSolidColor(50, 50, color=arcade.csscolor.RED) |
|
0 commit comments