|
| 1 | +import csv |
| 2 | +from datetime import datetime, timedelta |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pygame |
| 7 | +import pymunk |
| 8 | +import pymunk.pygame_util |
| 9 | +import typer |
| 10 | + |
| 11 | + |
| 12 | +def ball_fall(n_balls: int, debug_vis: bool, n_iter: int = 1000) -> timedelta: |
| 13 | + space = pymunk.Space() |
| 14 | + # 1. Flip Gravity: Positive Y pulls "down" in PyGame coordinates |
| 15 | + space.gravity = (0, 900) |
| 16 | + |
| 17 | + static_body = space.static_body |
| 18 | + # 2. Invert Container: Floor is now at Y=800, walls go up toward Y=50 |
| 19 | + segments = [ |
| 20 | + pymunk.Segment(static_body, (50, 800), (550, 800), 5), # Floor (Bottom) |
| 21 | + pymunk.Segment(static_body, (50, 800), (50, 50), 5), # Left Wall |
| 22 | + pymunk.Segment(static_body, (550, 800), (550, 50), 5), # Right Wall |
| 23 | + ] |
| 24 | + for seg in segments: |
| 25 | + seg.elasticity = 0.4 |
| 26 | + seg.friction = 0.5 |
| 27 | + space.add(*segments) |
| 28 | + |
| 29 | + radius = 4 |
| 30 | + mass = 1 |
| 31 | + moment = pymunk.moment_for_circle(mass, 0, radius) |
| 32 | + |
| 33 | + rng = np.random.default_rng() |
| 34 | + # 3. Invert Spawn: Balls start near the top (Y=100 to 400) |
| 35 | + x_coords = rng.uniform(70, 530, n_balls) |
| 36 | + y_coords = rng.uniform(50, 400, n_balls) |
| 37 | + |
| 38 | + for i in range(n_balls): |
| 39 | + body = pymunk.Body(mass, moment) |
| 40 | + body.position = (float(x_coords[i]), float(y_coords[i])) |
| 41 | + shape = pymunk.Circle(body, radius) |
| 42 | + shape.elasticity = 0.5 |
| 43 | + shape.friction = 0.5 |
| 44 | + space.add(body, shape) |
| 45 | + |
| 46 | + if debug_vis: |
| 47 | + pygame.init() |
| 48 | + screen = pygame.display.set_mode((600, 850)) |
| 49 | + pygame.display.set_caption(f"Inverted Gravity Benchmark: {n_balls} balls") |
| 50 | + draw_options = pymunk.pygame_util.DrawOptions(screen) |
| 51 | + else: |
| 52 | + screen = None |
| 53 | + draw_options = None |
| 54 | + |
| 55 | + start = datetime.now() |
| 56 | + for _ in range(n_iter): |
| 57 | + if screen is not None and draw_options is not None: |
| 58 | + for event in pygame.event.get(): |
| 59 | + if event.type == pygame.QUIT: |
| 60 | + pygame.quit() |
| 61 | + return datetime.now() - start |
| 62 | + |
| 63 | + screen.fill((255, 255, 255)) |
| 64 | + space.debug_draw(draw_options) |
| 65 | + pygame.display.flip() |
| 66 | + |
| 67 | + space.step(0.002) |
| 68 | + |
| 69 | + if debug_vis: |
| 70 | + pygame.quit() |
| 71 | + |
| 72 | + return datetime.now() - start |
| 73 | + |
| 74 | + |
| 75 | +def main( |
| 76 | + count_min: int = 1000, |
| 77 | + count_max: int = 100000, |
| 78 | + count_interval: int = 1000, |
| 79 | + debug_vis: bool = False, |
| 80 | + filename: Path = Path("bench.csv"), |
| 81 | +) -> None: |
| 82 | + results = [] |
| 83 | + |
| 84 | + for count in [x for x in range(count_min, count_max + 1, count_interval)]: |
| 85 | + duration = ball_fall(count, debug_vis) |
| 86 | + # Convert timedelta to total seconds as a float for the CSV |
| 87 | + seconds = duration.total_seconds() |
| 88 | + results.append((count, seconds)) |
| 89 | + |
| 90 | + if not debug_vis: |
| 91 | + with open(filename, mode="w", newline="") as file: |
| 92 | + writer = csv.writer(file) |
| 93 | + writer.writerow(["n_balls", "duration_seconds"]) # Header |
| 94 | + writer.writerows(results) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + typer.run(main) |
0 commit comments