-
Notifications
You must be signed in to change notification settings - Fork 359
Collision detection optimizations, benchmark runner, and collision detection benchmark #1524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
einarf
merged 6 commits into
pythonarcade:development
from
cspotcode:collision-detection-optimizations-2
Feb 17, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a2f9282
Add benchmark runner and collisions benchmark
cspotcode 8b909d2
get_adjusted_hit_box: inline rotate_point() call, avoid attribute loo…
cspotcode 335899f
get_adjusted_hit_box: list comprehension into tuple is faster than ge…
cspotcode 86931fa
_check_for_collision: optimize broadphase
cspotcode 9dd35a2
fix a logic mistake causing bad y coordinates in adjusted hitboxes
cspotcode 7a5ba9b
fix flake8 issues
cspotcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /results.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #!/usr/bin/env bash | ||
| # On windows, can run via the bash you get with git: | ||
| # C:\Program Files\Git\bin\bash.exe | ||
|
|
||
| __dirname="$(CDPATH= cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| cd "$__dirname" | ||
| cd .. | ||
|
|
||
| bench_name="$1" | ||
|
|
||
| # I am doing this to ensure it runs against python 3.11, my default install is 3.10 | ||
| # You can probably remove this line | ||
| export PATH="/c/Users/cspot/AppData/Local/Programs/Python/Python311:$PATH" | ||
| python.exe --version | ||
|
|
||
| # Get current branch name | ||
| gitTo=$( git rev-parse --abbrev-ref HEAD ) | ||
| # Get root of this branch | ||
| gitFrom=$( git merge-base origin/development $gitTo )~1 | ||
| gitCommitRange=$gitFrom..$gitTo | ||
|
|
||
| commits=$(git log --format='%H' $gitCommitRange) | ||
| commits=$(echo "$commits" | sed -z 's/\n/,/g;s/,$/\n/') | ||
| echo "$commits" | ||
|
|
||
| hyperfine \ | ||
| --show-output \ | ||
| --export-markdown benchmarks/results.md \ | ||
| --warmup 1 --runs 2 \ | ||
| --parameter-list commit "$commits,$commits" \ | ||
| --setup 'git checkout {commit}' \ | ||
| 'python -m benchmarks.'"$bench_name"'.bench {commit}' | ||
|
|
||
| git checkout $gitTo | ||
|
|
||
| # Postprocess hyperfine's report to include commit messages and github links | ||
| python -c ' | ||
| import subprocess | ||
| import re | ||
|
|
||
| report_path = "benchmarks/results.md" | ||
| with open(report_path,"r") as file: | ||
| report = file.read() | ||
|
|
||
| def replace(x): | ||
| commit = x[1] | ||
| result = subprocess.run(["git", "log", "-n1", "--oneline", commit], capture_output=True, encoding="utf-8") | ||
| message = result.stdout.strip() | ||
| return f"[{message}](https://github.com/pythonarcade/arcade/commit/{commit})" | ||
| report = re.sub(r".*? ([a-f0-9]{40})`", replace, report) | ||
|
|
||
| with open(report_path,"w") as file: | ||
| file.write(report) | ||
| ' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import math | ||
| import arcade | ||
| import pyglet | ||
| import random | ||
| import time | ||
|
|
||
| SCREEN_WIDTH = 800 | ||
| SCREEN_HEIGHT = 600 | ||
|
|
||
| WALL_DIM_MIN = 10 | ||
| WALL_DIM_MAX = 200 | ||
| WALLS_COUNT = 10 | ||
|
|
||
| BULLET_VELOCITY_MIN = 1/60 | ||
| BULLET_VELOCITY_MAX = 10/60 | ||
| BULLET_COUNT = 1000 | ||
|
|
||
| SIMULATE_MINUTES = 1 | ||
| SIMULATE_FPS = 60 | ||
|
|
||
| # Predictable randomization so that each benchmark is identical | ||
| rng = random.Random(0) | ||
|
|
||
| bullets = arcade.SpriteList() | ||
| walls = arcade.SpriteList() | ||
|
|
||
| window = arcade.Window() | ||
|
|
||
| # Seed chosen manually to create a wall distribution that looked good enough, | ||
| # like something I might create in a game. | ||
| rng.seed(2) | ||
| for i in range(0, WALLS_COUNT): | ||
| wall = arcade.SpriteSolidColor(rng.randint(WALL_DIM_MIN, WALL_DIM_MAX), rng.randint(WALL_DIM_MIN, WALL_DIM_MAX), arcade.color.BLACK) | ||
| wall.position = rng.randint(0, SCREEN_WIDTH), rng.randint(0, SCREEN_HEIGHT) | ||
| walls.append(wall) | ||
|
|
||
| for i in range(0, BULLET_COUNT): | ||
| # Create a new bullet | ||
| new_bullet = arcade.SpriteCircle(color=arcade.color.RED, radius=10) | ||
| new_bullet.position = (rng.randint(0, SCREEN_WIDTH), rng.randint(0, SCREEN_HEIGHT)) | ||
| speed = rng.random() * (BULLET_VELOCITY_MAX - BULLET_VELOCITY_MIN) + BULLET_VELOCITY_MIN | ||
| angle = rng.random() * math.pi * 2 | ||
| new_bullet.velocity = (math.cos(angle) * speed, math.sin(angle) * speed) | ||
| # Half of bullets are rotated, to test those code paths | ||
| if rng.random() > 0.5: | ||
| new_bullet.angle = 45 | ||
| bullets.append(new_bullet) | ||
|
|
||
| for i in range(0, int(SIMULATE_MINUTES * 60 * SIMULATE_FPS)): | ||
| pyglet.clock.tick() | ||
|
|
||
| window.switch_to() | ||
| window.dispatch_events() | ||
|
|
||
| # Move all bullets | ||
| for bullet in bullets: | ||
| bullet.position = (bullet.position[0] + bullet.velocity[0], bullet.position[1] + bullet.velocity[1]) | ||
|
|
||
| # Check for collisions | ||
| bullets_w_collision = [] | ||
| for bullet in bullets: | ||
| walls_hit = arcade.check_for_collision_with_list(bullet, walls) | ||
| if walls_hit: | ||
| bullets_w_collision.append(bullet) | ||
| for bullet in bullets_w_collision: | ||
| # bullets.remove(bullet) | ||
| bullet.position = (rng.randint(0, SCREEN_WIDTH), rng.randint(0, SCREEN_HEIGHT)) | ||
|
|
||
| window.dispatch_event('on_draw') | ||
|
|
||
| window.clear(color=arcade.color.WHITE) | ||
| walls.draw() | ||
| bullets.draw() | ||
| window.flip() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.