Skip to content

Commit 733a285

Browse files
Collision detection (#6)
* collision_detection initial commit --------- Co-authored-by: parikshit2111 <[email protected]>
1 parent ff19539 commit 733a285

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@
878878
* [Casimir Effect](physics/casimir_effect.py)
879879
* [Center Of Mass](physics/center_of_mass.py)
880880
* [Centripetal Force](physics/centripetal_force.py)
881+
* [Collision Detection](physics/collision_detection.py)
881882
* [Coulombs Law](physics/coulombs_law.py)
882883
* [Doppler Frequency](physics/doppler_frequency.py)
883884
* [Grahams Law](physics/grahams_law.py)

physics/collision_detection.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Title : AABB Collision Detection and Counter
3+
4+
Description : This program simulates two moving boxes that bounce back when they
5+
collide with each other or with the edges of the screen. A collision counter
6+
increments each time the boxes collide, except when they touch the edges of the
7+
screen, where they rebound without increasing the counter. The motion and
8+
collision logic demonstrate axis-aligned bounding box (AABB) collision detection.
9+
10+
The program is implemented using Pygame and features:
11+
- Two boxes moving towards each other
12+
- Collision detection between the boxes
13+
- Edge collision handling (without counter increment)
14+
- A visual counter displaying the number of collisions
15+
16+
Source :
17+
- https://en.wikipedia.org/wiki/Bounding_volume
18+
- https://www.pygame.org/docs/
19+
"""
20+
21+
import pygame
22+
23+
# Initialize Pygame
24+
pygame.init()
25+
26+
# Constants for screen dimensions and box properties
27+
WIDTH, HEIGHT = 500, 300 # Screen width and height
28+
BOX_SIZE = 50 # Size of each box
29+
SPEED = 3 # Speed of movement
30+
31+
# Colors
32+
WHITE = (255, 255, 255) # Background color
33+
RED = (255, 0, 0) # Box 1 color
34+
BLUE = (0, 0, 255) # Box 2 color
35+
36+
# Create display window
37+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
38+
pygame.display.set_caption("AABB Collision Detection")
39+
40+
# Initial positions of the boxes
41+
box1_x, box1_y = 50, HEIGHT // 2 - BOX_SIZE // 2
42+
box2_x, box2_y = WIDTH - 100, HEIGHT // 2 - BOX_SIZE // 2
43+
44+
# Movement directions
45+
box1_dir = SPEED
46+
box2_dir = -SPEED
47+
48+
# Collision counter
49+
collision_count = 0
50+
51+
# Main game loop
52+
running = True
53+
while running:
54+
pygame.time.delay(20) # Controls the frame rate
55+
screen.fill(WHITE) # Clear screen before drawing
56+
57+
# Move the boxes
58+
box1_x += box1_dir
59+
box2_x += box2_dir
60+
61+
# Collision detection between the two boxes
62+
if box1_x + BOX_SIZE > box2_x:
63+
# Only increase the counter if they overlap beyond just touching edges
64+
if box1_x + BOX_SIZE > box2_x + 1 or box2_x > box1_x + 1:
65+
collision_count += 1
66+
box1_dir *= -1 # Reverse direction
67+
box2_dir *= -1 # Reverse direction
68+
69+
# Edge collision detection (bouncing without increasing counter)
70+
if box1_x <= 0 or box1_x + BOX_SIZE >= WIDTH:
71+
box1_dir *= -1
72+
if box2_x <= 0 or box2_x + BOX_SIZE >= WIDTH:
73+
box2_dir *= -1
74+
75+
# Draw the boxes
76+
pygame.draw.rect(screen, RED, (box1_x, box1_y, BOX_SIZE, BOX_SIZE))
77+
pygame.draw.rect(screen, BLUE, (box2_x, box2_y, BOX_SIZE, BOX_SIZE))
78+
79+
# Display the collision count
80+
font = pygame.font.Font(None, 36)
81+
text = font.render("Collisions: " + str(collision_count), True, (0, 0, 0))
82+
screen.blit(text, (10, 10))
83+
84+
# Event handling
85+
for event in pygame.event.get():
86+
if event.type == pygame.QUIT:
87+
running = False
88+
print("Number of collisions occured are",collision_count)
89+
90+
91+
pygame.display.update()
92+
93+
# Quit Pygame
94+
pygame.quit()

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ sympy
1717
tweepy
1818
typing_extensions
1919
xgboost
20+
pygame

0 commit comments

Comments
 (0)