Skip to content
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

feat:added collision detection #12607

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@
* [Casimir Effect](physics/casimir_effect.py)
* [Center Of Mass](physics/center_of_mass.py)
* [Centripetal Force](physics/centripetal_force.py)
* [Collision Detection](physics/collision_detection.py)
* [Coulombs Law](physics/coulombs_law.py)
* [Doppler Frequency](physics/doppler_frequency.py)
* [Grahams Law](physics/grahams_law.py)
Expand Down
91 changes: 91 additions & 0 deletions physics/collision_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
Title : AABB Collision Detection and Counter

Description : This program simulates two moving boxes that bounce back when they
collide with each other or with the edges of the screen. A collision counter
increments each time the boxes collide, except when they touch the edges of the
screen, where they rebound without increasing the counter. The motion and
collision logic demonstrate axis-aligned bounding box (AABB) collision detection.

The program is implemented using Pygame and features:
- Two boxes moving towards each other
- Collision detection between the boxes
- Edge collision handling (without counter increment)
- A visual counter displaying the number of collisions

Source :
- https://en.wikipedia.org/wiki/Bounding_volume
- https://www.pygame.org/docs/
"""

import pygame

# Initialize Pygame
pygame.init()

# Constants for screen dimensions and box properties
WIDTH, HEIGHT = 500, 300 # Screen width and height
BOX_SIZE = 50 # Size of each box
SPEED = 3 # Speed of movement

# Colors
WHITE = (255, 255, 255) # Background color
RED = (255, 0, 0) # Box 1 color
BLUE = (0, 0, 255) # Box 2 color

# Create display window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AABB Collision Detection")

# Initial positions of the boxes
box1_x, box1_y = 50, HEIGHT // 2 - BOX_SIZE // 2
box2_x, box2_y = WIDTH - 100, HEIGHT // 2 - BOX_SIZE // 2

# Movement directions
box1_dir = SPEED
box2_dir = -SPEED

# Collision counter
collision_count = 0

# Main game loop
running = True
while running:
pygame.time.delay(20) # Controls the frame rate
screen.fill(WHITE) # Clear screen before drawing

# Move the boxes
box1_x += box1_dir
box2_x += box2_dir

# Collision detection between the two boxes
if box1_x + BOX_SIZE > box2_x:
# Only increase the counter if they overlap beyond just touching edges
if box1_x + BOX_SIZE > box2_x + 1 or box2_x > box1_x + 1:
collision_count += 1
box1_dir *= -1 # Reverse direction
box2_dir *= -1 # Reverse direction

# Edge collision detection (bouncing without increasing counter)
if box1_x <= 0 or box1_x + BOX_SIZE >= WIDTH:
box1_dir *= -1
if box2_x <= 0 or box2_x + BOX_SIZE >= WIDTH:
box2_dir *= -1

# Draw the boxes
pygame.draw.rect(screen, RED, (box1_x, box1_y, BOX_SIZE, BOX_SIZE))
pygame.draw.rect(screen, BLUE, (box2_x, box2_y, BOX_SIZE, BOX_SIZE))

# Display the collision count
font = pygame.font.Font(None, 36)
text = font.render("Collisions: " + str(collision_count), True, (0, 0, 0))
screen.blit(text, (10, 10))

# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
print("Number of collisions occurred are", collision_count)
pygame.display.update()
# Quit Pygame
pygame.quit()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ numpy
opencv-python
pandas
pillow
pygame
requests
rich
scikit-learn
Expand Down
81 changes: 18 additions & 63 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading